2015-09-30 01:21:17 +00:00
|
|
|
package controller
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
2015-09-09 01:55:03 +00:00
|
|
|
"os"
|
2015-09-30 01:21:17 +00:00
|
|
|
"path/filepath"
|
2015-04-08 22:43:59 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/drone/drone/engine"
|
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/drone/drone/router/middleware/context"
|
|
|
|
"github.com/drone/drone/shared/httputil"
|
|
|
|
"github.com/drone/drone/shared/token"
|
|
|
|
"github.com/drone/drone/yaml"
|
|
|
|
"github.com/drone/drone/yaml/matrix"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func PostHook(c *gin.Context) {
|
2015-09-30 01:21:17 +00:00
|
|
|
remote := context.Remote(c)
|
|
|
|
db := context.Database(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
tmprepo, build, err := remote.Hook(c.Request)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-04-11 22:46:30 +00:00
|
|
|
log.Errorf("failure to parse hook. %s", err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(400, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
if build == nil {
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
if tmprepo == nil {
|
2015-04-11 22:46:30 +00:00
|
|
|
log.Errorf("failure to ascertain repo from hook.")
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// a build may be skipped if the text [CI SKIP]
|
|
|
|
// is found inside the commit message
|
2015-09-30 01:21:17 +00:00
|
|
|
if strings.Contains(build.Message, "[CI SKIP]") {
|
2015-04-11 22:46:30 +00:00
|
|
|
log.Infof("ignoring hook. [ci skip] found for %s")
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
repo, err := model.GetRepoName(db, tmprepo.Owner, tmprepo.Name)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
log.Errorf("failure to find repo %s/%s from hook. %s", tmprepo.Owner, tmprepo.Name, err)
|
|
|
|
c.AbortWithError(404, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-11 08:36:07 +00:00
|
|
|
// get the token and verify the hook is authorized
|
2015-09-09 21:05:52 +00:00
|
|
|
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
|
|
|
|
return repo.Hash, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failure to parse token from hook for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(400, err)
|
2015-09-09 21:05:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if parsed.Text != repo.FullName {
|
|
|
|
log.Errorf("failure to verify token from hook. Expected %s, got %s", repo.FullName, parsed.Text)
|
2015-08-11 08:36:07 +00:00
|
|
|
c.AbortWithStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
if repo.UserID == 0 {
|
2015-05-11 07:45:31 +00:00
|
|
|
log.Warnf("ignoring hook. repo %s has no owner.", repo.FullName)
|
2015-04-11 22:46:30 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
var skipped = true
|
|
|
|
if (build.Event == model.EventPush && repo.AllowPush) ||
|
|
|
|
(build.Event == model.EventPull && repo.AllowPull) ||
|
|
|
|
(build.Event == model.EventDeploy && repo.AllowDeploy) ||
|
|
|
|
(build.Event == model.EventTag && repo.AllowTag) {
|
|
|
|
skipped = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if skipped {
|
|
|
|
log.Infof("ignoring hook. repo %s is disabled for %s events.", repo.FullName, build.Event)
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
user, err := model.GetUser(db, repo.UserID)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-05-11 07:45:31 +00:00
|
|
|
log.Errorf("failure to find repo owner %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(500, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:26:36 +00:00
|
|
|
// fetch the .drone.yml file from the database
|
2015-09-07 19:13:27 +00:00
|
|
|
raw, sec, err := remote.Script(user, repo, build)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-04-11 22:46:30 +00:00
|
|
|
log.Errorf("failure to get .drone.yml for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(404, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-07 19:13:27 +00:00
|
|
|
|
2015-04-16 07:24:53 +00:00
|
|
|
axes, err := matrix.Parse(string(raw))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failure to calculate matrix for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(400, err)
|
2015-04-16 07:24:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(axes) == 0 {
|
|
|
|
axes = append(axes, matrix.Axis{})
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-02 03:42:18 +00:00
|
|
|
netrc, err := remote.Netrc(user, repo)
|
2015-04-28 22:08:21 +00:00
|
|
|
if err != nil {
|
2015-05-11 07:45:31 +00:00
|
|
|
log.Errorf("failure to generate netrc for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(500, err)
|
2015-04-28 22:08:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
key, _ := model.GetKey(db, repo)
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
// verify the branches can be built vs skipped
|
2015-09-30 01:21:17 +00:00
|
|
|
yconfig, _ := yaml.Parse(string(raw))
|
|
|
|
var match = false
|
|
|
|
for _, branch := range yconfig.Branches {
|
|
|
|
if branch == build.Branch {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
match, _ = filepath.Match(branch, build.Branch)
|
|
|
|
if match {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match && len(yconfig.Branches) != 0 {
|
|
|
|
log.Infof("ignoring hook. yaml file excludes repo and branch %s %s", repo.FullName, build.Branch)
|
2015-04-30 17:39:16 +00:00
|
|
|
c.AbortWithStatus(200)
|
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failure to begin database transaction", err)
|
|
|
|
c.AbortWithError(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// update some build fields
|
|
|
|
build.Status = model.StatusPending
|
|
|
|
build.RepoID = repo.ID
|
|
|
|
|
|
|
|
var jobs []*model.Job
|
|
|
|
for num, axis := range axes {
|
|
|
|
jobs = append(jobs, &model.Job{
|
|
|
|
BuildID: build.ID,
|
|
|
|
Number: num + 1,
|
|
|
|
Status: model.StatusPending,
|
|
|
|
Environment: axis,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
err = model.CreateBuild(tx, build, jobs...)
|
2015-04-24 21:25:03 +00:00
|
|
|
if err != nil {
|
2015-05-11 07:45:31 +00:00
|
|
|
log.Errorf("failure to save commit for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(500, err)
|
2015-04-24 21:25:03 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
tx.Commit()
|
2015-04-24 21:25:03 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
c.JSON(200, build)
|
2015-05-06 03:59:07 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
url := fmt.Sprintf("%s/%s/%d", httputil.GetURL(c.Request), repo.FullName, build.Number)
|
|
|
|
err = remote.Status(user, repo, build, url)
|
2015-05-10 03:14:01 +00:00
|
|
|
if err != nil {
|
2015-06-23 03:45:08 +00:00
|
|
|
log.Errorf("error setting commit status for %s/%d", repo.FullName, build.Number)
|
2015-05-10 03:14:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 17:39:18 +00:00
|
|
|
// get the previous build so taht we can send
|
|
|
|
// on status change notifications
|
2015-09-30 01:21:17 +00:00
|
|
|
last, _ := model.GetBuildLast(db, repo, build.Branch)
|
2015-09-10 17:39:18 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
engine_ := context.Engine(c)
|
|
|
|
go engine_.Schedule(&engine.Task{
|
2015-09-10 17:39:18 +00:00
|
|
|
User: user,
|
|
|
|
Repo: repo,
|
|
|
|
Build: build,
|
|
|
|
BuildPrev: last,
|
2015-09-30 01:21:17 +00:00
|
|
|
Jobs: jobs,
|
|
|
|
Keys: key,
|
2015-09-10 17:39:18 +00:00
|
|
|
Netrc: netrc,
|
2015-09-30 01:21:17 +00:00
|
|
|
Config: string(raw),
|
|
|
|
Secret: string(sec),
|
|
|
|
System: &model.System{
|
2015-09-02 23:14:25 +00:00
|
|
|
Link: httputil.GetURL(c.Request),
|
2015-09-09 01:55:03 +00:00
|
|
|
Plugins: strings.Split(os.Getenv("PLUGIN_FILTER"), " "),
|
|
|
|
Globals: strings.Split(os.Getenv("PLUGIN_PARAMS"), " "),
|
2015-09-02 23:14:25 +00:00
|
|
|
},
|
2015-04-24 21:25:03 +00:00
|
|
|
})
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|