Add source and merge_request fields to pipeline event webhook

This commit is contained in:
Bian Jiaping 2019-09-11 19:33:45 +00:00 committed by Mayra Cabrera
parent 3fb6824892
commit 9c0e2d5b47
4 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
title: Add source and merge_request fields to pipeline event webhook
merge_request: 32373
author: Bian Jiaping
type: added

View file

@ -1001,6 +1001,7 @@ X-Gitlab-Event: Pipeline Hook
"tag": false, "tag": false,
"sha": "bcbb5ec396a2c0f828686f14fac9b80b780504f2", "sha": "bcbb5ec396a2c0f828686f14fac9b80b780504f2",
"before_sha": "bcbb5ec396a2c0f828686f14fac9b80b780504f2", "before_sha": "bcbb5ec396a2c0f828686f14fac9b80b780504f2",
"source": "merge_request_event",
"status": "success", "status": "success",
"stages":[ "stages":[
"build", "build",
@ -1016,6 +1017,18 @@ X-Gitlab-Event: Pipeline Hook
"value": "us-west-1" "value": "us-west-1"
} }
] ]
},
"merge_request": {
"id": 1,
"iid": 1,
"title": "Test",
"source_branch": "test",
"source_project_id": 1,
"target_branch": "master",
"target_project_id": 1,
"state": "opened",
"merge_status": "can_be_merged",
"url": "http://192.168.64.1:3005/gitlab-org/gitlab-test/merge_requests/1"
}, },
"user":{ "user":{
"name": "Administrator", "name": "Administrator",

View file

@ -9,6 +9,7 @@ module Gitlab
{ {
object_kind: 'pipeline', object_kind: 'pipeline',
object_attributes: hook_attrs(pipeline), object_attributes: hook_attrs(pipeline),
merge_request: pipeline.merge_request && merge_request_attrs(pipeline.merge_request),
user: pipeline.user.try(:hook_attrs), user: pipeline.user.try(:hook_attrs),
project: pipeline.project.hook_attrs(backward: false), project: pipeline.project.hook_attrs(backward: false),
commit: pipeline.commit.try(:hook_attrs), commit: pipeline.commit.try(:hook_attrs),
@ -23,6 +24,7 @@ module Gitlab
tag: pipeline.tag, tag: pipeline.tag,
sha: pipeline.sha, sha: pipeline.sha,
before_sha: pipeline.before_sha, before_sha: pipeline.before_sha,
source: pipeline.source,
status: pipeline.status, status: pipeline.status,
detailed_status: pipeline.detailed_status(nil).label, detailed_status: pipeline.detailed_status(nil).label,
stages: pipeline.stages_names, stages: pipeline.stages_names,
@ -33,6 +35,21 @@ module Gitlab
} }
end end
def merge_request_attrs(merge_request)
{
id: merge_request.id,
iid: merge_request.iid,
title: merge_request.title,
source_branch: merge_request.source_branch,
source_project_id: merge_request.source_project_id,
target_branch: merge_request.target_branch,
target_project_id: merge_request.target_project_id,
state: merge_request.state,
merge_status: merge_request.merge_status,
url: Gitlab::UrlBuilder.build(merge_request)
}
end
def build_hook_attrs(build) def build_hook_attrs(build)
{ {
id: build.id, id: build.id,

View file

@ -28,12 +28,14 @@ describe Gitlab::DataBuilder::Pipeline do
expect(attributes[:sha]).to eq(pipeline.sha) expect(attributes[:sha]).to eq(pipeline.sha)
expect(attributes[:tag]).to eq(pipeline.tag) expect(attributes[:tag]).to eq(pipeline.tag)
expect(attributes[:id]).to eq(pipeline.id) expect(attributes[:id]).to eq(pipeline.id)
expect(attributes[:source]).to eq(pipeline.source)
expect(attributes[:status]).to eq(pipeline.status) expect(attributes[:status]).to eq(pipeline.status)
expect(attributes[:detailed_status]).to eq('passed') expect(attributes[:detailed_status]).to eq('passed')
expect(build_data).to be_a(Hash) expect(build_data).to be_a(Hash)
expect(build_data[:id]).to eq(build.id) expect(build_data[:id]).to eq(build.id)
expect(build_data[:status]).to eq(build.status) expect(build_data[:status]).to eq(build.status)
expect(project_data).to eq(project.hook_attrs(backward: false)) expect(project_data).to eq(project.hook_attrs(backward: false))
expect(data[:merge_request]).to be_nil
end end
context 'pipeline without variables' do context 'pipeline without variables' do
@ -60,6 +62,22 @@ describe Gitlab::DataBuilder::Pipeline do
it 'returns a source ref' do it 'returns a source ref' do
expect(attributes[:ref]).to eq(merge_request.source_branch) expect(attributes[:ref]).to eq(merge_request.source_branch)
end end
it 'returns merge request' do
merge_request_attrs = data[:merge_request]
expect(merge_request_attrs).to be_a(Hash)
expect(merge_request_attrs[:id]).to eq(merge_request.id)
expect(merge_request_attrs[:iid]).to eq(merge_request.iid)
expect(merge_request_attrs[:title]).to eq(merge_request.title)
expect(merge_request_attrs[:source_branch]).to eq(merge_request.source_branch)
expect(merge_request_attrs[:source_project_id]).to eq(merge_request.source_project_id)
expect(merge_request_attrs[:target_branch]).to eq(merge_request.target_branch)
expect(merge_request_attrs[:target_project_id]).to eq(merge_request.target_project_id)
expect(merge_request_attrs[:state]).to eq(merge_request.state)
expect(merge_request_attrs[:merge_status]).to eq(merge_request.merge_status)
expect(merge_request_attrs[:url]).to eq("http://localhost/#{merge_request.target_project.full_path}/merge_requests/#{merge_request.iid}")
end
end end
end end
end end