brunel/handlers/build/triggerBuild.go

66 lines
1.5 KiB
Go

package handlers_build
import (
"brunel/buildqueue"
"brunel/domain"
"brunel/packages"
"github.com/gofiber/fiber/v2"
)
func TriggerBuild(c *fiber.Ctx) error {
var req struct {
PackageName string `json:"packageName"`
Version string `json:"version"`
BuildType string `json:"buildType"`
Rebuild bool `json:"rebuild"`
}
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid request body",
})
}
if req.PackageName == "" || req.Version == "" || req.BuildType == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Missing required fields",
})
}
packs := packages.GetPackages()
pack, ok := packs.Get(req.PackageName)
if !ok {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Package not found",
})
}
buildItem := domain.BuildQueueItem{
Source: pack,
BuildVersion: req.Version,
Type: domain.BuildType(req.BuildType),
Rebuild: req.Rebuild,
Status: domain.Queued,
Patch: false,
BuildNumber: 0,
}
err := buildqueue.Add(buildItem)
if err != nil {
if err.Error() == "package already in queue" {
return c.Status(fiber.StatusConflict).JSON(fiber.Map{
"error": "Package already in queue",
})
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Failed to add build to queue",
})
}
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"message": "Build job added to queue",
"package": req.PackageName,
})
}