summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2017-02-15 22:42:19 +0000
committerRobert Speicher <robert@gitlab.com>2017-02-15 22:42:19 +0000
commita6e5b9a17821dab311e71fa83d2a973cb5361edb (patch)
tree04e97e944380fbe114d410f084c7fa4cc05e89bb
parentf61bdfe39c6b0618144f369d5cf4df1099be9948 (diff)
parent09e4814483863b3cb570a98746c684a2709eece8 (diff)
downloadgitlab-shell-a6e5b9a17821dab311e71fa83d2a973cb5361edb.tar.gz
Merge branch 'gitaly-post-receive-2' into 'master'
Add gitaly notification on post-receive hook See merge request !119
-rw-r--r--lib/gitlab_net.rb30
-rw-r--r--lib/gitlab_post_receive.rb2
-rw-r--r--spec/gitlab_net_spec.rb25
-rw-r--r--spec/gitlab_post_receive_spec.rb10
-rw-r--r--spec/vcr_cassettes/notify-post-receive.yml44
5 files changed, 99 insertions, 12 deletions
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb
index e06557c..438c626 100644
--- a/lib/gitlab_net.rb
+++ b/lib/gitlab_net.rb
@@ -32,7 +32,7 @@ class GitlabNet
params.merge!(user_id: actor.gsub("user-", ""))
end
- url = "#{host}/allowed"
+ url = "#{host_v3}/allowed"
resp = post(url, params)
if resp.code == '200'
@@ -44,7 +44,7 @@ class GitlabNet
def discover(key)
key_id = key.gsub("key-", "")
- resp = get("#{host}/discover?key_id=#{key_id}")
+ resp = get("#{host_v3}/discover?key_id=#{key_id}")
JSON.parse(resp.body) rescue nil
end
@@ -54,7 +54,7 @@ class GitlabNet
key_id: key.gsub('key-', '')
}
- resp = post("#{host}/lfs_authenticate", params)
+ resp = post("#{host_v3}/lfs_authenticate", params)
if resp.code == '200'
GitlabLfsAuthentication.build_from_json(resp.body)
@@ -62,23 +62,23 @@ class GitlabNet
end
def broadcast_message
- resp = get("#{host}/broadcast_message")
+ resp = get("#{host_v3}/broadcast_message")
JSON.parse(resp.body) rescue {}
end
def merge_request_urls(repo_path, 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_path)}&changes=#{URI.escape(changes)}")
+ resp = get("#{host_v3}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}")
JSON.parse(resp.body) rescue []
end
def check
- get("#{host}/check", read_timeout: CHECK_TIMEOUT)
+ get("#{host_v3}/check", read_timeout: CHECK_TIMEOUT)
end
def authorized_key(key)
- resp = get("#{host}/authorized_keys?key=#{URI.escape(key, '+/=')}")
+ resp = get("#{host_v3}/authorized_keys?key=#{URI.escape(key, '+/=')}")
JSON.parse(resp.body) if resp.code == "200"
rescue
nil
@@ -86,13 +86,21 @@ class GitlabNet
def two_factor_recovery_codes(key)
key_id = key.gsub('key-', '')
- resp = post("#{host}/two_factor_recovery_codes", key_id: key_id)
+ resp = post("#{host_v3}/two_factor_recovery_codes", key_id: key_id)
JSON.parse(resp.body) if resp.code == '200'
rescue
{}
end
+ def notify_post_receive(repo_path)
+ resp = post("#{host}/notify_post_receive", repo_path: repo_path)
+
+ resp.code == '200'
+ rescue
+ false
+ end
+
def redis_client
redis_config = config.redis
database = redis_config['database'] || 0
@@ -127,10 +135,14 @@ class GitlabNet
@config ||= GitlabConfig.new
end
- def host
+ def host_v3
"#{config.gitlab_url}/api/v3/internal"
end
+ def host
+ "#{config.gitlab_url}/api/v4/internal"
+ end
+
def http_client_for(uri, options={})
if uri.is_a?(URI::HTTPUNIX)
http = Net::HTTPUNIX.new(uri.hostname)
diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb
index 7e3b310..8383135 100644
--- a/lib/gitlab_post_receive.rb
+++ b/lib/gitlab_post_receive.rb
@@ -35,6 +35,8 @@ class GitlabPostReceive
api.merge_request_urls(@repo_path, @changes)
end
print_merge_request_links(merge_request_urls)
+
+ api.notify_post_receive(repo_path)
rescue GitlabNet::ApiUnreachableError
nil
end
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb
index e3ab16c..1b64567 100644
--- a/spec/gitlab_net_spec.rb
+++ b/spec/gitlab_net_spec.rb
@@ -8,7 +8,8 @@ describe GitlabNet, vcr: true do
let(:changes) { ['0000000000000000000000000000000000000000 92d0970eefd7acb6d548878925ce2208cfe2d2ec refs/heads/branch4'] }
before do
- gitlab_net.stub(:host).and_return('https://dev.gitlab.org/api/v3/internal')
+ gitlab_net.stub(:host_v3).and_return('https://dev.gitlab.org/api/v3/internal')
+ gitlab_net.stub(:host).and_return('https://dev.gitlab.org/api/v4/internal')
gitlab_net.stub(:secret_token).and_return('a123')
end
@@ -138,6 +139,16 @@ describe GitlabNet, vcr: true do
end
end
+ describe '#notify_post_receive' do
+ let(:repo_path) { '/path/to/my/repo.git' }
+
+ it 'returns true if notification was succesful' do
+ VCR.use_cassette('notify-post-receive') do
+ expect(gitlab_net.notify_post_receive(repo_path)).to be_true
+ end
+ end
+ end
+
describe :check_access do
context 'ssh key with access to project' do
it 'should allow pull access for dev.gitlab.org' do
@@ -234,6 +245,14 @@ describe GitlabNet, vcr: true do
subject { net.send :host }
it { should include(net.send(:config).gitlab_url) }
+ it("uses API version 4") { should include("api/v4") }
+ end
+
+ describe :host_v3 do
+ let(:net) { GitlabNet.new }
+ subject { net.send :host_v3 }
+
+ it { should include(net.send(:config).gitlab_url) }
it("uses API version 3") { should include("api/v3") }
end
@@ -258,14 +277,14 @@ describe GitlabNet, vcr: true do
let(:password) { 'password' }
let(:url) { URI 'http://localhost/' }
subject { gitlab_net.send :http_request_for, :get, url }
-
+
before do
gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user }
gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password }
get.should_receive(:basic_auth).with(user, password).once
get.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')).once
end
-
+
it { should_not be_nil }
end
diff --git a/spec/gitlab_post_receive_spec.rb b/spec/gitlab_post_receive_spec.rb
index 711545f..c63c267 100644
--- a/spec/gitlab_post_receive_spec.rb
+++ b/spec/gitlab_post_receive_spec.rb
@@ -19,6 +19,7 @@ describe GitlabPostReceive do
GitlabConfig.any_instance.stub(repos_path: repository_path)
GitlabNet.any_instance.stub(broadcast_message: { })
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)
end
@@ -172,6 +173,15 @@ describe GitlabPostReceive do
end
end
+ context 'post_receive notification' do
+ it 'calls the api to notify the execution of the hook' do
+ expect_any_instance_of(GitlabNet).to receive(:notify_post_receive).
+ with(repo_path)
+
+ gitlab_post_receive.exec
+ end
+ end
+
context "when the redis command succeeds" do
before do
diff --git a/spec/vcr_cassettes/notify-post-receive.yml b/spec/vcr_cassettes/notify-post-receive.yml
new file mode 100644
index 0000000..9eeb297
--- /dev/null
+++ b/spec/vcr_cassettes/notify-post-receive.yml
@@ -0,0 +1,44 @@
+---
+http_interactions:
+- request:
+ method: post
+ uri: https://dev.gitlab.org/api/v4/internal/notify_post_receive
+ body:
+ encoding: US-ASCII
+ string: repo_path=%2Fpath%2Fto%2Fmy%2Frepo.git&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:
+ Cache-Control:
+ - max-age=0, private, must-revalidate
+ Content-Length:
+ - '2'
+ Content-Type:
+ - application/json
+ Date:
+ - Fri, 10 Feb 2017 17:06:53 GMT
+ Etag:
+ - W/"99914b932bd37a50b983c5e7c90ae93b"
+ Vary:
+ - Origin
+ X-Request-Id:
+ - cfefede6-9400-4ca5-a61d-2a519405295c
+ X-Runtime:
+ - '20.623406'
+ body:
+ encoding: UTF-8
+ string: "{}"
+ http_version:
+ recorded_at: Fri, 10 Feb 2017 17:06:53 GMT
+recorded_with: VCR 2.4.0