diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 438c626bd57599dd1d7ae8fbee7fd693e624cebc..7a48d10ac5387a2c8b0a1c3b246ee623759cd352 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -73,6 +73,11 @@ class GitlabNet JSON.parse(resp.body) rescue [] end + def project_settings(repo_path) + resp = get("#{host_v3}/project_settings?project=#{URI.escape(repo_path)}") + JSON.parse(resp.body) rescue {} + end + def check get("#{host_v3}/check", read_timeout: CHECK_TIMEOUT) end diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index 8383135af992cdada17bf97409dcc16c30e753ed..aa2948792750cc9f15a7866fee7ca046d86e12d5 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -31,10 +31,13 @@ class GitlabPostReceive print_broadcast_message(broadcast_message["message"]) end - merge_request_urls = GitlabMetrics.measure("merge-request-urls") do - api.merge_request_urls(@repo_path, @changes) + project_settings = api.project_settings(@repo_path) + if project_settings['printing_merge_request_link_enabled'] + merge_request_urls = GitlabMetrics.measure("merge-request-urls") do + api.merge_request_urls(@repo_path, @changes) + end + print_merge_request_links(merge_request_urls) end - print_merge_request_links(merge_request_urls) api.notify_post_receive(repo_path) rescue GitlabNet::ApiUnreachableError diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb index 1b64567a3de8d39dcacc68a61413fcfbdddaef37..c6ffd8cecafcc82798610ba5c1b44033856519bb 100644 --- a/spec/gitlab_net_spec.rb +++ b/spec/gitlab_net_spec.rb @@ -91,6 +91,25 @@ describe GitlabNet, vcr: true do end end + describe :project_settings do + let(:repo_path) { 'gitlab-org/gitlab-ce.git' } + + it 'should return project settings hash' do + VCR.use_cassette('project-settings') do + result = gitlab_net.project_settings(repo_path) + result['printing_merge_request_link_enabled'].should be(true) + end + end + + it 'should return empty hash on error' do + gitlab_net.stub(:get).and_return('Error!') + + result = gitlab_net.project_settings(repo_path) + + result.should eq({}) + end + end + describe :authorized_key do let (:ssh_key) { "AAAAB3NzaC1yc2EAAAADAQABAAACAQDPKPqqnqQ9PDFw65cO7iHXrKw6ucSZg8Bd2CZ150Yy1YRDPJOWeRNCnddS+M/Lk" } diff --git a/spec/gitlab_post_receive_spec.rb b/spec/gitlab_post_receive_spec.rb index c63c267308b8e36d5dd85c650c28ddf9f6a069f5..c4388d9634bf354cd33519d4a888356fd1259135 100644 --- a/spec/gitlab_post_receive_spec.rb +++ b/spec/gitlab_post_receive_spec.rb @@ -12,12 +12,14 @@ describe GitlabPostReceive do let(:repo_path) { File.join(repository_path, repo_name) + ".git" } let(:gitlab_post_receive) { GitlabPostReceive.new(repo_path, actor, wrongly_encoded_changes) } let(:message) { "test " * 10 + "message " * 10 } + let(:project_settings) { { 'printing_merge_request_link_enabled' => true } } let(:redis_client) { double('redis_client') } let(:enqueued_at) { Time.new(2016, 6, 23, 6, 59) } before do GitlabConfig.any_instance.stub(repos_path: repository_path) GitlabNet.any_instance.stub(broadcast_message: { }) + GitlabNet.any_instance.stub(:project_settings).with(repo_path) { project_settings } GitlabNet.any_instance.stub(:merge_request_urls).with(repo_path, wrongly_encoded_changes) { [] } GitlabNet.any_instance.stub(notify_post_receive: true) expect(Time).to receive(:now).and_return(enqueued_at) @@ -45,19 +47,36 @@ describe GitlabPostReceive do end end - it "prints the new merge request url" do - expect(redis_client).to receive(:rpush) + context 'when printing merge request urls is enabled' do + it "prints the new merge request url" do + GitlabNet.any_instance.stub(:project_settings).and_return({ + 'printing_merge_request_link_enabled' => true + }) + + expect(redis_client).to receive(:rpush) + expect(gitlab_post_receive).to receive(:puts).ordered + expect(gitlab_post_receive).to receive(:puts).with( + "To create a merge request for new_branch, visit:" + ).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 - expect(gitlab_post_receive).to receive(:puts).ordered - expect(gitlab_post_receive).to receive(:puts).with( - "To create a merge request for new_branch, visit:" - ).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 + context 'when printing merge request urls is not enabled' do + it "does not print the new merge request url" do + GitlabNet.any_instance.stub(:project_settings).and_return({ + 'printing_merge_request_link_enabled' => false + }) - gitlab_post_receive.exec + expect(gitlab_post_receive).not_to receive(:puts) + + gitlab_post_receive.exec + end end end @@ -72,19 +91,36 @@ describe GitlabPostReceive do end end - it "prints the view merge request url" do - expect(redis_client).to receive(:rpush) + context 'when printing merge request urls is enabled' do + it "prints the view merge request url" do + GitlabNet.any_instance.stub(:project_settings).and_return({ + 'printing_merge_request_link_enabled' => true + }) + + 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 - 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 + context 'when printing merge request urls is not enabled' do + it "does not print the new merge request url" do + GitlabNet.any_instance.stub(:project_settings).and_return({ + 'printing_merge_request_link_enabled' => false + }) - gitlab_post_receive.exec + expect(gitlab_post_receive).not_to receive(:puts) + + gitlab_post_receive.exec + end end end end diff --git a/spec/vcr_cassettes/project-settings.yml b/spec/vcr_cassettes/project-settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..c4ac3b6dc05cad605062f19646c3e2d3621fa441 --- /dev/null +++ b/spec/vcr_cassettes/project-settings.yml @@ -0,0 +1,46 @@ +--- +http_interactions: +- request: + method: get + uri: https://dev.gitlab.org/api/v3/internal/project_settings?project=gitlab-org/gitlab-ce.git + body: + encoding: US-ASCII + string: secret_token=a123 + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Thu, 02 Mar 2017 00:58:26 GMT + Content-Type: + - application/json + Content-Length: + - '44' + Connection: + - keep-alive + Cache-Control: + - no-cache + Vary: + - Origin + X-Request-Id: + - 1d1c20cc-04e4-4b1b-bc84-e4cf54ea7e49 + X-Runtime: + - '0.005841' + body: + encoding: UTF-8 + string: '{"printing_merge_request_link_enabled":true}' + http_version: + recorded_at: Thu, 02 Mar 2017 00:58:26 GMT +recorded_with: VCR 2.4.0