Commit a4026835 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'feature/new-merge-request-url' into 'master'

Show Merge request link after pushing if new brach

This is part of implementation for issue https://gitlab.com/gitlab-org/gitlab-ce/issues/18266

See merge request !77
parents 1e3d15a7 9ba4a0aa
......@@ -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
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment