115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/drone/drone/server/resource/build"
|
||
|
"github.com/drone/drone/server/resource/commit"
|
||
|
"github.com/drone/drone/server/resource/config"
|
||
|
"github.com/drone/drone/server/resource/repo"
|
||
|
"github.com/drone/drone/server/resource/user"
|
||
|
"github.com/gorilla/pat"
|
||
|
)
|
||
|
|
||
|
type HookHandler struct {
|
||
|
users user.UserManager
|
||
|
repos repo.RepoManager
|
||
|
commits commit.CommitManager
|
||
|
builds build.BuildManager
|
||
|
conf *config.Config
|
||
|
}
|
||
|
|
||
|
func NewHookHandler(users user.UserManager, repos repo.RepoManager, commits commit.CommitManager,
|
||
|
builds build.BuildManager, conf *config.Config) *HookHandler {
|
||
|
return &HookHandler{users, repos, commits, builds, conf}
|
||
|
}
|
||
|
|
||
|
// PostHook receives a post-commit hook from GitHub, Bitbucket, etc
|
||
|
// GET /hook/:host
|
||
|
func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error {
|
||
|
host := r.FormValue(":host")
|
||
|
|
||
|
// get the remote system's client.
|
||
|
remote := h.conf.GetRemote(host)
|
||
|
if remote == nil {
|
||
|
return notFound{}
|
||
|
}
|
||
|
|
||
|
// parse the hook payload
|
||
|
hook, err := remote.GetHook(r)
|
||
|
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'
|
||
|
if hook == nil {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// fetch the repository from the database
|
||
|
repo, err := h.repos.FindName(remote.GetName(), hook.Owner, hook.Repo)
|
||
|
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
|
||
|
client := remote.GetClient(user.Access, user.Secret)
|
||
|
script, err := client.GetScript(hook)
|
||
|
if err != nil {
|
||
|
return badRequest{err}
|
||
|
}
|
||
|
|
||
|
c := commit.Commit{
|
||
|
RepoID: repo.ID,
|
||
|
Status: commit.StatusEnqueue,
|
||
|
Sha: hook.Sha,
|
||
|
Branch: hook.Branch,
|
||
|
PullRequest: hook.PullRequest,
|
||
|
Timestamp: hook.Timestamp,
|
||
|
Message: hook.Message}
|
||
|
c.SetAuthor(hook.Author)
|
||
|
// inser the commit into the database
|
||
|
if err := h.commits.Insert(&c); err != nil {
|
||
|
return badRequest{err}
|
||
|
}
|
||
|
|
||
|
b := build.Build{
|
||
|
CommitID: c.ID,
|
||
|
Status: c.Status,
|
||
|
Number: 1,
|
||
|
}
|
||
|
// inser the build entry into the database
|
||
|
if err := h.builds.Insert(&b); err != nil {
|
||
|
return internalServerError{err}
|
||
|
}
|
||
|
|
||
|
fmt.Printf("%#v", hook)
|
||
|
fmt.Printf("%s", script)
|
||
|
|
||
|
// drop the items on the queue
|
||
|
//h.queue.Add(&queue.BuildTask{Repo: repo, Commit: commit, Build: build, Script: script})
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (h *HookHandler) Register(r *pat.Router) {
|
||
|
r.Post("/hook/{host}", errorHandler(h.PostHook))
|
||
|
r.Put("/hook/{host}", errorHandler(h.PostHook))
|
||
|
}
|