mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-06 08:16:19 +02:00
Added support for new hook attributes in merge request hook
This commit is contained in:
parent
37e29b930e
commit
f9c299ce57
@ -348,36 +348,87 @@ func (g *Gitlab) Hook(req *http.Request) (*model.Repo, *model.Build, error) {
|
|||||||
func mergeRequest(parsed *client.HookPayload, req *http.Request) (*model.Repo, *model.Build, error) {
|
func mergeRequest(parsed *client.HookPayload, req *http.Request) (*model.Repo, *model.Build, error) {
|
||||||
|
|
||||||
repo := &model.Repo{}
|
repo := &model.Repo{}
|
||||||
|
|
||||||
|
obj := parsed.ObjectAttributes
|
||||||
|
if obj == nil {
|
||||||
|
return nil, nil, fmt.Errorf("object_attributes key expected in merge request hook")
|
||||||
|
}
|
||||||
|
|
||||||
|
target := obj.Target
|
||||||
|
source := obj.Source
|
||||||
|
|
||||||
|
if target == nil && source == nil {
|
||||||
|
return nil, nil, fmt.Errorf("target and source keys expected in merge request hook")
|
||||||
|
} else if target == nil {
|
||||||
|
return nil, nil, fmt.Errorf("target key expected in merge request hook")
|
||||||
|
} else if source == nil {
|
||||||
|
return nil, nil, fmt.Errorf("source key exptected in merge request hook")
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.PathWithNamespace != "" {
|
||||||
|
var err error
|
||||||
|
if repo.Owner, repo.Name, err = ExtractFromPath(target.PathWithNamespace); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
repo.FullName = target.PathWithNamespace
|
||||||
|
} else {
|
||||||
repo.Owner = req.FormValue("owner")
|
repo.Owner = req.FormValue("owner")
|
||||||
repo.Name = req.FormValue("name")
|
repo.Name = req.FormValue("name")
|
||||||
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
|
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
|
||||||
repo.Link = parsed.ObjectAttributes.Target.WebUrl
|
}
|
||||||
repo.Clone = parsed.ObjectAttributes.Target.HttpUrl
|
|
||||||
|
repo.Link = target.WebUrl
|
||||||
|
|
||||||
|
if target.GitHttpUrl != "" {
|
||||||
|
repo.Clone = target.GitHttpUrl
|
||||||
|
} else {
|
||||||
|
repo.Clone = target.HttpUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.DefaultBranch != "" {
|
||||||
|
repo.Branch = target.DefaultBranch
|
||||||
|
} else {
|
||||||
repo.Branch = "master"
|
repo.Branch = "master"
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.AvatarUrl != "" {
|
||||||
|
repo.Avatar = target.AvatarUrl
|
||||||
|
}
|
||||||
|
|
||||||
build := &model.Build{}
|
build := &model.Build{}
|
||||||
build.Event = "pull_request"
|
build.Event = "pull_request"
|
||||||
build.Message = parsed.ObjectAttributes.LastCommit.Message
|
|
||||||
build.Commit = parsed.ObjectAttributes.LastCommit.Id
|
|
||||||
//build.Remote = parsed.ObjectAttributes.Source.HttpUrl
|
|
||||||
|
|
||||||
if parsed.ObjectAttributes.SourceProjectId == parsed.ObjectAttributes.TargetProjectId {
|
lastCommit := obj.LastCommit
|
||||||
build.Ref = fmt.Sprintf("refs/heads/%s", parsed.ObjectAttributes.SourceBranch)
|
if lastCommit == nil {
|
||||||
} else {
|
return nil, nil, fmt.Errorf("last_commit key expected in merge request hook")
|
||||||
build.Ref = fmt.Sprintf("refs/merge-requests/%d/head", parsed.ObjectAttributes.IId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
build.Branch = parsed.ObjectAttributes.SourceBranch
|
build.Message = lastCommit.Message
|
||||||
// build.Timestamp = parsed.ObjectAttributes.LastCommit.Timestamp
|
build.Commit = lastCommit.Id
|
||||||
|
//build.Remote = parsed.ObjectAttributes.Source.HttpUrl
|
||||||
|
|
||||||
|
if obj.SourceProjectId == obj.TargetProjectId {
|
||||||
|
build.Ref = fmt.Sprintf("refs/heads/%s", obj.SourceBranch)
|
||||||
|
} else {
|
||||||
|
build.Ref = fmt.Sprintf("refs/merge-requests/%d/head", obj.IId)
|
||||||
|
}
|
||||||
|
|
||||||
|
build.Branch = obj.SourceBranch
|
||||||
|
|
||||||
|
author := lastCommit.Author
|
||||||
|
if author == nil {
|
||||||
|
return nil, nil, fmt.Errorf("author key expected in merge request hook")
|
||||||
|
}
|
||||||
|
|
||||||
|
build.Author = author.Name
|
||||||
|
build.Email = author.Email
|
||||||
|
|
||||||
build.Author = parsed.ObjectAttributes.LastCommit.Author.Name
|
|
||||||
build.Email = parsed.ObjectAttributes.LastCommit.Author.Email
|
|
||||||
if len(build.Email) != 0 {
|
if len(build.Email) != 0 {
|
||||||
build.Avatar = GetUserAvatar(build.Email)
|
build.Avatar = GetUserAvatar(build.Email)
|
||||||
}
|
}
|
||||||
|
|
||||||
build.Title = parsed.ObjectAttributes.Title
|
build.Title = obj.Title
|
||||||
build.Link = parsed.ObjectAttributes.Url
|
build.Link = obj.Url
|
||||||
|
|
||||||
return repo, build, nil
|
return repo, build, nil
|
||||||
}
|
}
|
||||||
|
@ -189,6 +189,7 @@ func Test_Gitlab(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
g.Describe("Merge request hook", func() {
|
||||||
g.It("Should parse merge request hook", func() {
|
g.It("Should parse merge request hook", func() {
|
||||||
req, _ := http.NewRequest(
|
req, _ := http.NewRequest(
|
||||||
"POST",
|
"POST",
|
||||||
@ -198,6 +199,24 @@ func Test_Gitlab(t *testing.T) {
|
|||||||
|
|
||||||
repo, build, err := gitlab.Hook(req)
|
repo, build, err := gitlab.Hook(req)
|
||||||
|
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(repo.Avatar).Equal("http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg")
|
||||||
|
g.Assert(repo.Branch).Equal("develop")
|
||||||
|
g.Assert(repo.Owner).Equal("awesome_space")
|
||||||
|
g.Assert(repo.Name).Equal("awesome_project")
|
||||||
|
|
||||||
|
g.Assert(build.Title).Equal("MS-Viewport")
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should parse legacy merge request hook", func() {
|
||||||
|
req, _ := http.NewRequest(
|
||||||
|
"POST",
|
||||||
|
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
|
||||||
|
bytes.NewReader(testdata.LegacyMergeRequestHook),
|
||||||
|
)
|
||||||
|
|
||||||
|
repo, build, err := gitlab.Hook(req)
|
||||||
|
|
||||||
g.Assert(err == nil).IsTrue()
|
g.Assert(err == nil).IsTrue()
|
||||||
g.Assert(repo.Owner).Equal("diaspora")
|
g.Assert(repo.Owner).Equal("diaspora")
|
||||||
g.Assert(repo.Name).Equal("diaspora-client")
|
g.Assert(repo.Name).Equal("diaspora-client")
|
||||||
@ -206,4 +225,5 @@ func Test_Gitlab(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
80
remote/gitlab/testdata/hooks.go
vendored
80
remote/gitlab/testdata/hooks.go
vendored
@ -85,6 +85,86 @@ var LegacyTagHook = []byte(`
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
var MergeRequestHook = []byte(`
|
var MergeRequestHook = []byte(`
|
||||||
|
{
|
||||||
|
"object_kind": "merge_request",
|
||||||
|
"user": {
|
||||||
|
"name": "Administrator",
|
||||||
|
"username": "root",
|
||||||
|
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||||
|
},
|
||||||
|
"object_attributes": {
|
||||||
|
"id": 99,
|
||||||
|
"target_branch": "master",
|
||||||
|
"source_branch": "ms-viewport",
|
||||||
|
"source_project_id": 14,
|
||||||
|
"author_id": 51,
|
||||||
|
"assignee_id": 6,
|
||||||
|
"title": "MS-Viewport",
|
||||||
|
"created_at": "2013-12-03T17:23:34Z",
|
||||||
|
"updated_at": "2013-12-03T17:23:34Z",
|
||||||
|
"st_commits": null,
|
||||||
|
"st_diffs": null,
|
||||||
|
"milestone_id": null,
|
||||||
|
"state": "opened",
|
||||||
|
"merge_status": "unchecked",
|
||||||
|
"target_project_id": 14,
|
||||||
|
"iid": 1,
|
||||||
|
"description": "",
|
||||||
|
"source":{
|
||||||
|
"name":"Awesome Project",
|
||||||
|
"description":"Aut reprehenderit ut est.",
|
||||||
|
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||||
|
"avatar_url":"http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
|
||||||
|
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
|
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
|
"namespace":"Awesome Space",
|
||||||
|
"visibility_level":20,
|
||||||
|
"path_with_namespace":"awesome_space/awesome_project",
|
||||||
|
"default_branch":"master",
|
||||||
|
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||||
|
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
|
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
|
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||||
|
},
|
||||||
|
"target": {
|
||||||
|
"name":"Awesome Project",
|
||||||
|
"description":"Aut reprehenderit ut est.",
|
||||||
|
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||||
|
"avatar_url":"http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
|
||||||
|
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
|
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
|
"namespace":"Awesome Space",
|
||||||
|
"visibility_level":20,
|
||||||
|
"path_with_namespace":"awesome_space/awesome_project",
|
||||||
|
"default_branch":"develop",
|
||||||
|
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||||
|
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
|
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
|
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||||
|
},
|
||||||
|
"last_commit": {
|
||||||
|
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||||
|
"message": "fixed readme",
|
||||||
|
"timestamp": "2012-01-03T23:36:29+02:00",
|
||||||
|
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||||
|
"author": {
|
||||||
|
"name": "GitLab dev user",
|
||||||
|
"email": "gitlabdev@dv6700.(none)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"work_in_progress": false,
|
||||||
|
"url": "http://example.com/diaspora/merge_requests/1",
|
||||||
|
"action": "open",
|
||||||
|
"assignee": {
|
||||||
|
"name": "User1",
|
||||||
|
"username": "user1",
|
||||||
|
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
|
var LegacyMergeRequestHook = []byte(`
|
||||||
{
|
{
|
||||||
"object_kind": "merge_request",
|
"object_kind": "merge_request",
|
||||||
"user": {
|
"user": {
|
||||||
|
Loading…
Reference in New Issue
Block a user