summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2018-12-14 09:37:37 +0000
committerDouwe Maan <douwe@gitlab.com>2018-12-14 09:37:37 +0000
commit4581f9d89be721bf853ceac433c2492dd9438c5a (patch)
tree69198bb4580416a9aa2af1473d80e108a997ce5d
parentcdd9e12beb06354ff0babfb18907d3f184bac547 (diff)
parente4d62acc8d0c1325e6451ce222e7f4d5bdd55fc7 (diff)
downloadgitlab-shell-4581f9d89be721bf853ceac433c2492dd9438c5a.tar.gz
Merge branch 'handle-push-options' into 'master'
Handle push options See merge request gitlab-org/gitlab-shell!166
-rwxr-xr-xhooks/post-receive5
-rw-r--r--lib/gitlab_net.rb5
-rw-r--r--lib/gitlab_post_receive.rb5
-rw-r--r--lib/hooks_utils.rb15
-rw-r--r--spec/gitlab_net_spec.rb5
-rw-r--r--spec/gitlab_post_receive_spec.rb3
-rw-r--r--spec/hooks_utils_spec.rb22
7 files changed, 52 insertions, 8 deletions
diff --git a/hooks/post-receive b/hooks/post-receive
index 30f4be1..2b6538f 100755
--- a/hooks/post-receive
+++ b/hooks/post-receive
@@ -9,9 +9,12 @@ gl_repository = ENV['GL_REPOSITORY']
repo_path = Dir.pwd
require_relative '../lib/gitlab_custom_hook'
+require_relative '../lib/hooks_utils'
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)
exit 0
else
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb
index cae3bdb..a84ba84 100644
--- a/lib/gitlab_net.rb
+++ b/lib/gitlab_net.rb
@@ -119,11 +119,12 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
false
end
- def post_receive(gl_repository, identifier, changes)
+ def post_receive(gl_repository, identifier, changes, push_options)
params = {
gl_repository: gl_repository,
identifier: identifier,
- changes: changes
+ changes: changes,
+ :"push_options[]" => push_options, # rubocop:disable Style/HashSyntax
}
resp = post("#{internal_api_endpoint}/post_receive", params)
diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb
index ea57f6a..2d412b9 100644
--- a/lib/gitlab_post_receive.rb
+++ b/lib/gitlab_post_receive.rb
@@ -8,18 +8,19 @@ require 'securerandom'
class GitlabPostReceive
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
@gl_repository = gl_repository
@repo_path = repo_path.strip
@actor = actor
@changes = changes
+ @push_options = push_options
@jid = SecureRandom.hex(12)
end
def exec
response = GitlabMetrics.measure("post-receive") do
- api.post_receive(gl_repository, @actor, changes)
+ api.post_receive(gl_repository, @actor, changes, @push_options)
end
return false unless response
diff --git a/lib/hooks_utils.rb b/lib/hooks_utils.rb
new file mode 100644
index 0000000..b11317c
--- /dev/null
+++ b/lib/hooks_utils.rb
@@ -0,0 +1,15 @@
+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
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb
index 07b772b..1ca4096 100644
--- a/spec/gitlab_net_spec.rb
+++ b/spec/gitlab_net_spec.rb
@@ -174,8 +174,9 @@ describe GitlabNet, vcr: true do
describe '#post_receive' do
let(:gl_repository) { "project-1" }
let(:changes) { "123456 789012 refs/heads/test\n654321 210987 refs/tags/tag" }
+ let(:push_options) { ["ci-skip", "something unexpected"] }
let(:params) do
- { gl_repository: gl_repository, identifier: key, changes: changes }
+ { gl_repository: gl_repository, identifier: key, changes: changes, :"push_options[]" => push_options }
end
let(:merge_request_urls) do
[{
@@ -185,7 +186,7 @@ describe GitlabNet, vcr: true do
}]
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
expect_any_instance_of(Net::HTTP::Post).to receive(:set_form_data).with(hash_including(params))
diff --git a/spec/gitlab_post_receive_spec.rb b/spec/gitlab_post_receive_spec.rb
index 7dd1828..3cae631 100644
--- a/spec/gitlab_post_receive_spec.rb
+++ b/spec/gitlab_post_receive_spec.rb
@@ -11,7 +11,8 @@ describe GitlabPostReceive do
let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) }
let(:repo_path) { File.join(repository_path, repo_name) + ".git" }
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(:enqueued_at) { Time.new(2016, 6, 23, 6, 59) }
let(:new_merge_request_urls) do
diff --git a/spec/hooks_utils_spec.rb b/spec/hooks_utils_spec.rb
new file mode 100644
index 0000000..5246bbc
--- /dev/null
+++ b/spec/hooks_utils_spec.rb
@@ -0,0 +1,22 @@
+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