added logging

This commit is contained in:
Brad Rydzewski 2017-03-18 23:20:09 +08:00
parent bd25f57fa7
commit 0795dce4ed
4 changed files with 34 additions and 19 deletions

View file

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

View file

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

View file

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

View file

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