2014-06-05 00:25:38 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
"github.com/drone/drone/server/datastore"
|
2014-09-29 06:21:02 +03:00
|
|
|
"github.com/drone/drone/server/worker"
|
|
|
|
"github.com/drone/drone/shared/httputil"
|
|
|
|
"github.com/drone/drone/shared/model"
|
2014-09-29 04:36:24 +03:00
|
|
|
"github.com/goji/context"
|
|
|
|
"github.com/zenazn/goji/web"
|
2014-06-05 00:25:38 +03:00
|
|
|
)
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
// GetCommitList accepts a request to retrieve a list
|
|
|
|
// of recent commits by Repo, and retur in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/repos/:host/:owner/:name/commits
|
|
|
|
//
|
|
|
|
func GetCommitList(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var repo = ToRepo(c)
|
2014-06-05 00:25:38 +03:00
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
commits, err := datastore.GetCommitList(ctx, repo)
|
2014-06-05 00:25:38 +03:00
|
|
|
if err != nil {
|
2014-09-29 04:36:24 +03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
json.NewEncoder(w).Encode(commits)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
// GetCommit accepts a request to retrieve a commit
|
|
|
|
// from the datastore for the given repository, branch and
|
|
|
|
// commit hash.
|
|
|
|
//
|
|
|
|
// GET /api/repos/:host/:owner/:name/branches/:branch/commits/:commit
|
|
|
|
//
|
|
|
|
func GetCommit(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var (
|
|
|
|
branch = c.URLParams["branch"]
|
|
|
|
hash = c.URLParams["commit"]
|
|
|
|
repo = ToRepo(c)
|
|
|
|
)
|
|
|
|
|
|
|
|
commit, err := datastore.GetCommitSha(ctx, repo, branch, hash)
|
2014-06-05 00:25:38 +03:00
|
|
|
if err != nil {
|
2014-09-29 04:36:24 +03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
json.NewEncoder(w).Encode(commit)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:21:02 +03:00
|
|
|
// PostHook accepts a post-commit hook and parses the payload
|
|
|
|
// in order to trigger a build. The payload is specified to the
|
|
|
|
// remote system (ie GitHub) and will therefore get parsed by
|
|
|
|
// the appropriate remote plugin.
|
|
|
|
//
|
|
|
|
// POST /api/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}
|
|
|
|
//
|
|
|
|
func PostCommit(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var (
|
|
|
|
branch = c.URLParams["branch"]
|
|
|
|
hash = c.URLParams["commit"]
|
|
|
|
repo = ToRepo(c)
|
|
|
|
)
|
2014-06-12 03:42:49 +03:00
|
|
|
|
2014-09-29 06:21:02 +03:00
|
|
|
commit, err := datastore.GetCommitSha(ctx, repo, branch, hash)
|
2014-06-12 03:42:49 +03:00
|
|
|
if err != nil {
|
2014-09-29 06:21:02 +03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2014-06-12 03:42:49 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:21:02 +03:00
|
|
|
if commit.Status == model.StatusStarted ||
|
|
|
|
commit.Status == model.StatusEnqueue {
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
return
|
2014-06-12 03:42:49 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:21:02 +03:00
|
|
|
commit.Status = model.StatusEnqueue
|
|
|
|
commit.Started = 0
|
|
|
|
commit.Finished = 0
|
|
|
|
commit.Duration = 0
|
|
|
|
if err := datastore.PutCommit(ctx, commit); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2014-06-12 03:42:49 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 06:21:02 +03:00
|
|
|
owner, err := datastore.GetUser(ctx, repo.UserID)
|
2014-08-14 22:44:36 +03:00
|
|
|
if err != nil {
|
2014-09-29 06:21:02 +03:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
2014-08-14 22:44:36 +03:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:42:49 +03:00
|
|
|
// drop the items on the queue
|
2014-09-29 06:21:02 +03:00
|
|
|
go worker.Do(ctx, &worker.Work{
|
|
|
|
User: owner,
|
|
|
|
Repo: repo,
|
|
|
|
Commit: commit,
|
|
|
|
Host: httputil.GetURL(r),
|
|
|
|
})
|
2014-07-16 10:33:55 +03:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|