From 9ba4a0aa4fc104189873134644a2d5da28f5b4b7 Mon Sep 17 00:00:00 2001 From: Scott Le Date: Thu, 28 Jul 2016 12:17:04 +0700 Subject: print new merge request urls after push received update spec different text for new and existing merge request update spec fix style switch order of messages enhance message appearance remove extra spaces --- lib/gitlab_net.rb | 7 +++ lib/gitlab_post_receive.rb | 30 +++++++++- spec/gitlab_post_receive_spec.rb | 124 ++++++++++++++++++++++++++++++++------- 3 files changed, 138 insertions(+), 23 deletions(-) diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index e10a07a..c06ed1e 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -54,6 +54,13 @@ class GitlabNet JSON.parse(resp.body) rescue {} end + def merge_request_urls(repo_name, changes) + changes = changes.join("\n") unless changes.kind_of?(String) + changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '') + resp = get("#{host}/merge_request_urls?project=#{URI.escape(repo_name)}&changes=#{URI.escape(changes)}") + JSON.parse(resp.body) rescue [] + end + def check get("#{host}/check", read_timeout: CHECK_TIMEOUT) end diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index 7874c85..9ad74f5 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -6,11 +6,14 @@ require 'base64' require 'securerandom' class GitlabPostReceive + include NamesHelper + attr_reader :config, :repo_path, :changes, :jid def initialize(repo_path, actor, changes) @config = GitlabConfig.new @repo_path, @actor = repo_path.strip, actor + @repo_name = extract_repo_name(@repo_path.dup) @changes = changes @jid = SecureRandom.hex(12) end @@ -19,12 +22,15 @@ class GitlabPostReceive result = update_redis begin - broadcast_message = GitlabNet.new.broadcast_message + broadcast_message = api.broadcast_message if broadcast_message.has_key?("message") puts print_broadcast_message(broadcast_message["message"]) end + + merge_request_urls = api.merge_request_urls(@repo_name, @changes) + print_merge_request_links(merge_request_urls) rescue GitlabNet::ApiUnreachableError nil end @@ -34,6 +40,28 @@ class GitlabPostReceive protected + def api + @api ||= GitlabNet.new + end + + def print_merge_request_links(merge_request_urls) + return if merge_request_urls.empty? + puts + merge_request_urls.each { |mr| print_merge_request_link(mr) } + end + + def print_merge_request_link(merge_request) + if merge_request["new_merge_request"] + message = "Create merge request for #{merge_request["branch_name"]}:" + else + message = "View merge request for #{merge_request["branch_name"]}:" + end + + puts message + puts((" " * 2) + merge_request["url"]) + puts + end + def print_broadcast_message(message) # A standard terminal window is (at least) 80 characters wide. total_width = 80 diff --git a/spec/gitlab_post_receive_spec.rb b/spec/gitlab_post_receive_spec.rb index 6762d49..db1e776 100644 --- a/spec/gitlab_post_receive_spec.rb +++ b/spec/gitlab_post_receive_spec.rb @@ -17,12 +17,12 @@ describe GitlabPostReceive do before do GitlabConfig.any_instance.stub(repos_path: repository_path) - GitlabNet.any_instance.stub(broadcast_message: { "message" => message }) + GitlabNet.any_instance.stub(broadcast_message: { }) + GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) { [] } expect(Time).to receive(:now).and_return(enqueued_at) end describe "#exec" do - before do allow_any_instance_of(GitlabNet).to receive(:redis_client).and_return(redis_client) allow_any_instance_of(GitlabReferenceCounter).to receive(:redis_client).and_return(redis_client) @@ -32,27 +32,107 @@ describe GitlabPostReceive do allow(redis_client).to receive(:rpush).and_return(true) end - it "prints the broadcast message" do - expect(redis_client).to receive(:rpush) - expect(gitlab_post_receive).to receive(:puts).ordered - expect(gitlab_post_receive).to receive(:puts).with( - "========================================================================" - ).ordered - expect(gitlab_post_receive).to receive(:puts).ordered - - expect(gitlab_post_receive).to receive(:puts).with( - " test test test test test test test test test test message message" - ).ordered - expect(gitlab_post_receive).to receive(:puts).with( - " message message message message message message message message" - ).ordered - - expect(gitlab_post_receive).to receive(:puts).ordered - expect(gitlab_post_receive).to receive(:puts).with( - "========================================================================" - ).ordered + context 'Without broad cast message' do + context 'pushing new branch' do + before do + GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) do + [{ + "branch_name" => "new_branch", + "url" => "http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch", + "new_merge_request" => true + }] + end + end - gitlab_post_receive.exec + it "prints the new merge request url" do + expect(redis_client).to receive(:rpush) + + expect(gitlab_post_receive).to receive(:puts).ordered + expect(gitlab_post_receive).to receive(:puts).with( + "Create merge request for new_branch:" + ).ordered + expect(gitlab_post_receive).to receive(:puts).with( + " http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch" + ).ordered + expect(gitlab_post_receive).to receive(:puts).ordered + + gitlab_post_receive.exec + end + end + + context 'pushing existing branch with merge request created' do + before do + GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) do + [{ + "branch_name" => "feature_branch", + "url" => "http://localhost/dzaporozhets/gitlab-ci/merge_requests/1", + "new_merge_request" => false + }] + end + end + + it "prints the view merge request url" do + expect(redis_client).to receive(:rpush) + + expect(gitlab_post_receive).to receive(:puts).ordered + expect(gitlab_post_receive).to receive(:puts).with( + "View merge request for feature_branch:" + ).ordered + expect(gitlab_post_receive).to receive(:puts).with( + " http://localhost/dzaporozhets/gitlab-ci/merge_requests/1" + ).ordered + expect(gitlab_post_receive).to receive(:puts).ordered + + gitlab_post_receive.exec + end + end + end + + context 'show broadcast message and merge request link' do + before do + GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) do + [{ + "branch_name" => "new_branch", + "url" => "http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch", + "new_merge_request" => true + }] + end + GitlabNet.any_instance.stub(broadcast_message: { "message" => message }) + end + + it 'prints the broadcast message and create new merge request link' do + expect(redis_client).to receive(:rpush) + + expect(gitlab_post_receive).to receive(:puts).ordered + expect(gitlab_post_receive).to receive(:puts).with( + "========================================================================" + ).ordered + expect(gitlab_post_receive).to receive(:puts).ordered + + expect(gitlab_post_receive).to receive(:puts).with( + " test test test test test test test test test test message message" + ).ordered + expect(gitlab_post_receive).to receive(:puts).with( + " message message message message message message message message" + ).ordered + + expect(gitlab_post_receive).to receive(:puts).ordered + expect(gitlab_post_receive).to receive(:puts).with( + "========================================================================" + ).ordered + + expect(gitlab_post_receive).to receive(:puts).ordered + expect(gitlab_post_receive).to receive(:puts).with( + "Create merge request for new_branch:" + ).ordered + expect(gitlab_post_receive).to receive(:puts).with( + " http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch" + ).ordered + expect(gitlab_post_receive).to receive(:puts).ordered + + + gitlab_post_receive.exec + end end it "pushes a Sidekiq job onto the queue" do -- cgit v1.2.1