From 0795dce4edc5c02232c5a7a7fe419ba459d88498 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 18 Mar 2017 23:20:09 +0800 Subject: [PATCH] added logging --- remote/gogs/gogs.go | 14 ++++++++++---- remote/gogs/helper.go | 6 +++--- remote/gogs/types.go | 22 ++++++++++++---------- server/hook.go | 11 +++++++++-- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/remote/gogs/gogs.go b/remote/gogs/gogs.go index aa5b922e6..85292ac9a 100644 --- a/remote/gogs/gogs.go +++ b/remote/gogs/gogs.go @@ -175,10 +175,16 @@ func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) { // File fetches the file from the Gogs repository and returns its contents. func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) { client := c.newClientToken(u.Token) - buildRef := b.Commit - if buildRef == "" { + ref := b.Commit + + // TODO gogs does not yet return a sha with the pull request + // so unfortunately we need to use the pull request branch. + if b.Event == model.EventPull { + ref = b.Branch + } + if ref == "" { // Remove refs/tags or refs/heads, Gogs needs a short ref - buildRef = strings.TrimPrefix( + ref = strings.TrimPrefix( strings.TrimPrefix( b.Ref, "refs/heads/", @@ -186,7 +192,7 @@ func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ "refs/tags/", ) } - cfg, err := client.GetFile(r.Owner, r.Name, buildRef, f) + cfg, err := client.GetFile(r.Owner, r.Name, ref, f) return cfg, err } diff --git a/remote/gogs/helper.go b/remote/gogs/helper.go index 7e0ef1db8..1570c23b6 100644 --- a/remote/gogs/helper.go +++ b/remote/gogs/helper.go @@ -137,15 +137,15 @@ func buildFromPullRequest(hook *pullRequestHook) *model.Build { Commit: hook.PullRequest.Head.Sha, Link: hook.PullRequest.URL, Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), - Branch: hook.PullRequest.Base.Ref, + Branch: hook.PullRequest.BaseBranch, Message: hook.PullRequest.Title, Author: hook.PullRequest.User.Username, Avatar: avatar, Sender: sender, Title: hook.PullRequest.Title, Refspec: fmt.Sprintf("%s:%s", - hook.PullRequest.Head.Ref, - hook.PullRequest.Base.Ref, + hook.PullRequest.HeadBranch, + hook.PullRequest.BaseBranch, ), } return build diff --git a/remote/gogs/types.go b/remote/gogs/types.go index 1bf33c624..611004455 100644 --- a/remote/gogs/types.go +++ b/remote/gogs/types.go @@ -53,15 +53,16 @@ type pullRequestHook struct { Email string `json:"email"` Avatar string `json:"avatar_url"` } `json:"user"` - Title string `json:"title"` - Body string `json:"body"` - Labels []string `json:"labels"` - State string `json:"state"` - URL string `json:"html_url"` - Mergeable bool `json:"mergeable"` - Merged bool `json:"merged"` - MergeBase string `json:"merge_base"` - Base struct { + Title string `json:"title"` + Body string `json:"body"` + Labels []string `json:"labels"` + State string `json:"state"` + URL string `json:"html_url"` + Mergeable bool `json:"mergeable"` + Merged bool `json:"merged"` + MergeBase string `json:"merge_base"` + BaseBranch string `json:"base_branch"` + Base struct { Label string `json:"label"` Ref string `json:"ref"` Sha string `json:"sha"` @@ -80,7 +81,8 @@ type pullRequestHook struct { } `json:"owner"` } `json:"repo"` } `json:"base"` - Head struct { + HeadBranch string `json:"head_branch"` + Head struct { Label string `json:"label"` Ref string `json:"ref"` Sha string `json:"sha"` diff --git a/server/hook.go b/server/hook.go index e0d01c2a8..0a30dfe17 100644 --- a/server/hook.go +++ b/server/hook.go @@ -159,11 +159,13 @@ func PostHook(c *gin.Context) { // secrets have skip-verify flag if build.Event == model.EventPull { - old, ferr := remote_.FileRef(user, repo, build.Ref, cfg.Yaml) + old, ferr := remote_.FileRef(user, repo, build.Branch, cfg.Yaml) if ferr != nil { build.Status = model.StatusBlocked + logrus.Debugf("cannot fetch base yaml: status: blocked") } else if bytes.Equal(old, raw) { build.Status = model.StatusPending + logrus.Debugf("base yaml matches head yaml: status: accepted") } else { // this block is executed if the target yaml file // does not match the base yaml. @@ -174,6 +176,7 @@ func PostHook(c *gin.Context) { sender, uerr := store.GetUserLogin(c, build.Sender) if uerr != nil { build.Status = model.StatusBlocked + logrus.Debugf("sender does not have a drone account: status: blocked") } else { if refresher, ok := remote_.(remote.Refresher); ok { ok, _ := refresher.Refresh(sender) @@ -184,8 +187,12 @@ func PostHook(c *gin.Context) { // if the sender does not have push access to the // repository the pull request should be blocked. perm, perr := remote_.Perm(sender, repo.Owner, repo.Name) - if perr != nil || perm.Push == false { + if perr == nil && perm.Push == true { + build.Status = model.StatusPending + logrus.Debugf("sender %s has push access: status: accepted", sender.Login) + } else { build.Status = model.StatusBlocked + logrus.Debugf("sender %s does not have push access: status: blocked", sender.Login) } } }