From 43381827b96ba340c655834f416158f73aa79271 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 16 Apr 2015 22:07:13 -0700 Subject: [PATCH] stubs for restarting, canceling builds --- drone.go | 2 + server/builds.go | 76 +++++++++++++++++++++ server/static/scripts/controllers/builds.js | 11 +++ server/static/scripts/services/builds.js | 20 ++++++ server/static/scripts/views/build.html | 5 ++ 5 files changed, 114 insertions(+) diff --git a/drone.go b/drone.go index e8f92a7d6..e8b331a90 100644 --- a/drone.go +++ b/drone.go @@ -79,6 +79,8 @@ func main() { repo.GET("/builds", server.GetBuilds) repo.GET("/builds/:number", server.GetBuild) + repo.POST("/builds/:number", server.RunBuild) + repo.DELETE("/builds/:number", server.KillBuild) repo.GET("/logs/:number/:task", server.GetBuildLogs) repo.POST("/status/:number", server.PostBuildStatus) } diff --git a/server/builds.go b/server/builds.go index 23549ab5d..513cf70cf 100644 --- a/server/builds.go +++ b/server/builds.go @@ -95,3 +95,79 @@ func PostBuildStatus(c *gin.Context) { c.JSON(201, in) } } + +// RunBuild accepts a request to restart an existing build. +// +// POST /api/builds/:owner/:name/builds/:number +// +func RunBuild(c *gin.Context) { + store := ToDatastore(c) + repo := ToRepo(c) + num, err := strconv.Atoi(c.Params.ByName("number")) + if err != nil { + c.Fail(400, err) + return + } + build, err := store.Build(repo.FullName, num) + if err != nil { + c.Fail(404, err) + return + } + + // must not restart a running build + if build.State == common.StatePending || build.State == common.StateRunning { + c.Fail(409, err) + return + } + + build.State = common.StatePending + build.Started = 0 + build.Finished = 0 + build.Duration = 0 + build.Statuses = []*common.Status{} + for _, task := range build.Tasks { + task.State = common.StatePending + task.Started = 0 + task.Finished = 0 + task.ExitCode = 0 + } + + err = store.SetBuild(repo.FullName, build) + if err != nil { + c.Fail(500, err) + return + } + + // TODO push build to queue + + c.JSON(202, build) +} + +// KillBuild accepts a request to kill a running build. +// +// DELETE /api/builds/:owner/:name/builds/:number +// +func KillBuild(c *gin.Context) { + store := ToDatastore(c) + repo := ToRepo(c) + num, err := strconv.Atoi(c.Params.ByName("number")) + if err != nil { + c.Fail(400, err) + return + } + build, err := store.Build(repo.FullName, num) + if err != nil { + c.Fail(404, err) + return + } + + // must not restart a running build + if build.State != common.StatePending && build.State != common.StateRunning { + c.Fail(409, err) + return + } + + // TODO tell queue to cancel build + + c.Writer.WriteHeader(202) +} diff --git a/server/static/scripts/controllers/builds.js b/server/static/scripts/controllers/builds.js index 1e1374422..6b271b1dc 100644 --- a/server/static/scripts/controllers/builds.js +++ b/server/static/scripts/controllers/builds.js @@ -87,6 +87,17 @@ $scope.error = err; }); + $scope.restart = function() { + builds.restart(fullName, number).catch(function(err){ + $scope.error = err; + }); + }; + + $scope.cancel = function() { + builds.cancel(fullName, number).catch(function(err) { + $scope.error = err; + }); + }; } angular diff --git a/server/static/scripts/services/builds.js b/server/static/scripts/services/builds.js index ddb3cb36a..97826c416 100644 --- a/server/static/scripts/services/builds.js +++ b/server/static/scripts/services/builds.js @@ -26,6 +26,26 @@ this.get = function(repoName, buildNumber) { return $http.get('/api/repos/'+repoName+'/builds/'+buildNumber); }; + + /** + * Restarts a build. + * + * @param {string} Name of the repository. + * @param {number} Number of the build. + */ + this.restart = function(repoName, buildNumber) { + return $http.post('/api/repos/' + repoName+'/builds/'+buildNumber); + }; + + /** + * Cancels a running build. + * + * @param {string} Name of the repository. + * @param {number} Number of the build. + */ + this.cancel = function(repoName, buildNumber) { + return $http.delete('/api/repos/'+repoName+'/builds/'+buildNumber); + }; } angular diff --git a/server/static/scripts/views/build.html b/server/static/scripts/views/build.html index d67287c52..b022b7e99 100644 --- a/server/static/scripts/views/build.html +++ b/server/static/scripts/views/build.html @@ -2,6 +2,11 @@ Back +
+ + +
+
Build State
{{ build.state }}