Skip to content

Commit

Permalink
Merge branch 'main' into feat/oidc-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed May 16, 2024
2 parents 095208a + 178e678 commit ebc9518
Show file tree
Hide file tree
Showing 144 changed files with 6,456 additions and 740 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/validate-pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ jobs:

steps:
- name: validate title
env:
TITLE: ${{ github.event.pull_request.title }}
run: |
echo "${{ github.event.pull_request.title }}" | grep -Eq '^(feat|fix|chore|refactor|enhance|test|docs)(\(.*\)|)!?:\s.+$' && (echo "Pass"; exit 0) || (echo "Incorrect Format. Please see https://go-vela.github.io/docs/community/contributing_guidelines/#development-workflow"; exit 1)
echo "$TITLE" | grep -Eq '^(feat|fix|chore|refactor|enhance|test|docs)(\(.*\)|)!?:\s.+$' && (echo "Pass"; exit 0) || (echo "Incorrect Format. Please see https://go-vela.github.io/docs/community/contributing_guidelines/#development-workflow"; exit 1)
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ linters:
- bidichk # checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- contextcheck # check the function whether use a non-inherited context
- deadcode # finds unused code
- dupl # code clone detection
- errcheck # checks for unchecked errors
- errorlint # find misuses of errors
Expand All @@ -97,14 +96,12 @@ linters:
- nolintlint # reports ill-formed or insufficient nolint directives
- revive # linter for go
- staticcheck # applies static analysis checks, go vet on steroids
- structcheck # finds unused struct fields
- stylecheck # replacement for golint
- tenv # analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
- typecheck # parses and type-checks go code, like the front-end of a go compiler
- unconvert # remove unnecessary type conversions
- unparam # reports unused function parameters
- unused # checks for unused constants, variables, functions and types
- varcheck # finds unused global variables and constants
- whitespace # detects leading and trailing whitespace
- wsl # forces code to use empty lines

Expand Down
2 changes: 1 addition & 1 deletion api/admin/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
// "$ref": "#/definitions/Error"

