Merge pull request #1504 from Bugagazavr/fetch-data-from-hooks

Added support for new attributes for GitLab
This commit is contained in:
Brad Rydzewski 2016-03-03 12:36:58 -08:00
commit f19841c2d2
5 changed files with 415 additions and 76 deletions

View file

@ -64,12 +64,17 @@ type Person struct {
}
type hProject struct {
Name string `json:"name"`
SshUrl string `json:"ssh_url"`
HttpUrl string `json:"http_url"`
VisibilityLevel int `json:"visibility_level"`
WebUrl string `json:"web_url"`
Namespace string `json:"namespace"`
Name string `json:"name"`
SshUrl string `json:"ssh_url"`
HttpUrl string `json:"http_url"`
GitSshUrl string `json:"git_ssh_url"`
GitHttpUrl string `json:"git_http_url"`
AvatarUrl string `json:"avatar_url"`
VisibilityLevel int `json:"visibility_level"`
WebUrl string `json:"web_url"`
PathWithNamespace string `json:"path_with_namespace"`
DefaultBranch string `json:"default_branch"`
Namespace string `json:"namespace"`
}
type hRepository struct {
@ -124,6 +129,7 @@ type HookPayload struct {
UserId int `json:"user_id,omitempty"`
UserName string `json:"user_name,omitempty"`
ProjectId int `json:"project_id,omitempty"`
Project *hProject `json:"project,omitempty"`
Repository *hRepository `json:"repository,omitempty"`
Commits []hCommit `json:"commits,omitempty"`
TotalCommitsCount int `json:"total_commits_count,omitempty"`

View file

@ -372,62 +372,136 @@ 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) {
repo := &model.Repo{}
repo.Owner = req.FormValue("owner")
repo.Name = req.FormValue("name")
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
repo.Link = parsed.ObjectAttributes.Target.WebUrl
repo.Clone = parsed.ObjectAttributes.Target.HttpUrl
repo.Branch = "master"
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.Name = req.FormValue("name")
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
}
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"
}
if target.AvatarUrl != "" {
repo.Avatar = target.AvatarUrl
}
build := &model.Build{}
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 {
build.Ref = fmt.Sprintf("refs/heads/%s", parsed.ObjectAttributes.SourceBranch)
} else {
build.Ref = fmt.Sprintf("refs/merge-requests/%d/head", parsed.ObjectAttributes.IId)
lastCommit := obj.LastCommit
if lastCommit == nil {
return nil, nil, fmt.Errorf("last_commit key expected in merge request hook")
}
build.Branch = parsed.ObjectAttributes.SourceBranch
// build.Timestamp = parsed.ObjectAttributes.LastCommit.Timestamp
build.Message = lastCommit.Message
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 {
build.Avatar = GetUserAvatar(build.Email)
}
build.Title = parsed.ObjectAttributes.Title
build.Link = parsed.ObjectAttributes.Url
build.Title = obj.Title
build.Link = obj.Url
return repo, build, nil
}
func push(parsed *client.HookPayload, req *http.Request) (*model.Repo, *model.Build, error) {
var cloneUrl = parsed.Repository.GitHttpUrl
repo := &model.Repo{}
repo.Owner = req.FormValue("owner")
repo.Name = req.FormValue("name")
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
repo.Link = parsed.Repository.URL
repo.Clone = cloneUrl
repo.Branch = "master"
switch parsed.Repository.VisibilityLevel {
case 0:
repo.IsPrivate = true
case 10:
repo.IsPrivate = true
case 20:
repo.IsPrivate = false
// Since gitlab 8.5, used project instead repository key
// see https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/web_hooks/web_hooks.md#web-hooks
if project := parsed.Project; project != nil {
var err error
if repo.Owner, repo.Name, err = ExtractFromPath(project.PathWithNamespace); err != nil {
return nil, nil, err
}
repo.Avatar = project.AvatarUrl
repo.Link = project.WebUrl
repo.Clone = project.GitHttpUrl
repo.FullName = project.PathWithNamespace
repo.Branch = project.DefaultBranch
switch project.VisibilityLevel {
case 0:
repo.IsPrivate = true
case 10:
repo.IsPrivate = true
case 20:
repo.IsPrivate = false
}
} else if repository := parsed.Repository; repository != nil {
repo.Owner = req.FormValue("owner")
repo.Name = req.FormValue("name")
repo.Link = repository.URL
repo.Clone = repository.GitHttpUrl
repo.Branch = "master"
repo.FullName = fmt.Sprintf("%s/%s", req.FormValue("owner"), req.FormValue("name"))
switch repository.VisibilityLevel {
case 0:
repo.IsPrivate = true
case 10:
repo.IsPrivate = true
case 20:
repo.IsPrivate = false
}
} else {
return nil, nil, fmt.Errorf("No project/repository keys given")
}
repo.FullName = fmt.Sprintf("%s/%s", req.FormValue("owner"), req.FormValue("name"))
build := &model.Build{}
build.Event = model.EventPush
build.Commit = parsed.After

View file

@ -134,52 +134,114 @@ func Test_Gitlab(t *testing.T) {
// Test hook method
g.Describe("Hook", func() {
g.It("Should parse push hoook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.PushHook),
)
g.Describe("Push hook", func() {
g.It("Should parse actual push hoook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.PushHook),
)
repo, build, err := gitlab.Hook(req)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(build.Ref).Equal("refs/heads/master")
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("mike")
g.Assert(repo.Name).Equal("diaspora")
g.Assert(repo.Avatar).Equal("http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg")
g.Assert(repo.Branch).Equal("develop")
g.Assert(build.Ref).Equal("refs/heads/master")
})
g.It("Should parse legacy push hoook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.LegacyPushHook),
)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(repo.Avatar).Equal("")
g.Assert(repo.Branch).Equal("master")
g.Assert(build.Ref).Equal("refs/heads/master")
})
})
g.It("Should parse tag push hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.TagHook),
)
g.Describe("Tag push hook", func() {
g.It("Should parse tag push hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.TagHook),
)
repo, build, err := gitlab.Hook(req)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(build.Ref).Equal("refs/tags/v1.0.0")
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("jsmith")
g.Assert(repo.Name).Equal("example")
g.Assert(repo.Avatar).Equal("http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg")
g.Assert(repo.Branch).Equal("develop")
g.Assert(build.Ref).Equal("refs/tags/v1.0.0")
})
g.It("Should parse legacy tag push hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.LegacyTagHook),
)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(build.Ref).Equal("refs/tags/v1.0.0")
})
})
g.It("Should parse merge request hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.MergeRequestHook),
)
g.Describe("Merge request hook", func() {
g.It("Should parse merge request hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.MergeRequestHook),
)
repo, build, err := gitlab.Hook(req)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
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.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(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(build.Title).Equal("MS-Viewport")
})
})
})
})

View file

@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/drone/drone/remote/gitlab/client"
)
@ -97,6 +98,14 @@ func GetUserAvatar(email string) string {
)
}
func ExtractFromPath(str string) (string, string, error) {
s := strings.Split(str, "/")
if len(s) < 2 {
return "", "", fmt.Errorf("Minimum match not found")
}
return s[0], s[1], nil
}
func GetUserEmail(c *client.Client, defaultURL string) (*client.Client, error) {
return c, nil
}

View file

@ -1,6 +1,46 @@
package testdata
var TagHook = []byte(`
{
"object_kind": "tag_push",
"ref": "refs/tags/v1.0.0",
"before": "0000000000000000000000000000000000000000",
"after": "82b3d5ae55f7080f1e6022629cdb57bfae7cccc7",
"user_id": 1,
"user_name": "John Smith",
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s=80",
"project_id": 1,
"project":{
"name":"Example",
"description":"",
"web_url":"http://example.com/jsmith/example",
"avatar_url":"http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
"git_ssh_url":"git@example.com:jsmith/example.git",
"git_http_url":"http://example.com/jsmith/example.git",
"namespace":"Jsmith",
"visibility_level":0,
"path_with_namespace":"jsmith/example",
"default_branch":"develop",
"homepage":"http://example.com/jsmith/example",
"url":"git@example.com:jsmith/example.git",
"ssh_url":"git@example.com:jsmith/example.git",
"http_url":"http://example.com/jsmith/example.git"
},
"repository":{
"name": "jsmith",
"url": "ssh://git@example.com/jsmith/example.git",
"description": "",
"homepage": "http://example.com/jsmith/example",
"git_http_url":"http://example.com/jsmith/example.git",
"git_ssh_url":"git@example.com:jsmith/example.git",
"visibility_level":0
},
"commits": [],
"total_commits_count": 0
}
`)
var LegacyTagHook = []byte(`
{
"object_kind": "tag_push",
"ref": "refs/tags/v1.0.0",
@ -45,6 +85,86 @@ var TagHook = []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",
"user": {
@ -101,6 +221,74 @@ var MergeRequestHook = []byte(`
`)
var PushHook = []byte(`
{
"object_kind": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"ref": "refs/heads/master",
"user_id": 4,
"user_name": "John Smith",
"user_email": "john@example.com",
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
"project_id": 15,
"project":{
"name":"Diaspora",
"description":"",
"web_url":"http://example.com/mike/diaspora",
"avatar_url":"http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
"git_ssh_url":"git@example.com:mike/diaspora.git",
"git_http_url":"http://example.com/mike/diaspora.git",
"namespace":"Mike",
"visibility_level":0,
"path_with_namespace":"mike/diaspora",
"default_branch":"develop",
"homepage":"http://example.com/mike/diaspora",
"url":"git@example.com:mike/diasporadiaspora.git",
"ssh_url":"git@example.com:mike/diaspora.git",
"http_url":"http://example.com/mike/diaspora.git"
},
"repository":{
"name": "Diaspora",
"url": "git@example.com:mike/diasporadiaspora.git",
"description": "",
"homepage": "http://example.com/mike/diaspora",
"git_http_url":"http://example.com/mike/diaspora.git",
"git_ssh_url":"git@example.com:mike/diaspora.git",
"visibility_level":0
},
"commits": [
{
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"message": "Update Catalan translation to e38cb41.",
"timestamp": "2011-12-12T14:27:31+02:00",
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"author": {
"name": "Jordi Mallach",
"email": "jordi@softcatala.org"
},
"added": ["CHANGELOG"],
"modified": ["app/controller/application.rb"],
"removed": []
},
{
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29+02:00",
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)"
},
"added": ["CHANGELOG"],
"modified": ["app/controller/application.rb"],
"removed": []
}
],
"total_commits_count": 4
}
`)
var LegacyPushHook = []byte(`
{
"object_kind": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",