Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: status endpoints #1235

Merged
merged 8 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions api/build/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: Apache-2.0

package build

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/router/middleware/build"
)

// swagger:operation GET /status/{org}/{repo}/{build} builds GetBuildStatus
//
// 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"

// GetBuildStatus represents the API handler to return "status", a lite representation of the resource with limited fields for unauthenticated access.
func GetBuildStatus(c *gin.Context) {
// capture middleware values
l := c.MustGet("logger").(*logrus.Entry)
b := build.Retrieve(c)

l.Debug("reading status for build")

// sanitize fields for the unauthenticated response
b.StatusSanitize()

c.JSON(http.StatusOK, b)
}
64 changes: 64 additions & 0 deletions api/repo/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 GetRepoStatus
//
// 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"

// GetRepoStatus represents the API handler to return "status", a lite representation of the resource with limited fields for unauthenticated access.
func GetRepoStatus(c *gin.Context) {
// capture middleware values
l := c.MustGet("logger").(*logrus.Entry)
r := repo.Retrieve(c)

l.Debug("reading status for repo")

// sanitize fields for the unauthenticated response
r.StatusSanitize()

c.JSON(http.StatusOK, r)
}
8 changes: 8 additions & 0 deletions api/types/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,11 @@ func (b *Build) String() string {
b.GetTitle(),
)
}

// StatusSanitize removes sensitive information before producing a "status".
func (b *Build) StatusSanitize() {
// sanitize repo
if b.Repo != nil {
b.Repo.StatusSanitize()
}
}
6 changes: 6 additions & 0 deletions api/types/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,9 @@ func (r *Repo) String() string {
r.GetInstallID(),
)
}

// StatusSanitize removes sensitive information before producing a "status".
func (r *Repo) StatusSanitize() {
// remove allowed events
r.AllowEvents = nil
}
10 changes: 10 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.GetRepoStatus)
status.GET("/:build", org.Establish(), repo.Establish(), build.Establish(), apiBuild.GetBuildStatus)
}

// Health endpoint
r.GET("/health", api.Health)

Expand Down
Loading