// AllBuildsQueue represents the API handler to
// captures all running and pending builds stored in the database.
// capture all running and pending builds stored in the database.
func AllBuildsQueue(c *gin.Context) {
// capture middleware values
ctx := c.Request.Context()
Expand Down
293 changes: 293 additions & 0 deletions api/admin/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
// SPDX-License-Identifier: Apache-2.0

package admin

import (
"fmt"
"net/http"
"time"

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

"github.com/go-vela/server/api/types/settings"
"github.com/go-vela/server/compiler/native"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/image"
"github.com/go-vela/server/queue"
cliMiddleware "github.com/go-vela/server/router/middleware/cli"
sMiddleware "github.com/go-vela/server/router/middleware/settings"
uMiddleware "github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
)

// swagger:operation GET /api/v1/admin/settings admin GetSettings
//
// Get the currently configured settings.
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved settings
// type: json
// schema:
// "$ref": "#/definitions/Platform"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve settings
// schema:
// "$ref": "#/definitions/Error"

// GetSettings represents the API handler to
// captures settings stored in the database.
func GetSettings(c *gin.Context) {
// capture middleware values
s := sMiddleware.FromContext(c)

logrus.Info("Admin: reading settings")

// check captured value because we aren't retrieving settings from the database
// instead we are retrieving the auto-refreshed middleware value
if s == nil {
retErr := fmt.Errorf("settings not found")

util.HandleError(c, http.StatusNotFound, retErr)

return
}

c.JSON(http.StatusOK, s)
}

// swagger:operation PUT /api/v1/admin/settings admin UpdateSettings
//
// Update the platform settings singleton in the database.
//
// ---
// produces:
// - application/json
// parameters:
// - in: body
// name: body
// description: Payload containing settings to update
// required: true
// schema:
// "$ref": "#/definitions/Platform"
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated platform settings in the database
// type: json
// schema:
// "$ref": "#/definitions/Platform"
// '400':
// description: Unable to update settings — bad request
// schema:
// "$ref": "#/definitions/Error"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve platform settings to update
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to update platform settings in the database
// schema:
// "$ref": "#/definitions/Error"

// UpdateSettings represents the API handler to
// update the settings singleton stored in the database.
func UpdateSettings(c *gin.Context) {
// capture middleware values
s := sMiddleware.FromContext(c)
u := uMiddleware.FromContext(c)
ctx := c.Request.Context()

logrus.Info("Admin: updating settings")

// check captured value because we aren't retrieving settings from the database
// instead we are retrieving the auto-refreshed middleware value
if s == nil {
retErr := fmt.Errorf("settings not found")

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// duplicate settings to not alter the shared pointer
_s := new(settings.Platform)
_s.FromSettings(s)

// ensure we update the singleton record
_s.SetID(1)

// capture body from API request
input := new(settings.Platform)

err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for settings: %w", err)

util.HandleError(c, http.StatusBadRequest, retErr)

return
}

if input.Compiler != nil {
if input.CloneImage != nil {
// validate clone image
cloneImage := *input.CloneImage

_, err = image.ParseWithError(cloneImage)
if err != nil {
retErr := fmt.Errorf("invalid clone image %s: %w", cloneImage, err)

util.HandleError(c, http.StatusBadRequest, retErr)

return
}

_s.SetCloneImage(cloneImage)
}

if input.TemplateDepth != nil {
_s.SetTemplateDepth(*input.TemplateDepth)
}

if input.StarlarkExecLimit != nil {
_s.SetStarlarkExecLimit(*input.StarlarkExecLimit)
}
}

if input.Queue != nil {
if input.Queue.Routes != nil {
_s.SetRoutes(input.GetRoutes())
}
}

if input.RepoAllowlist != nil {
_s.SetRepoAllowlist(input.GetRepoAllowlist())
}

if input.ScheduleAllowlist != nil {
_s.SetScheduleAllowlist(input.GetScheduleAllowlist())
}

_s.SetUpdatedBy(u.GetName())

// send API call to update the settings
_s, err = database.FromContext(c).UpdateSettings(ctx, _s)
if err != nil {
retErr := fmt.Errorf("unable to update settings: %w", err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusOK, _s)
}

// swagger:operation DELETE /api/v1/admin/settings admin RestoreSettings
//
// Restore the currently configured settings to the environment defaults.
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully restored default settings in the database
// type: json
// schema:
// "$ref": "#/definitions/Platform"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve settings to restore
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to restore settings in the database
// schema:
// "$ref": "#/definitions/Error"

// RestoreSettings represents the API handler to
// restore settings stored in the database to the environment defaults.
func RestoreSettings(c *gin.Context) {
// capture middleware values
s := sMiddleware.FromContext(c)
u := uMiddleware.FromContext(c)
cliCtx := cliMiddleware.FromContext(c)
ctx := c.Request.Context()

logrus.Info("Admin: restoring settings")

// check captured value because we aren't retrieving settings from the database
// instead we are retrieving the auto-refreshed middleware value
if s == nil {
retErr := fmt.Errorf("settings not found")

util.HandleError(c, http.StatusNotFound, retErr)

return
}

compiler, err := native.FromCLIContext(cliCtx)
if err != nil {
retErr := fmt.Errorf("unable to restore settings: %w", err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

queue, err := queue.FromCLIContext(cliCtx)
if err != nil {
retErr := fmt.Errorf("unable to restore settings: %w", err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// initialize a new settings record
_s := settings.FromCLIContext(cliCtx)

_s.SetUpdatedAt(time.Now().UTC().Unix())
_s.SetUpdatedBy(u.GetName())

// read in defaults supplied from the cli runtime
compilerSettings := compiler.GetSettings()
_s.SetCompiler(compilerSettings)

queueSettings := queue.GetSettings()
_s.SetQueue(queueSettings)

// send API call to update the settings
s, err = database.FromContext(c).UpdateSettings(ctx, _s)
if err != nil {
retErr := fmt.Errorf("unable to update (restore) settings: %w", err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusOK, s)
}
2 changes: 1 addition & 1 deletion api/admin/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/go-vela/types/library"
)

// swagger:operation POST /api/v1/admin/workers/{worker}/register-token admin RegisterToken
// swagger:operation POST /api/v1/admin/workers/{worker}/register admin RegisterToken
//
// Get a worker registration token
//
Expand Down
2 changes: 1 addition & 1 deletion api/build/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import (
// schema:
// "$ref": "#/definitions/Error"

// CreateBuild represents the API handler to approve a build to run in the configured backend.
// ApproveBuild represents the API handler to approve a build to run in the configured backend.
func ApproveBuild(c *gin.Context) {
// capture middleware values
b := build.Retrieve(c)
Expand Down
4 changes: 4 additions & 0 deletions api/dashboard/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import (
// description: Unauthorized to delete dashboard
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to find dashboard
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Server error when deleting dashboard
// schema:
Expand Down
5 changes: 5 additions & 0 deletions api/dashboard/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ import (
// description: Unauthorized to retrieve dashboard
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to find dashboard
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Server error when retrieving dashboard
// schema:
Expand Down Expand Up @@ -98,6 +102,7 @@ func buildRepoPartials(c context.Context, repos []*types.DashboardRepo) ([]types
repo.Org = dbRepo.GetOrg()
repo.Name = dbRepo.GetName()
repo.Counter = dbRepo.GetCounter()
repo.Active = dbRepo.GetActive()

// list last 5 builds for repo given the branch and event filters
builds, err := database.FromContext(c).ListBuildsForDashboardRepo(c, dbRepo, r.GetBranches(), r.GetEvents())
Expand Down
Loading

0 comments on commit ebc9518

Please sign in to comment.