2014-06-04 21:25:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2014-07-18 05:49:11 +00:00
|
|
|
"strings"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
"github.com/drone/drone/plugin/remote"
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/server/database"
|
2014-07-18 05:49:11 +00:00
|
|
|
"github.com/drone/drone/shared/build/script"
|
2014-07-13 02:01:58 +00:00
|
|
|
"github.com/drone/drone/shared/httputil"
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/gorilla/pat"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HookHandler struct {
|
2014-06-12 23:41:04 +00:00
|
|
|
users database.UserManager
|
|
|
|
repos database.RepoManager
|
|
|
|
commits database.CommitManager
|
2014-07-13 02:01:58 +00:00
|
|
|
remotes database.RemoteManager
|
|
|
|
queue chan *model.Request
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
func NewHookHandler(users database.UserManager, repos database.RepoManager, commits database.CommitManager, remotes database.RemoteManager, queue chan *model.Request) *HookHandler {
|
|
|
|
return &HookHandler{users, repos, commits, remotes, queue}
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostHook receives a post-commit hook from GitHub, Bitbucket, etc
|
|
|
|
// GET /hook/:host
|
|
|
|
func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error {
|
2014-09-02 07:18:17 +00:00
|
|
|
var host = r.FormValue(":host")
|
|
|
|
var remote = remote.Lookup(host)
|
|
|
|
if remote == nil {
|
2014-06-04 21:25:38 +00:00
|
|
|
return notFound{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the hook payload
|
2014-09-02 07:18:17 +00:00
|
|
|
hook, err := remote.ParseHook(r)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in some cases we have neither a hook nor error. An example
|
|
|
|
// would be GitHub sending a ping request to the URL, in which
|
|
|
|
// case we'll just exit quiely with an 'OK'
|
2014-07-18 05:49:11 +00:00
|
|
|
if hook == nil || strings.Contains(hook.Message, "[CI SKIP]") {
|
2014-06-04 21:25:38 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the repository from the database
|
2014-09-02 07:18:17 +00:00
|
|
|
repo, err := h.repos.FindName(remote.GetHost(), hook.Owner, hook.Repo)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return notFound{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if repo.Active == false ||
|
|
|
|
(repo.PostCommit == false && len(hook.PullRequest) == 0) ||
|
|
|
|
(repo.PullRequest == false && len(hook.PullRequest) != 0) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the user from the database that owns this repo
|
|
|
|
user, err := h.users.Find(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return notFound{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// featch the .drone.yml file from the database
|
2014-09-02 07:18:17 +00:00
|
|
|
yml, err := remote.GetScript(user, repo, hook)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
}
|
|
|
|
|
2014-07-18 05:49:11 +00:00
|
|
|
// verify the commit hooks branch matches the list of approved
|
|
|
|
// branches (unless it is a pull request). Note that we don't really
|
|
|
|
// care if parsing the yaml fails here.
|
2014-09-02 07:18:17 +00:00
|
|
|
s, _ := script.ParseBuild(string(yml), map[string]string{})
|
2014-07-18 05:49:11 +00:00
|
|
|
if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
c := model.Commit{
|
2014-06-04 21:25:38 +00:00
|
|
|
RepoID: repo.ID,
|
2014-06-12 23:41:04 +00:00
|
|
|
Status: model.StatusEnqueue,
|
2014-06-04 21:25:38 +00:00
|
|
|
Sha: hook.Sha,
|
|
|
|
Branch: hook.Branch,
|
|
|
|
PullRequest: hook.PullRequest,
|
|
|
|
Timestamp: hook.Timestamp,
|
2014-06-12 00:42:49 +00:00
|
|
|
Message: hook.Message,
|
2014-09-02 07:18:17 +00:00
|
|
|
Config: string(yml)}
|
2014-06-04 21:25:38 +00:00
|
|
|
c.SetAuthor(hook.Author)
|
|
|
|
// inser the commit into the database
|
|
|
|
if err := h.commits.Insert(&c); err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:44:19 +00:00
|
|
|
//fmt.Printf("%s", yml)
|
2014-08-04 04:28:51 +00:00
|
|
|
owner, err := h.users.Find(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
}
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// drop the items on the queue
|
2014-06-21 21:22:38 +00:00
|
|
|
go func() {
|
2014-07-13 02:01:58 +00:00
|
|
|
h.queue <- &model.Request{
|
2014-08-04 04:28:51 +00:00
|
|
|
User: owner,
|
2014-07-13 02:01:58 +00:00
|
|
|
Host: httputil.GetURL(r),
|
2014-06-21 21:22:38 +00:00
|
|
|
Repo: repo,
|
|
|
|
Commit: &c,
|
|
|
|
}
|
|
|
|
}()
|
2014-07-16 07:33:55 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2014-06-04 21:25:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HookHandler) Register(r *pat.Router) {
|
2014-06-21 21:22:38 +00:00
|
|
|
r.Post("/v1/hook/{host}", errorHandler(h.PostHook))
|
|
|
|
r.Put("/v1/hook/{host}", errorHandler(h.PostHook))
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|