As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit 730ebf83 authored by Alejandro Rodríguez's avatar Alejandro Rodríguez
Browse files

Add POST notification to gitaly on post-receive

parent 2e1299cc
No related branches found
No related tags found
No related merge requests found
require_relative 'http_client'
class GitalyClient < HttpClient
attr_reader :gitaly_url
def initialize(gitaly_url)
@gitaly_url = gitaly_url
end
def notify_post_receive(repo_path)
url = "#{gitaly_url}/post-receive"
params = { project: sanitize_path(repo_path) }
resp = post(url, params)
resp.code == '200'
end
end
Loading
Loading
@@ -84,6 +84,20 @@ class GitlabNet < HttpClient
{}
end
def gitaly_socket_path
return @gitaly_socket_path if defined?(@gitaly_socket_path)
@gitaly_socket_path = begin
resp = get("#{host}/gitaly")
if resp.code == '200' && (socket_path = JSON.parse(resp.body)['socket_path'])
"http+unix://#{socket_path.gsub('/', '%2F')}"
else
nil
end
end
end
def redis_client
redis_config = config.redis
database = redis_config['database'] || 0
Loading
Loading
require_relative 'gitlab_init'
require_relative 'gitlab_net'
require_relative 'gitaly_client'
require_relative 'gitlab_reference_counter'
require_relative 'gitlab_metrics'
require 'json'
Loading
Loading
@@ -35,6 +36,8 @@ class GitlabPostReceive
api.merge_request_urls(@repo_path, @changes)
end
print_merge_request_links(merge_request_urls)
result &&= notify_gitaly
rescue HttpClient::ApiUnreachableError
nil
end
Loading
Loading
@@ -121,4 +124,21 @@ class GitlabPostReceive
false
end
end
def notify_gitaly
return true unless gitaly_client
gitaly_client.notify_post_receive(repo_path)
end
def gitaly_client
unless defined?(@gitaly_client)
socket_path = api.gitaly_socket_path
@gitaly_client = socket_path && GitalyClient.new(socket_path)
end
@gitaly_client
rescue HttpClient::ApiUnreachableError
nil
end
end
require_relative 'spec_helper'
require_relative '../lib/gitaly_client'
describe GitalyClient, vcr: true do
include WebMock::API
let(:gitaly_client) { GitalyClient.new('http+unix://%2Fpath%2Fto%2Fgitaly.socket') }
let(:repo_url) { 'gitlab/gitlabhq.git' }
describe :notify_post_receive do
before do
stub_request(:post, /.*/)
end
it 'should return 200 code for gitlab check' do
Net::HTTP::Post.any_instance.should_receive(:set_form_data).with(project: repo_url)
gitaly_client.notify_post_receive(repo_url)
end
it "raises an exception if the connection fails" do
Net::HTTP.any_instance.stub(:request).and_raise(StandardError)
expect { gitaly_client.notify_post_receive(repo_url) }.to raise_error(GitalyClient::ApiUnreachableError)
end
after do
WebMock.reset!
end
end
end
Loading
Loading
@@ -17,9 +17,10 @@ describe GitlabPostReceive do
before do
GitlabConfig.any_instance.stub(repos_path: repository_path)
GitlabNet.any_instance.stub(gitaly_socket_path: 'http+unix://%2Fpath%2Fto%2Fgitaly.socket')
GitlabNet.any_instance.stub(broadcast_message: { })
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_path, wrongly_encoded_changes) { [] }
expect(Time).to receive(:now).and_return(enqueued_at)
allow(Time).to receive(:now).and_return(enqueued_at)
end
describe "#exec" do
Loading
Loading
@@ -47,6 +48,8 @@ describe GitlabPostReceive do
it "prints the new merge request url" do
expect(redis_client).to receive(:rpush)
expect_any_instance_of(GitalyClient).to receive(:notify_post_receive).with(repo_path)
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:"
Loading
Loading
@@ -74,6 +77,8 @@ describe GitlabPostReceive do
it "prints the view merge request url" do
expect(redis_client).to receive(:rpush)
expect_any_instance_of(GitalyClient).to receive(:notify_post_receive).with(repo_path)
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"View merge request for feature_branch:"
Loading
Loading
@@ -103,6 +108,8 @@ describe GitlabPostReceive do
it 'prints the broadcast message and create new merge request link' do
expect(redis_client).to receive(:rpush)
expect_any_instance_of(GitalyClient).to receive(:notify_post_receive).with(repo_path)
expect(gitlab_post_receive).to receive(:puts).ordered
expect(gitlab_post_receive).to receive(:puts).with(
"========================================================================"
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment