From 6245e49be7f1838d2876be621e870aa90481f42c Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Mon, 5 Sep 2022 03:30:10 +0200 Subject: [PATCH] Retrieve Drone build state --- repositories.go | 28 ++++++++++++++++++++++--- ui/src/components/WorkRepository.svelte | 19 +++++++++++++++++ ui/src/lib/repositories.js | 11 ++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/repositories.go b/repositories.go index 1d185de..93ecf4d 100644 --- a/repositories.go +++ b/repositories.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "strconv" + "strings" "time" "github.com/drone/drone-go/drone" @@ -155,10 +156,10 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) { now := time.Now() - if repo.LastCheck != nil && !repo.LastCheck.Before(now.Add(-5*time.Minute)) { + /*if repo.LastCheck != nil && !repo.LastCheck.Before(now.Add(-5*time.Minute)) { c.AbortWithStatusJSON(http.StatusPaymentRequired, gin.H{"errmsg": "Please wait between two pulls."}) return - } + }*/ client := drone.NewClient(droneEndpoint, droneConfig) result, err := client.BuildCreate("srs", "atsebay.t-worker", "", "master", map[string]string{ @@ -179,6 +180,27 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) { c.JSON(http.StatusOK, repo) }) + + repositoriesRoutes.GET("/state", func(c *gin.Context) { + repo := c.MustGet("repository").(*Repository) + + tmp := strings.Split(repo.DroneRef, "/") + nbuild, err := strconv.Atoi(tmp[2]) + if err != nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Bad build number. Please retry pulling your work."}) + return + } + + client := drone.NewClient(droneEndpoint, droneConfig) + result, err := client.Build(tmp[0], tmp[1], nbuild) + if err != nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Unable to find the referenced extraction."}) + return + } + + c.JSON(http.StatusOK, result) + + }) } func repositoryHandler(c *gin.Context) { @@ -252,7 +274,7 @@ func (u *User) getRepository(id int) (r *Repository, err error) { } func (u *User) NewRepository(w *Work, uri string) (*Repository, error) { - if res, err := DBExec("INSERT INTO user_work_repositories (id_user, id_work, uri) VALUES (?, ?, ?)", u.Id, w.Id, uri); err != nil { + if res, err := DBExec("INSERT INTO user_work_repositories (id_user, id_work, uri, droneref) VALUES (?, ?, ?, ?)", u.Id, w.Id, uri, ""); err != nil { return nil, err } else if rid, err := res.LastInsertId(); err != nil { return nil, err diff --git a/ui/src/components/WorkRepository.svelte b/ui/src/components/WorkRepository.svelte index 1984b2f..bb74549 100644 --- a/ui/src/components/WorkRepository.svelte +++ b/ui/src/components/WorkRepository.svelte @@ -52,6 +52,25 @@
Dépôt lié : {repo.uri}
Dernière récupération : {#if repo.last_check}{:else}-{/if} + {#await repo.getBuildState()} +
+ {:then state} + {#if state.status == "pending" || state.status == "running"} +
+ {:else if state.status == "success"} + + {:else if state.status == "failure"} + + {:else} + {state.status} + {/if} + {/await}
diff --git a/ui/src/lib/repositories.js b/ui/src/lib/repositories.js index 5ef2f85..3397625 100644 --- a/ui/src/lib/repositories.js +++ b/ui/src/lib/repositories.js @@ -25,6 +25,17 @@ export class WorkRepository { } } + async getBuildState() { + const res = await fetch(`api/repositories/${this.id}/state`, { + headers: {'Accept': 'application/json'} + }); + if (res.status == 200) { + return await res.json(); + } else { + throw new Error((await res.json()).errmsg); + } + } + async retrieveWork() { const res = await fetch(`api/repositories/${this.id}/trigger`, { method: 'POST',