2022-09-12 22:12:02 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.front.kjuulh.io/kjuulh/kraken/internal/commands"
|
|
|
|
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
|
|
|
|
"git.front.kjuulh.io/kjuulh/kraken/internal/services/jobs"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CommandRoute(logger *zap.Logger, app *gin.Engine, deps *serverdeps.ServerDeps) {
|
|
|
|
commandRoute := app.Group("commands")
|
|
|
|
commandRoute.POST("processRepos", func(c *gin.Context) {
|
|
|
|
type processReposRequest struct {
|
2022-09-13 22:54:49 +02:00
|
|
|
Repository string `json:"repository"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
Path string `json:"path"`
|
2022-09-12 22:12:02 +02:00
|
|
|
}
|
|
|
|
var request processReposRequest
|
|
|
|
err := c.BindJSON(&request)
|
|
|
|
if err != nil {
|
|
|
|
logger.Info("could not bind request", zap.String("request", "processRepo"), zap.Error(err))
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobId := uuid.New().String()
|
|
|
|
|
2022-09-13 22:54:49 +02:00
|
|
|
go func(repository string, branch string, path string, jobId string) {
|
2022-09-12 22:12:02 +02:00
|
|
|
ctx := context.WithValue(context.Background(), jobs.JobId{}, jobId)
|
|
|
|
processRepos := commands.NewProcessRepos(logger, deps)
|
2022-09-13 22:54:49 +02:00
|
|
|
err = processRepos.Process(ctx, repository, branch, path)
|
2022-09-14 23:40:58 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("could not process repo", zap.Error(err))
|
|
|
|
}
|
2022-09-13 22:54:49 +02:00
|
|
|
}(request.Repository, request.Branch, request.Path, jobId)
|
2022-09-12 22:12:02 +02:00
|
|
|
|
|
|
|
c.Status(http.StatusAccepted)
|
|
|
|
})
|
|
|
|
}
|