From 9ded21d7922a9be473779b5916e4717df31f294a Mon Sep 17 00:00:00 2001 From: Kirilll Zaitsev Date: Sun, 30 Aug 2015 03:34:05 +0300 Subject: [PATCH] Some optimizations --- Godeps/Godeps.json | 2 +- .../examples/projects/main.go | 21 +------------- .../go-gitlab-client/hook_payload.go | 3 +- pkg/remote/builtin/gitlab/gitlab.go | 28 +++++-------------- pkg/remote/builtin/gitlab/gitlab_test.go | 2 +- 5 files changed, 12 insertions(+), 44 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 089d4c11..b2596e4e 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -12,7 +12,7 @@ }, { "ImportPath": "github.com/Bugagazavr/go-gitlab-client", - "Rev": "fa361f26087a2ff8fbb267fbe2d82037fc35e51a" + "Rev": "e5999f934dc45c41073fc57998a1224a75ff7d50" }, { "ImportPath": "github.com/BurntSushi/migration", diff --git a/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/examples/projects/main.go b/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/examples/projects/main.go index 80a3d478..4eb0b0a6 100644 --- a/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/examples/projects/main.go +++ b/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/examples/projects/main.go @@ -43,8 +43,7 @@ func main() { " > -m merge_requests -id PROJECT_ID\n"+ " > -m merge_request_notes -id PROJECT_ID -merge_id MERGE_REQUEST_ID\n"+ " > -m merge_request_comment -id PROJECT_ID -merge_id MERGE_REQUEST_ID -comment COMMENT_BODY\n"+ - " > -m team -id PROJECT_ID\n"+ - " > -m add_drone -id PROJECT_ID\n -token DRONE_TOKEN -url DRONE_URL") + " > -m team -id PROJECT_ID") var id string flag.StringVar(&id, "id", "", "Specify repository id") @@ -55,12 +54,6 @@ func main() { var comment string flag.StringVar(&comment, "comment", "", "The body of the new comment") - var drone_token string - flag.StringVar(&drone_token, "drone_token", "", "Drone service token") - - var drone_url string - flag.StringVar(&drone_url, "drone_url", "", "Drone service url") - flag.Usage = func() { fmt.Printf("Usage:\n") flag.PrintDefaults() @@ -249,17 +242,5 @@ func main() { for _, member := range members { fmt.Printf("> [%d] %s (%s) since %s\n", member.Id, member.Username, member.Name, member.CreatedAt) } - case "add_drone": - fmt.Println("Adding drone service to project") - - if id == "" || drone_token == "" || drone_url == "" { - flag.Usage() - return - } - - if err := gitlab.AddDroneService(id, map[string]string{"token": drone_token, "drone_url": drone_url}); err != nil { - fmt.Println(err) - return - } } } diff --git a/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/hook_payload.go b/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/hook_payload.go index e4c33c0a..b778e1fb 100644 --- a/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/hook_payload.go +++ b/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client/hook_payload.go @@ -27,7 +27,7 @@ type HookObjAttr struct { StDiffs string `json:"st_diffs,omitempty"` MergeStatus string `json:"merge_status,omitempty"` TargetProjectId int `json:"target_project_id,omitempty"` - URL string `json:"url,omitempty"` + Url string `json:"url,omiyempty"` Source *hProject `json:"source,omitempty"` Target *hProject `json:"target,omitempty"` LastCommit *hCommit `json:"last_commit,omitempty"` @@ -38,6 +38,7 @@ type hProject struct { SshUrl string `json:"ssh_url"` HttpUrl string `json:"http_url"` VisibilityLevel int `json:"visibility_level"` + WebUrl string `json:"web_url"` Namespace string `json:"namespace"` } diff --git a/pkg/remote/builtin/gitlab/gitlab.go b/pkg/remote/builtin/gitlab/gitlab.go index 7b67a53c..74c64485 100644 --- a/pkg/remote/builtin/gitlab/gitlab.go +++ b/pkg/remote/builtin/gitlab/gitlab.go @@ -6,10 +6,8 @@ import ( "io/ioutil" "net/http" "net/url" - "regexp" "strconv" "strings" - "time" "github.com/drone/drone/Godeps/_workspace/src/github.com/Bugagazavr/go-gitlab-client" "github.com/drone/drone/Godeps/_workspace/src/github.com/hashicorp/golang-lru" @@ -232,17 +230,8 @@ func (r *Gitlab) Hook(req *http.Request) (*common.Hook, error) { switch parsed.ObjectKind { case "merge_request": - if parsed.ObjectAttributes.State != "reopened" && parsed.ObjectAttributes.MergeStatus != "unchecked" || - parsed.ObjectAttributes.State != "opened" && parsed.ObjectAttributes.MergeStatus != "unchecked" { - return nil, nil - } - return mergeRequest(parsed, req) case "tag_push", "push": - if len(parsed.After) == 0 || parsed.TotalCommitsCount == 0 { - return nil, nil - } - return push(parsed, req) default: return nil, nil @@ -252,13 +241,11 @@ func (r *Gitlab) Hook(req *http.Request) (*common.Hook, error) { func mergeRequest(parsed *gogitlab.HookPayload, req *http.Request) (*common.Hook, error) { var hook = new(common.Hook) - re := regexp.MustCompile(".git$") - hook.Repo = &common.Repo{} hook.Repo.Owner = req.FormValue("owner") hook.Repo.Name = req.FormValue("name") hook.Repo.FullName = fmt.Sprintf("%s/%s", hook.Repo.Owner, hook.Repo.Name) - hook.Repo.Link = re.ReplaceAllString(parsed.ObjectAttributes.Target.HttpUrl, "$1") + hook.Repo.Link = parsed.ObjectAttributes.Target.WebUrl hook.Repo.Clone = parsed.ObjectAttributes.Target.HttpUrl hook.Repo.Branch = "master" @@ -267,24 +254,23 @@ func mergeRequest(parsed *gogitlab.HookPayload, req *http.Request) (*common.Hook hook.Commit.Sha = parsed.ObjectAttributes.LastCommit.Id hook.Commit.Remote = parsed.ObjectAttributes.Source.HttpUrl - if parsed.ObjectAttributes.Source.HttpUrl == parsed.ObjectAttributes.Target.HttpUrl { + if parsed.ObjectAttributes.SourceProjectId == parsed.ObjectAttributes.TargetProjectId { hook.Commit.Ref = fmt.Sprintf("refs/heads/%s", parsed.ObjectAttributes.SourceBranch) - hook.Commit.Branch = parsed.ObjectAttributes.SourceBranch - hook.Commit.Timestamp = time.Now().UTC().Format("2006-01-02 15:04:05.000000000 +0000 MST") } else { hook.Commit.Ref = fmt.Sprintf("refs/merge-requests/%d/head", parsed.ObjectAttributes.IId) - hook.Commit.Branch = parsed.ObjectAttributes.SourceBranch - hook.Commit.Timestamp = time.Now().UTC().Format("2006-01-02 15:04:05.000000000 +0000 MST") } + hook.Commit.Branch = parsed.ObjectAttributes.SourceBranch + hook.Commit.Timestamp = parsed.ObjectAttributes.LastCommit.Timestamp + hook.Commit.Author = &common.Author{} hook.Commit.Author.Login = parsed.ObjectAttributes.LastCommit.Author.Name hook.Commit.Author.Email = parsed.ObjectAttributes.LastCommit.Author.Email hook.PullRequest = &common.PullRequest{} hook.PullRequest.Number = parsed.ObjectAttributes.IId - hook.PullRequest.Title = parsed.ObjectAttributes.Description - hook.PullRequest.Link = parsed.ObjectAttributes.URL + hook.PullRequest.Title = parsed.ObjectAttributes.Title + hook.PullRequest.Link = parsed.ObjectAttributes.Url return hook, nil } diff --git a/pkg/remote/builtin/gitlab/gitlab_test.go b/pkg/remote/builtin/gitlab/gitlab_test.go index 1421f711..e694ab93 100644 --- a/pkg/remote/builtin/gitlab/gitlab_test.go +++ b/pkg/remote/builtin/gitlab/gitlab_test.go @@ -160,7 +160,7 @@ func Test_Gitlab(t *testing.T) { g.Assert(hook.Repo.Name).Equal("diaspora-client") g.Assert(hook.PullRequest.Number).Equal(1) - g.Assert(hook.PullRequest.Title).Equal("") + g.Assert(hook.PullRequest.Title).Equal("MS-Viewport") }) }) })