Commit 4581f9d8 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'handle-push-options' into 'master'

Handle push options

See merge request gitlab-org/gitlab-shell!166
parents cdd9e12b e4d62acc
...@@ -9,9 +9,12 @@ gl_repository = ENV['GL_REPOSITORY'] ...@@ -9,9 +9,12 @@ gl_repository = ENV['GL_REPOSITORY']
repo_path = Dir.pwd repo_path = Dir.pwd
require_relative '../lib/gitlab_custom_hook' require_relative '../lib/gitlab_custom_hook'
require_relative '../lib/hooks_utils'
require_relative '../lib/gitlab_post_receive' require_relative '../lib/gitlab_post_receive'
if GitlabPostReceive.new(gl_repository, repo_path, key_id, refs).exec && push_options = HooksUtils.get_push_options
if GitlabPostReceive.new(gl_repository, repo_path, key_id, refs, push_options).exec &&
GitlabCustomHook.new(repo_path, key_id).post_receive(refs) GitlabCustomHook.new(repo_path, key_id).post_receive(refs)
exit 0 exit 0
else else
......
...@@ -119,11 +119,12 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -119,11 +119,12 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
false false
end end
def post_receive(gl_repository, identifier, changes) def post_receive(gl_repository, identifier, changes, push_options)
params = { params = {
gl_repository: gl_repository, gl_repository: gl_repository,
identifier: identifier, identifier: identifier,
changes: changes changes: changes,
:"push_options[]" => push_options, # rubocop:disable Style/HashSyntax
} }
resp = post("#{internal_api_endpoint}/post_receive", params) resp = post("#{internal_api_endpoint}/post_receive", params)
......
...@@ -8,18 +8,19 @@ require 'securerandom' ...@@ -8,18 +8,19 @@ require 'securerandom'
class GitlabPostReceive class GitlabPostReceive
attr_reader :config, :gl_repository, :repo_path, :changes, :jid attr_reader :config, :gl_repository, :repo_path, :changes, :jid
def initialize(gl_repository, repo_path, actor, changes) def initialize(gl_repository, repo_path, actor, changes, push_options)
@config = GitlabConfig.new @config = GitlabConfig.new
@gl_repository = gl_repository @gl_repository = gl_repository
@repo_path = repo_path.strip @repo_path = repo_path.strip
@actor = actor @actor = actor
@changes = changes @changes = changes
@push_options = push_options
@jid = SecureRandom.hex(12) @jid = SecureRandom.hex(12)
end end
def exec def exec
response = GitlabMetrics.measure("post-receive") do response = GitlabMetrics.measure("post-receive") do
api.post_receive(gl_repository, @actor, changes) api.post_receive(gl_repository, @actor, changes, @push_options)
end end
return false unless response return false unless response
......
module HooksUtils
module_function
# Gets an array of Git push options from the environment
def get_push_options
count = ENV['GIT_PUSH_OPTION_COUNT'].to_i
result = []
count.times do |i|
result.push(ENV["GIT_PUSH_OPTION_#{i}"])
end
result
end
end
...@@ -174,8 +174,9 @@ describe GitlabNet, vcr: true do ...@@ -174,8 +174,9 @@ describe GitlabNet, vcr: true do
describe '#post_receive' do describe '#post_receive' do
let(:gl_repository) { "project-1" } let(:gl_repository) { "project-1" }
let(:changes) { "123456 789012 refs/heads/test\n654321 210987 refs/tags/tag" } let(:changes) { "123456 789012 refs/heads/test\n654321 210987 refs/tags/tag" }
let(:push_options) { ["ci-skip", "something unexpected"] }
let(:params) do let(:params) do
{ gl_repository: gl_repository, identifier: key, changes: changes } { gl_repository: gl_repository, identifier: key, changes: changes, :"push_options[]" => push_options }
end end
let(:merge_request_urls) do let(:merge_request_urls) do
[{ [{
...@@ -185,7 +186,7 @@ describe GitlabNet, vcr: true do ...@@ -185,7 +186,7 @@ describe GitlabNet, vcr: true do
}] }]
end end
subject { gitlab_net.post_receive(gl_repository, key, changes) } subject { gitlab_net.post_receive(gl_repository, key, changes, push_options) }
it 'sends the correct parameters' do it 'sends the correct parameters' do
expect_any_instance_of(Net::HTTP::Post).to receive(:set_form_data).with(hash_including(params)) expect_any_instance_of(Net::HTTP::Post).to receive(:set_form_data).with(hash_including(params))
......
...@@ -11,7 +11,8 @@ describe GitlabPostReceive do ...@@ -11,7 +11,8 @@ describe GitlabPostReceive do
let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) } let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) }
let(:repo_path) { File.join(repository_path, repo_name) + ".git" } let(:repo_path) { File.join(repository_path, repo_name) + ".git" }
let(:gl_repository) { "project-1" } let(:gl_repository) { "project-1" }
let(:gitlab_post_receive) { GitlabPostReceive.new(gl_repository, repo_path, actor, wrongly_encoded_changes) } let(:push_options) { [] }
let(:gitlab_post_receive) { GitlabPostReceive.new(gl_repository, repo_path, actor, wrongly_encoded_changes, push_options) }
let(:broadcast_message) { "test " * 10 + "message " * 10 } let(:broadcast_message) { "test " * 10 + "message " * 10 }
let(:enqueued_at) { Time.new(2016, 6, 23, 6, 59) } let(:enqueued_at) { Time.new(2016, 6, 23, 6, 59) }
let(:new_merge_request_urls) do let(:new_merge_request_urls) do
......
require_relative 'spec_helper'
require_relative '../lib/hooks_utils.rb'
describe :get_push_options do
context "when GIT_PUSH_OPTION_COUNT is not set" do
HooksUtils.get_push_options.should == []
end
context "when one option is given" do
ENV['GIT_PUSH_OPTION_COUNT'] = '1'
ENV['GIT_PUSH_OPTION_0'] = 'aaa'
HooksUtils.get_push_options.should == ['aaa']
end
context "when multiple options are given" do
ENV['GIT_PUSH_OPTION_COUNT'] = '3'
ENV['GIT_PUSH_OPTION_0'] = 'aaa'
ENV['GIT_PUSH_OPTION_1'] = 'bbb'
ENV['GIT_PUSH_OPTION_2'] = 'ccc'
HooksUtils.get_push_options.should == ['aaa', 'bbb', 'ccc']
end
end
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