Retrieve Drone build state

This commit is contained in:
nemunaire 2022-09-05 03:30:10 +02:00
parent 65b62afb82
commit 6245e49be7
3 changed files with 55 additions and 3 deletions

View file

@ -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