2015-04-09 00:43:59 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2015-05-22 20:37:40 +02:00
|
|
|
log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
2015-09-02 05:42:18 +02:00
|
|
|
"github.com/drone/drone/pkg/hash"
|
2015-05-17 20:42:56 +02:00
|
|
|
"github.com/drone/drone/pkg/queue"
|
2015-05-17 22:51:42 +02:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-09-03 01:14:25 +02:00
|
|
|
"github.com/drone/drone/pkg/utils/httputil"
|
2015-05-17 20:36:12 +02:00
|
|
|
"github.com/drone/drone/pkg/yaml"
|
|
|
|
"github.com/drone/drone/pkg/yaml/matrix"
|
2015-04-09 00:43:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 23:25:03 +02:00
|
|
|
queue_ := ToQueue(c)
|
2015-06-08 02:04:57 +02:00
|
|
|
conf := ToSettings(c)
|
2015-04-09 00:43:59 +02:00
|
|
|
|
|
|
|
hook, err := remote.Hook(c.Request)
|
|
|
|
if err != nil {
|
2015-04-12 00:46:30 +02:00
|
|
|
log.Errorf("failure to parse hook. %s", err)
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if hook == nil {
|
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if hook.Repo == nil {
|
2015-04-12 00:46:30 +02:00
|
|
|
log.Errorf("failure to ascertain repo from hook.")
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Writer.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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]") {
|
2015-04-12 00:46:30 +02:00
|
|
|
log.Infof("ignoring hook. [ci skip] found for %s")
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:45:31 +02:00
|
|
|
repo, err := store.RepoName(hook.Repo.Owner, hook.Repo.Name)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-05-11 09:45:31 +02:00
|
|
|
log.Errorf("failure to find repo %s/%s from hook. %s", hook.Repo.Owner, hook.Repo.Name, err)
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-11 10:36:07 +02:00
|
|
|
// get the token and verify the hook is authorized
|
2015-09-02 05:42:18 +02:00
|
|
|
if c.Request.FormValue("access_token") != hash.New(repo.FullName, repo.Hash) {
|
2015-08-11 10:36:07 +02:00
|
|
|
log.Errorf("invalid token sent with hook.")
|
|
|
|
c.AbortWithStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:46:30 +02:00
|
|
|
switch {
|
2015-05-11 09:45:31 +02:00
|
|
|
case repo.UserID == 0:
|
|
|
|
log.Warnf("ignoring hook. repo %s has no owner.", repo.FullName)
|
2015-04-12 00:46:30 +02:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
2015-09-01 11:44:30 +02:00
|
|
|
case !repo.Hooks.Push && hook.Commit != nil:
|
2015-05-11 09:45:31 +02:00
|
|
|
log.Infof("ignoring hook. repo %s is disabled.", repo.FullName)
|
2015-04-12 00:46:30 +02:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
2015-09-01 11:44:30 +02:00
|
|
|
case !repo.Hooks.PullRequest && hook.PullRequest != nil:
|
2015-04-12 00:46:30 +02:00
|
|
|
log.Warnf("ignoring hook. repo %s is disabled for pull requests.", repo.FullName)
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:45:31 +02:00
|
|
|
user, err := store.User(repo.UserID)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-05-11 09:45:31 +02:00
|
|
|
log.Errorf("failure to find repo owner %s. %s", repo.FullName, err)
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:45:08 +02:00
|
|
|
build := &common.Build{}
|
|
|
|
build.Commit = hook.Commit
|
|
|
|
build.PullRequest = hook.PullRequest
|
|
|
|
build.Status = common.StatePending
|
|
|
|
build.RepoID = repo.ID
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-07-13 21:26:36 +02:00
|
|
|
// fetch the .drone.yml file from the database
|
2015-09-07 21:13:27 +02:00
|
|
|
raw, sec, err := remote.Script(user, repo, build)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-04-12 00:46:30 +02:00
|
|
|
log.Errorf("failure to get .drone.yml for %s. %s", repo.FullName, err)
|
2015-04-09 00:43:59 +02:00
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-09-07 21:13:27 +02:00
|
|
|
|
2015-04-16 09:24:53 +02:00
|
|
|
axes, err := matrix.Parse(string(raw))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failure to calculate matrix for %s. %s", repo.FullName, err)
|
2015-08-20 18:39:34 +02:00
|
|
|
c.Fail(400, err)
|
2015-04-16 09:24:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(axes) == 0 {
|
|
|
|
axes = append(axes, matrix.Axis{})
|
|
|
|
}
|
|
|
|
for num, axis := range axes {
|
2015-06-23 05:45:08 +02:00
|
|
|
build.Jobs = append(build.Jobs, &common.Job{
|
|
|
|
BuildID: build.ID,
|
2015-06-19 02:36:52 +02:00
|
|
|
Number: num + 1,
|
|
|
|
Status: common.StatePending,
|
2015-04-16 09:24:53 +02:00
|
|
|
Environment: axis,
|
|
|
|
})
|
|
|
|
}
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-02 05:42:18 +02:00
|
|
|
netrc, err := remote.Netrc(user, repo)
|
2015-04-29 00:08:21 +02:00
|
|
|
if err != nil {
|
2015-05-11 09:45:31 +02:00
|
|
|
log.Errorf("failure to generate netrc for %s. %s", repo.FullName, err)
|
2015-04-29 00:08:21 +02:00
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:43:59 +02:00
|
|
|
// verify the branches can be built vs skipped
|
2015-04-30 19:39:16 +02:00
|
|
|
when, _ := parser.ParseCondition(string(raw))
|
2015-06-23 05:45:08 +02:00
|
|
|
if build.PullRequest != nil && when != nil && !when.MatchBranch(build.Commit.Branch) {
|
|
|
|
log.Infof("ignoring hook. yaml file excludes repo and branch %s %s", repo.FullName, build.Commit.Branch)
|
2015-04-30 19:39:16 +02:00
|
|
|
c.AbortWithStatus(200)
|
|
|
|
return
|
|
|
|
}
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-06-23 05:45:08 +02:00
|
|
|
err = store.AddBuild(build)
|
2015-04-24 23:25:03 +02:00
|
|
|
if err != nil {
|
2015-05-11 09:45:31 +02:00
|
|
|
log.Errorf("failure to save commit for %s. %s", repo.FullName, err)
|
2015-04-24 23:25:03 +02:00
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:45:08 +02:00
|
|
|
c.JSON(200, build)
|
2015-05-06 05:59:07 +02:00
|
|
|
|
2015-06-23 05:45:08 +02:00
|
|
|
err = remote.Status(user, repo, build)
|
2015-05-10 05:14:01 +02:00
|
|
|
if err != nil {
|
2015-06-23 05:45:08 +02:00
|
|
|
log.Errorf("error setting commit status for %s/%d", repo.FullName, build.Number)
|
2015-05-10 05:14:01 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:25:03 +02:00
|
|
|
queue_.Publish(&queue.Work{
|
2015-09-07 21:13:27 +02:00
|
|
|
User: user,
|
|
|
|
Repo: repo,
|
|
|
|
Build: build,
|
|
|
|
Keys: repo.Keys,
|
|
|
|
Netrc: netrc,
|
|
|
|
Config: raw,
|
|
|
|
Secret: sec,
|
2015-09-03 01:14:25 +02:00
|
|
|
System: &common.System{
|
|
|
|
Link: httputil.GetURL(c.Request),
|
|
|
|
Plugins: conf.Plugins,
|
|
|
|
Globals: conf.Environment,
|
|
|
|
},
|
2015-04-24 23:25:03 +02:00
|
|
|
})
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|