From 2d6555cd2696e0a3785272a1e75ed06cbf205fb6 Mon Sep 17 00:00:00 2001 From: davidvader Date: Thu, 2 Jan 2025 11:25:39 -0600 Subject: [PATCH] feat: unauthenticated status endpoints --- api/build/status.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ api/repo/status.go | 61 ++++++++++++++++++++++++++++++++++++++++++ router/router.go | 10 +++++++ 3 files changed, 136 insertions(+) create mode 100644 api/build/status.go create mode 100644 api/repo/status.go diff --git a/api/build/status.go b/api/build/status.go new file mode 100644 index 000000000..efc1370a3 --- /dev/null +++ b/api/build/status.go @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: Apache-2.0 + +package build + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/go-vela/server/router/middleware/build" + "github.com/sirupsen/logrus" +) + +// swagger:operation GET /status/{org}/{repo}/{build} builds GetStatus +// +// Get a build status +// +// --- +// produces: +// - application/json +// parameters: +// - in: path +// name: org +// description: Name of the organization +// required: true +// type: string +// - in: path +// name: repo +// description: Name of the repository +// required: true +// type: string +// - in: path +// name: build +// description: Build number +// required: true +// type: integer +// security: +// - ApiKeyAuth: [] +// responses: +// '200': +// description: Successfully retrieved the build +// schema: +// "$ref": "#/definitions/Build" +// '400': +// description: Invalid request payload or path +// schema: +// "$ref": "#/definitions/Build" +// '401': +// description: Unauthorized +// schema: +// "$ref": "#/definitions/Build" +// '404': +// description: Not found +// schema: +// "$ref": "#/definitions/Build" + +// GetStatus represents the API handler to return "status", a lite representation of the resource with limited fields for unauthenticated access. +func GetStatus(c *gin.Context) { + // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) + b := build.Retrieve(c) + + l.Debug("reading status for build") + + c.JSON(http.StatusOK, b) +} diff --git a/api/repo/status.go b/api/repo/status.go new file mode 100644 index 000000000..f20f2d6ef --- /dev/null +++ b/api/repo/status.go @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: Apache-2.0 + +package repo + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + + "github.com/go-vela/server/router/middleware/repo" +) + +// swagger:operation GET /status/{org}/{repo} repos GetStatus +// +// Get a repository status +// +// --- +// produces: +// - application/json +// parameters: +// - in: path +// name: org +// description: Name of the organization +// required: true +// type: string +// - in: path +// name: repo +// description: Name of the repository +// required: true +// type: string +// security: +// - ApiKeyAuth: [] +// responses: +// '200': +// description: Successfully retrieved the repo +// schema: +// "$ref": "#/definitions/Repo" +// '400': +// description: Invalid request payload or path +// schema: +// "$ref": "#/definitions/Repo" +// '401': +// description: Unauthorized +// schema: +// "$ref": "#/definitions/Repo" +// '404': +// description: Not found +// schema: +// "$ref": "#/definitions/Repo" + +// GetStatus represents the API handler to return "status", a lite representation of the resource with limited fields for unauthenticated access. +func GetStatus(c *gin.Context) { + // capture middleware values + l := c.MustGet("logger").(*logrus.Entry) + r := repo.Retrieve(c) + + l.Debug("reading status for repo") + + c.JSON(http.StatusOK, r) +} diff --git a/router/router.go b/router/router.go index 2e7aebbb2..81bbe2208 100644 --- a/router/router.go +++ b/router/router.go @@ -34,8 +34,11 @@ import ( "github.com/go-vela/server/api" "github.com/go-vela/server/api/auth" + apiBuild "github.com/go-vela/server/api/build" + apiRepo "github.com/go-vela/server/api/repo" "github.com/go-vela/server/api/webhook" "github.com/go-vela/server/router/middleware" + "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/claims" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" @@ -62,6 +65,13 @@ func Load(options ...gin.HandlerFunc) *gin.Engine { // Badge endpoint r.GET("/badge/:org/:repo/status.svg", org.Establish(), repo.Establish(), api.GetBadge) + // Status endpoints + status := r.Group("/status/:org/:repo", org.Establish(), repo.Establish()) + { + status.GET("", org.Establish(), repo.Establish(), apiRepo.GetStatus) + status.GET("/:build", org.Establish(), repo.Establish(), build.Establish(), apiBuild.GetStatus) + } + // Health endpoint r.GET("/health", api.Health)