woodpecker/server/hooks.go

172 lines
4 KiB
Go
Raw Normal View History

2015-04-08 22:43:59 +00:00
package server
import (
"fmt"
2015-04-08 22:43:59 +00:00
"strings"
log "github.com/Sirupsen/logrus"
2015-04-08 22:43:59 +00:00
"github.com/drone/drone/common"
"github.com/drone/drone/common/httputil"
2015-04-30 17:39:16 +00:00
"github.com/drone/drone/parser"
2015-04-21 22:48:48 +00:00
"github.com/drone/drone/parser/inject"
"github.com/drone/drone/parser/matrix"
2015-04-24 21:25:03 +00:00
"github.com/drone/drone/queue"
2015-04-08 22:43:59 +00:00
"github.com/gin-gonic/gin"
)
// PostHook accepts a post-commit hook and parses the payload
// in order to trigger a build.
//
// GET /api/hook
//
func PostHook(c *gin.Context) {
remote := ToRemote(c)
store := ToDatastore(c)
2015-04-24 21:25:03 +00:00
queue_ := ToQueue(c)
2015-04-30 21:23:46 +00:00
sess := ToSession(c)
2015-04-08 22:43:59 +00:00
hook, err := remote.Hook(c.Request)
if err != nil {
log.Errorf("failure to parse hook. %s", err)
2015-04-08 22:43:59 +00:00
c.Fail(400, err)
return
}
if hook == nil {
c.Writer.WriteHeader(200)
return
}
if hook.Repo == nil {
log.Errorf("failure to ascertain repo from hook.")
2015-04-08 22:43:59 +00:00
c.Writer.WriteHeader(400)
return
}
2015-04-30 21:23:46 +00:00
// get the token and verify the hook is authorized
token := sess.GetLogin(c.Request)
if token == nil || token.Label != hook.Repo.FullName {
log.Errorf("invalid token sent with hook.")
c.AbortWithStatus(403)
return
}
2015-04-08 22:43:59 +00:00
// a build may be skipped if the text [CI SKIP]
// is found inside the commit message
if hook.Commit != nil && strings.Contains(hook.Commit.Message, "[CI SKIP]") {
log.Infof("ignoring hook. [ci skip] found for %s")
2015-04-08 22:43:59 +00:00
c.Writer.WriteHeader(204)
return
}
repo, err := store.Repo(hook.Repo.FullName)
2015-04-08 22:43:59 +00:00
if err != nil {
log.Errorf("failure to find repo %s from hook. %s", hook.Repo.FullName, err)
2015-04-08 22:43:59 +00:00
c.Fail(404, err)
return
}
switch {
case repo.Disabled:
log.Infof("ignoring hook. repo %s is disabled.", repo.FullName)
c.Writer.WriteHeader(204)
return
case repo.User == nil:
log.Warnf("ignoring hook. repo %s has no owner.", repo.FullName)
c.Writer.WriteHeader(204)
return
case repo.DisablePR && hook.PullRequest != nil:
log.Warnf("ignoring hook. repo %s is disabled for pull requests.", repo.FullName)
2015-04-08 22:43:59 +00:00
c.Writer.WriteHeader(204)
return
}
user, err := store.User(repo.User.Login)
2015-04-08 22:43:59 +00:00
if err != nil {
log.Errorf("failure to find repo owner %s. %s", repo.User.Login, err)
2015-04-08 22:43:59 +00:00
c.Fail(500, err)
return
}
2015-04-21 22:48:48 +00:00
params, _ := store.RepoParams(repo.FullName)
2015-04-08 22:43:59 +00:00
build := &common.Build{}
build.State = common.StatePending
build.Commit = hook.Commit
build.PullRequest = hook.PullRequest
// featch the .drone.yml file from the database
raw, err := remote.Script(user, repo, build)
2015-04-08 22:43:59 +00:00
if err != nil {
log.Errorf("failure to get .drone.yml for %s. %s", repo.FullName, err)
2015-04-08 22:43:59 +00:00
c.Fail(404, err)
return
}
2015-04-21 22:48:48 +00:00
// inject any private parameters into the .drone.yml
if params != nil && len(params) != 0 {
raw = []byte(inject.InjectSafe(string(raw), params))
}
axes, err := matrix.Parse(string(raw))
if err != nil {
log.Errorf("failure to calculate matrix for %s. %s", repo.FullName, err)
c.Fail(404, err)
return
}
if len(axes) == 0 {
axes = append(axes, matrix.Axis{})
}
for num, axis := range axes {
build.Tasks = append(build.Tasks, &common.Task{
Number: num + 1,
State: common.StatePending,
Environment: axis,
})
}
2015-04-24 21:25:03 +00:00
keys, err := store.RepoKeypair(repo.FullName)
2015-04-08 22:43:59 +00:00
if err != nil {
2015-04-24 21:25:03 +00:00
log.Errorf("failure to fetch keypair for %s. %s", repo.FullName, err)
c.Fail(404, err)
2015-04-08 22:43:59 +00:00
return
}
2015-04-28 22:08:21 +00:00
netrc, err := remote.Netrc(user)
if err != nil {
c.Fail(500, err)
return
}
2015-04-08 22:43:59 +00:00
// verify the branches can be built vs skipped
2015-04-30 17:39:16 +00:00
when, _ := parser.ParseCondition(string(raw))
if build.Commit != nil && when != nil && !when.MatchBranch(build.Commit.Ref) {
log.Infof("ignoring hook. yaml file excludes repo and branch %s %s", repo.FullName, build.Commit.Ref)
c.AbortWithStatus(200)
return
}
2015-04-08 22:43:59 +00:00
2015-04-24 21:25:03 +00:00
err = store.SetBuild(repo.FullName, build)
if err != nil {
c.Fail(500, err)
return
}
c.JSON(200, build)
link := fmt.Sprintf(
"%s/%s/%d",
httputil.GetURL(c.Request),
repo.FullName,
build.Number,
)
err = remote.Status(user, repo, build, link)
if err != nil {
log.Errorf("error setting commit status for %s/%d", repo.FullName, build.Number)
}
2015-04-24 21:25:03 +00:00
queue_.Publish(&queue.Work{
User: user,
Repo: repo,
Build: build,
Keys: keys,
2015-04-28 22:08:21 +00:00
Netrc: netrc,
2015-04-24 21:25:03 +00:00
Yaml: raw,
})
2015-04-08 22:43:59 +00:00
}