mirror of
https://github.com/wahyd4/gitlabhq.git
synced 2026-08-10 21:26:07 +10:00
Note: This feature was developed independently on master while this was in review. I've removed the conflicting bits and left the relevant additions, mainly a test for `Gitlab::Git::Hook`. The original commit message follows: 1. `gitlab-shell` outputs errors to `stderr`, but we weren't using this information, prior to this commit. Now we capture the `stderr`, and display it in the flash message when branch creation fails. 2. This can be used to display better errors for other git operation failures with small tweaks. 3. The return value of `Gitlab::Git::Hook#trigger` is changed from a simple `true`/`false` to a tuple of `[status, errors]`. All usages and tests have been updated to reflect this change. 4. This is only relevant to branch creation _from the Web UI_, since SSH and HTTP pushes access `gitlab-shell` either directly or through `gitlab-workhorse`. 5. A few minor changes need to be made on the `gitlab-shell` end. Right now, the `stderr` message it outputs is prefixed by "GitLab: ", which shows up in our flash message. This is better removed.
71 lines
2.3 KiB
Ruby
71 lines
2.3 KiB
Ruby
require 'spec_helper'
|
|
require 'fileutils'
|
|
|
|
describe Gitlab::Git::Hook, lib: true do
|
|
describe "#trigger" do
|
|
let(:project) { create(:project) }
|
|
let(:user) { create(:user) }
|
|
|
|
def create_hook(name)
|
|
FileUtils.mkdir_p(File.join(project.repository.path, 'hooks'))
|
|
File.open(File.join(project.repository.path, 'hooks', name), 'w', 0755) do |f|
|
|
f.write('exit 0')
|
|
end
|
|
end
|
|
|
|
def create_failing_hook(name)
|
|
FileUtils.mkdir_p(File.join(project.repository.path, 'hooks'))
|
|
File.open(File.join(project.repository.path, 'hooks', name), 'w', 0755) do |f|
|
|
f.write(<<-HOOK)
|
|
echo 'regular message from the hook'
|
|
echo 'error message from the hook' 1>&2
|
|
exit 1
|
|
HOOK
|
|
end
|
|
end
|
|
|
|
['pre-receive', 'post-receive', 'update'].each do |hook_name|
|
|
|
|
context "when triggering a #{hook_name} hook" do
|
|
context "when the hook is successful" do
|
|
it "returns success with no errors" do
|
|
create_hook(hook_name)
|
|
hook = Gitlab::Git::Hook.new(hook_name, project.repository.path)
|
|
blank = Gitlab::Git::BLANK_SHA
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
|
|
|
|
status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref)
|
|
expect(status).to be true
|
|
expect(errors).to be_blank
|
|
end
|
|
end
|
|
|
|
context "when the hook is unsuccessful" do
|
|
it "returns failure with errors" do
|
|
create_failing_hook(hook_name)
|
|
hook = Gitlab::Git::Hook.new(hook_name, project.repository.path)
|
|
blank = Gitlab::Git::BLANK_SHA
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
|
|
|
|
status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref)
|
|
expect(status).to be false
|
|
expect(errors).to eq("error message from the hook\n")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
context "when the hook doesn't exist" do
|
|
it "returns success with no errors" do
|
|
hook = Gitlab::Git::Hook.new('unknown_hook', project.repository.path)
|
|
blank = Gitlab::Git::BLANK_SHA
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
|
|
|
|
status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref)
|
|
expect(status).to be true
|
|
expect(errors).to be_nil
|
|
end
|
|
end
|
|
end
|
|
end
|