2016-03-31 06:27:53 +00:00
|
|
|
package web
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
2015-09-09 01:55:03 +00:00
|
|
|
"os"
|
2015-09-30 01:21:17 +00:00
|
|
|
"path/filepath"
|
2015-12-11 00:44:14 +00:00
|
|
|
"regexp"
|
2016-01-10 23:19:48 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-04-21 07:25:30 +00:00
|
|
|
"github.com/drone/drone/bus"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/engine"
|
|
|
|
"github.com/drone/drone/model"
|
2016-04-19 20:02:28 +00:00
|
|
|
"github.com/drone/drone/queue"
|
2015-10-05 00:40:27 +00:00
|
|
|
"github.com/drone/drone/remote"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/shared/httputil"
|
|
|
|
"github.com/drone/drone/shared/token"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/store"
|
2016-04-19 20:02:28 +00:00
|
|
|
"github.com/drone/drone/yaml"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2016-03-22 10:34:33 +00:00
|
|
|
var (
|
|
|
|
droneYml = os.Getenv("BUILD_CONFIG_FILE")
|
|
|
|
droneSec string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if droneYml == "" {
|
|
|
|
droneYml = ".drone.yml"
|
|
|
|
}
|
|
|
|
droneSec = fmt.Sprintf("%s.sec", strings.TrimSuffix(droneYml, filepath.Ext(droneYml)))
|
|
|
|
}
|
|
|
|
|
2015-12-11 01:06:56 +00:00
|
|
|
var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
func PostHook(c *gin.Context) {
|
2015-10-21 23:14:02 +00:00
|
|
|
remote_ := remote.FromContext(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-10-05 00:40:27 +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
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:44:14 +00:00
|
|
|
// skip the build if any case-insensitive combination of the words "skip" and "ci"
|
|
|
|
// wrapped in square brackets appear in the commit message
|
2015-12-11 01:12:27 +00:00
|
|
|
skipMatch := skipRe.FindString(build.Message)
|
|
|
|
if len(skipMatch) > 0 {
|
|
|
|
log.Infof("ignoring hook. %s found in %s", skipMatch, build.Commit)
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
repo, err := store.GetRepoOwnerName(c, 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-10-21 23:14:02 +00:00
|
|
|
user, err := store.GetUser(c, 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-10-07 01:31:01 +00:00
|
|
|
// if there is no email address associated with the pull request,
|
|
|
|
// we lookup the email address based on the authors github login.
|
|
|
|
//
|
|
|
|
// my initial hesitation with this code is that it has the ability
|
|
|
|
// to expose your email address. At the same time, your email address
|
|
|
|
// is already exposed in the public .git log. So while some people will
|
|
|
|
// a small number of people will probably be upset by this, I'm not sure
|
|
|
|
// it is actually that big of a deal.
|
|
|
|
if len(build.Email) == 0 {
|
2015-10-21 23:14:02 +00:00
|
|
|
author, err := store.GetUserLogin(c, build.Author)
|
2015-10-07 01:31:01 +00:00
|
|
|
if err == nil {
|
|
|
|
build.Email = author.Email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:40:27 +00:00
|
|
|
// if the remote has a refresh token, the current access token
|
|
|
|
// may be stale. Therefore, we should refresh prior to dispatching
|
|
|
|
// the job.
|
|
|
|
if refresher, ok := remote_.(remote.Refresher); ok {
|
|
|
|
ok, _ := refresher.Refresh(user)
|
|
|
|
if ok {
|
2015-10-21 23:14:02 +00:00
|
|
|
store.UpdateUser(c, user)
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 10:34:33 +00:00
|
|
|
// fetch the build file from the database
|
|
|
|
raw, err := remote_.File(user, repo, build, droneYml)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2016-03-22 10:34:33 +00:00
|
|
|
log.Errorf("failure to get build config 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
|
|
|
|
}
|
2016-03-22 10:34:33 +00:00
|
|
|
sec, err := remote_.File(user, repo, build, droneSec)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("cannot find build secrets for %s. %s", repo.FullName, err)
|
|
|
|
// NOTE we don't exit on failure. The sec file is optional
|
|
|
|
}
|
2015-09-07 19:13:27 +00:00
|
|
|
|
2016-04-19 20:02:28 +00:00
|
|
|
axes, err := yaml.ParseMatrix(raw)
|
2015-04-16 07:24:53 +00:00
|
|
|
if err != nil {
|
2016-04-11 18:15:53 +00:00
|
|
|
c.String(500, "Failed to parse yaml file or calculate matrix. %s", err)
|
2015-04-16 07:24:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(axes) == 0 {
|
2016-04-19 20:02:28 +00:00
|
|
|
axes = append(axes, yaml.Axis{})
|
2015-04-16 07:24:53 +00:00
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-10-05 00:40:27 +00:00
|
|
|
netrc, err := remote_.Netrc(user, repo)
|
2015-04-28 22:08:21 +00:00
|
|
|
if err != nil {
|
2016-04-11 18:15:53 +00:00
|
|
|
c.String(500, "Failed to generate netrc file. %s", err)
|
2015-04-28 22:08:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
key, _ := store.GetKey(c, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
// verify the branches can be built vs skipped
|
2016-04-19 20:02:28 +00:00
|
|
|
branches := yaml.ParseBranch(raw)
|
2016-04-11 18:15:53 +00:00
|
|
|
if !branches.Matches(build.Branch) {
|
|
|
|
c.String(200, "Branch does not match restrictions defined in yaml")
|
2015-04-30 17:39:16 +00:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
// and use a transaction
|
2015-09-30 01:21:17 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
2015-10-21 23:14:02 +00:00
|
|
|
err = store.CreateBuild(c, 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-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)
|
2015-10-05 00:40:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-27 00:14:00 +00:00
|
|
|
// get the previous build so that we can send
|
2015-09-10 17:39:18 +00:00
|
|
|
// on status change notifications
|
2015-10-21 23:14:02 +00:00
|
|
|
last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID)
|
2016-04-21 07:25:30 +00:00
|
|
|
secs, _ := store.GetSecretList(c, repo)
|
2015-09-10 17:39:18 +00:00
|
|
|
|
2016-04-18 19:07:01 +00:00
|
|
|
// IMPORTANT. PLEASE READ
|
|
|
|
//
|
|
|
|
// The below code uses a feature flag to switch between the current
|
|
|
|
// build engine and the exerimental 0.5 build engine. This can be
|
|
|
|
// enabled using with the environment variable CANARY=true
|
|
|
|
|
|
|
|
if os.Getenv("CANARY") == "true" {
|
2016-04-21 07:25:30 +00:00
|
|
|
bus.Publish(c, bus.NewBuildEvent(bus.Enqueued, repo, build))
|
2016-04-18 19:07:01 +00:00
|
|
|
for _, job := range jobs {
|
|
|
|
queue.Publish(c, &queue.Work{
|
|
|
|
User: user,
|
|
|
|
Repo: repo,
|
|
|
|
Build: build,
|
|
|
|
BuildLast: last,
|
|
|
|
Job: job,
|
|
|
|
Keys: key,
|
|
|
|
Netrc: netrc,
|
|
|
|
Yaml: string(raw),
|
|
|
|
YamlEnc: string(sec),
|
2016-04-21 07:25:30 +00:00
|
|
|
Secrets: secs,
|
2016-04-18 19:07:01 +00:00
|
|
|
System: &model.System{
|
|
|
|
Link: httputil.GetURL(c.Request),
|
|
|
|
Plugins: strings.Split(os.Getenv("PLUGIN_FILTER"), " "),
|
|
|
|
Globals: strings.Split(os.Getenv("PLUGIN_PARAMS"), " "),
|
|
|
|
Escalates: strings.Split(os.Getenv("ESCALATE_FILTER"), " "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return // EXIT NOT TO AVOID THE 0.4 ENGINE CODE BELOW
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:08:17 +00:00
|
|
|
engine_ := engine.FromContext(c)
|
2015-10-21 23:14:02 +00:00
|
|
|
go engine_.Schedule(c.Copy(), &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{
|
2016-01-10 23:19:48 +00:00
|
|
|
Link: httputil.GetURL(c.Request),
|
|
|
|
Plugins: strings.Split(os.Getenv("PLUGIN_FILTER"), " "),
|
|
|
|
Globals: strings.Split(os.Getenv("PLUGIN_PARAMS"), " "),
|
|
|
|
Escalates: strings.Split(os.Getenv("ESCALATE_FILTER"), " "),
|
2015-09-02 23:14:25 +00:00
|
|
|
},
|
2015-04-24 21:25:03 +00:00
|
|
|
})
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|