diff --git a/cmd/droned/drone.go b/cmd/droned/drone.go index c638357c5..189f6c9e5 100644 --- a/cmd/droned/drone.go +++ b/cmd/droned/drone.go @@ -227,6 +227,7 @@ func setupHandlers() { // handlers for repository, commits and build details m.Get("/:host/:owner/:name/commit/:commit/build/:label/out.txt", handler.RepoHandler(handler.BuildOut)) + m.Get("/:host/:owner/:name/commit/:commit/build/:label/status.json", handler.PublicHandler(handler.BuildStatus)) m.Post("/:host/:owner/:name/commit/:commit/build/:label/rebuild", handler.RepoAdminHandler(rebuild.CommitRebuild)) m.Get("/:host/:owner/:name/commit/:commit/build/:label", handler.RepoHandler(handler.CommitShow)) m.Post("/:host/:owner/:name/commit/:commit/rebuild", handler.RepoAdminHandler(rebuild.CommitRebuild)) diff --git a/pkg/handler/builds.go b/pkg/handler/builds.go index be524a411..e06cdac16 100644 --- a/pkg/handler/builds.go +++ b/pkg/handler/builds.go @@ -7,6 +7,10 @@ import ( . "github.com/drone/drone/pkg/model" ) +type BuildResult struct { + Status string +} + // Returns the combined stdout / stderr for an individual Build. func BuildOut(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error { branch := r.FormValue("branch") @@ -32,6 +36,33 @@ func BuildOut(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error return RenderText(w, build.Stdout, http.StatusOK) } +// Returns the combined stdout / stderr for an individual Build. +func BuildStatus(w http.ResponseWriter, r *http.Request, repo *Repo) error { + branch := r.FormValue("branch") + if branch == "" { + branch = "master" + } + + hash := r.FormValue(":commit") + labl := r.FormValue(":label") + + // get the commit from the database + commit, err := database.GetCommitBranchHash(branch, hash, repo.ID) + if err != nil { + return err + } + + // get the build from the database + build, err := database.GetBuildSlug(labl, commit.ID) + if err != nil { + return err + } + + build_result := BuildResult{build.Status} + + return RenderJson(w, build_result) +} + // Returns the gzipped stdout / stderr for an individual Build func BuildOutGzip(w http.ResponseWriter, r *http.Request, u *User) error { // TODO diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index 40de6fd3c..7636ab3a2 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -64,10 +64,33 @@ func (h AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// PublicHandler wraps the default http.HandlerFunc to include +// requested Repository in the method signature, in addition +// to handling an error as the return value. +type PublicHandler func(w http.ResponseWriter, r *http.Request, repo *Repo) error + +func (h PublicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // repository name from the URL parameters + hostParam := r.FormValue(":host") + userParam := r.FormValue(":owner") + nameParam := r.FormValue(":name") + repoName := fmt.Sprintf("%s/%s/%s", hostParam, userParam, nameParam) + + repo, err := database.GetRepoSlug(repoName) + if err != nil || repo == nil { + RenderNotFound(w) + return + } + + h(w, r, repo) + return +} + // RepoHandler wraps the default http.HandlerFunc to include // the currently authenticated User and requested Repository // in the method signature, in addition to handling an error // as the return value. + type RepoHandler func(w http.ResponseWriter, r *http.Request, user *User, repo *Repo) error func (h RepoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {