brunel/handlers/build/builkRebuild.go

68 lines
1.5 KiB
Go
Raw Normal View History

2024-08-02 22:07:39 +02:00
package handlers_build
import (
"brunel/buildqueue"
"brunel/domain"
"brunel/packages"
"github.com/gofiber/fiber/v2"
)
type BulkRebuildRequestItem struct {
PackageName string `json:"packageName"`
BuildType string `json:"buildType"`
}
func BulkRebuild(c *fiber.Ctx) error {
var req []BulkRebuildRequestItem
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid request body",
})
}
if len(req) == 0 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "No packages specified",
})
}
packs := packages.GetPackages()
for _, reqItem := range req {
pack, ok := packs.Get(reqItem.PackageName)
if !ok {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Package not found",
})
}
2024-08-04 19:14:29 +02:00
buildVersion := pack.NewVersion
if buildVersion == "" {
buildVersion = pack.Version
}
2024-08-02 22:07:39 +02:00
buildItem := domain.BuildQueueItem{
Source: pack,
BuildVersion: buildVersion,
Type: domain.BuildType(reqItem.BuildType),
Rebuild: true,
Status: domain.Queued,
Patch: false,
BuildNumber: 33,
}
err := buildqueue.Add(buildItem)
if err != nil {
if err.Error() != "package already in queue" {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Failed to add build to queue",
})
}
}
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"message": "Build job added to queue",
})
}