Some optimizations

This commit is contained in:
Kirilll Zaitsev 2015-08-30 03:34:05 +03:00
parent 6854d1343e
commit 9ded21d792
5 changed files with 12 additions and 44 deletions

2
Godeps/Godeps.json generated
View file

@ -12,7 +12,7 @@
},
{
"ImportPath": "github.com/Bugagazavr/go-gitlab-client",
"Rev": "fa361f26087a2ff8fbb267fbe2d82037fc35e51a"
"Rev": "e5999f934dc45c41073fc57998a1224a75ff7d50"
},
{
"ImportPath": "github.com/BurntSushi/migration",

View file

@ -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
}
}
}

View file

@ -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"`
}

View file

@ -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
}

View file

@ -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")
})
})
})