Skip to content

Commit

Permalink
Merge branch 'main' into feat/ruleset-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 12, 2024
2 parents 5d47580 + d83c78e commit 0af6469
Show file tree
Hide file tree
Showing 222 changed files with 765 additions and 222 deletions.
5 changes: 2 additions & 3 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
{
"description": "Update docker images in go files",
"fileMatch": [
"^.*\\.go$"
"^cmd/vela-server/.+\\.go$"
],
"matchStrings": [
"\\/\\/ renovate: image=(?<depName>.*?)\\s+?.*[:|=]\\s+\"(?<currentValue>.*)\"\\,?"
"\"(?<depName>.*?):(?<currentValue>[^\"]*?)@(?<currentDigest>sha256:[a-f0-9]+)\",? // renovate: container"
],
"versioningTemplate": "docker",
"datasourceTemplate": "docker"
}
]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: create spec
run: |
sudo make spec-install
make spec-install
make spec
- name: upload spec
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ jobs:
- name: validate spec
run: |
sudo make spec-install
make spec-install
make spec
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,10 @@ spec-install:
$(if $(shell command -v apt-get 2> /dev/null),,$(error 'apt-get' not found - install jq, sponge, and go-swagger manually))
@echo
@echo "### Installing utilities (jq and sponge)"
@apt-get update
@apt-get install -y jq moreutils
@echo "### Downloading and installing go-swagger"
@curl -o /usr/local/bin/swagger -L "https://github.com/go-swagger/go-swagger/releases/download/v0.30.2/swagger_linux_amd64"
@chmod +x /usr/local/bin/swagger
@sudo apt-get update
@sudo apt-get install -y jq moreutils
@echo "### Installing go-swagger"
@go install github.com/go-swagger/go-swagger/cmd/swagger@v0.31.0

# The `spec-gen` target is intended to create an api-spec
# using go-swagger (https://goswagger.io)
Expand Down
2 changes: 0 additions & 2 deletions api/jwks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
// produces:
// - application/json
// parameters:
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the Vela JWKS
Expand Down
12 changes: 10 additions & 2 deletions api/log/get_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package log

import (
"errors"
"fmt"
"net/http"

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

"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/build"
Expand Down Expand Up @@ -85,9 +87,15 @@ func GetServiceLog(c *gin.Context) {
// send API call to capture the service logs
sl, err := database.FromContext(c).GetLogForService(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)
var status int
if errors.Is(err, gorm.ErrRecordNotFound) {
status = http.StatusNotFound
} else {
status = http.StatusInternalServerError
}

util.HandleError(c, http.StatusInternalServerError, retErr)
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)
util.HandleError(c, status, retErr)

return
}
Expand Down
12 changes: 10 additions & 2 deletions api/log/get_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package log

import (
"errors"
"fmt"
"net/http"

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

"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/build"
Expand Down Expand Up @@ -86,9 +88,15 @@ func GetStepLog(c *gin.Context) {
// send API call to capture the step logs
sl, err := database.FromContext(c).GetLogForStep(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)
var status int
if errors.Is(err, gorm.ErrRecordNotFound) {
status = http.StatusNotFound
} else {
status = http.StatusInternalServerError
}

util.HandleError(c, http.StatusInternalServerError, retErr)
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)
util.HandleError(c, status, retErr)

return
}
Expand Down
5 changes: 3 additions & 2 deletions api/oi_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
// produces:
// - application/json
// parameters:
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the Vela OpenID Configuration
Expand All @@ -46,6 +44,7 @@ func GetOpenIDConfig(c *gin.Context) {
"iat",
"iss",
"aud",
"branch",
"build_number",
"build_id",
"repo",
Expand All @@ -54,6 +53,8 @@ func GetOpenIDConfig(c *gin.Context) {
"actor_scm_id",
"commands",
"image",
"image_name",
"image_tag",
"request",
"event",
"sha",
Expand Down
2 changes: 1 addition & 1 deletion api/types/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func testExecutor() *Executor {
{
ID: "step_github_octocat_1_clone",
Directory: "/home/github/octocat",
Image: "target/vela-git:v0.3.0",
Image: "target/vela-git-slim:v0.12.0",
Name: "clone",
Number: 2,
Pull: "always",
Expand Down
28 changes: 19 additions & 9 deletions api/types/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package types

import (
"github.com/golang-jwt/jwt/v5"
"github.com/lestrrat-go/jwx/v2/jwk"
)

// OpenIDConfig is a struct that represents the OpenID Connect configuration.
Expand All @@ -22,24 +21,35 @@ type OpenIDConfig struct {
// OpenIDClaims struct is an extension of the JWT standard claims. It
// includes information relevant to OIDC services.
type OpenIDClaims struct {
BuildNumber int `json:"build_number,omitempty"`
BuildID int64 `json:"build_id,omitempty"`
Actor string `json:"actor,omitempty"`
ActorSCMID string `json:"actor_scm_id,omitempty"`
Repo string `json:"repo,omitempty"`
TokenType string `json:"token_type,omitempty"`
Image string `json:"image,omitempty"`
Request string `json:"request,omitempty"`
Branch string `json:"branch,omitempty"`
BuildID int64 `json:"build_id,omitempty"`
BuildNumber int `json:"build_number,omitempty"`
Commands bool `json:"commands,omitempty"`
Event string `json:"event,omitempty"`
Image string `json:"image,omitempty"`
ImageName string `json:"image_name,omitempty"`
ImageTag string `json:"image_tag,omitempty"`
Ref string `json:"ref,omitempty"`
Repo string `json:"repo,omitempty"`
Request string `json:"request,omitempty"`
SHA string `json:"sha,omitempty"`
TokenType string `json:"token_type,omitempty"`
jwt.RegisteredClaims
}

// JWKSet is a wrapper of lestrrat-go/jwx/jwk.Set for API Swagger gen.
// JWKSet exists solely to provide proper swagger documentation.
// It is not otherwise used in code.
//
// swagger:model JWKSet
type JWKSet struct {
jwk.Set
Keys []JWK `json:"keys"`
}

type JWK struct {
Kty string `json:"kty"`
Kid string `json:"kid"`
E string `json:"e"`
N string `json:"n"`
}
2 changes: 1 addition & 1 deletion api/types/settings/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestTypes_Compiler_String(t *testing.T) {
func testCompilerSettings() *Compiler {
cs := new(Compiler)

cs.SetCloneImage("target/vela-git:latest")
cs.SetCloneImage("target/vela-git-slim:latest")
cs.SetTemplateDepth(1)
cs.SetStarlarkExecLimit(100)

Expand Down
2 changes: 1 addition & 1 deletion api/types/settings/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func testPlatformSettings() *Platform {
// setup compiler
cs := new(Compiler)

cs.SetCloneImage("target/vela-git:latest")
cs.SetCloneImage("target/vela-git-slim:latest")
cs.SetTemplateDepth(1)
cs.SetStarlarkExecLimit(100)

Expand Down
2 changes: 1 addition & 1 deletion api/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"strings"

"github.com/buildkite/yaml"
json "github.com/ghodss/yaml"
"gopkg.in/yaml.v3"
)

// ToString is a helper function to convert
Expand Down
95 changes: 72 additions & 23 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,35 +239,77 @@ func PostWebhook(c *gin.Context) {
b.SetRepo(repo)
h.SetRepoID(repo.GetID())

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", repo.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
// number of times to retry
retryLimit := 3
// implement a loop to process asynchronous operations with a retry limit
//
// Some operations taken during the webhook workflow can lead to race conditions
// failing to successfully process the request. This logic ensures we attempt our
// best efforts to handle these cases gracefully.
for i := 0; i < retryLimit; i++ {
// check if we're on the first iteration of the loop
if i > 0 {
// incrementally sleep in between retries
time.Sleep(time.Duration(i) * time.Second)
}

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())
// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, repo)
if err != nil {
// format the error message with extra information
err = fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

return
}
// log the error for traceability
logrus.Error(err.Error())

// set the Number field
if lastHook != nil {
h.SetNumber(
lastHook.GetNumber() + 1,
)
}
// check if the retry limit has been exceeded
if i < retryLimit {
// continue to the next iteration of the loop
continue
}

// send API call to create the webhook
h, err = database.FromContext(c).CreateHook(ctx, h)
if err != nil {
retErr := fmt.Errorf("unable to create webhook %s/%d: %w", repo.GetFullName(), h.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
retErr := fmt.Errorf("%s: %w", baseErr, err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())
h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
return
}

// set the Number field
if lastHook != nil {
h.SetNumber(
lastHook.GetNumber() + 1,
)
}

// send API call to create the webhook
h, err = database.FromContext(c).CreateHook(ctx, h)
if err != nil {
// format the error message with extra information
err = fmt.Errorf("unable to create webhook %s/%d: %w", r.GetFullName(), h.GetNumber(), err)

// log the error for traceability
logrus.Error(err.Error())

// check if the retry limit has been exceeded
if i < retryLimit {
// continue to the next iteration of the loop
continue
}

retErr := fmt.Errorf("%s: %w", baseErr, err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}

// hook was created successfully
break
}

l.WithFields(logrus.Fields{
Expand Down Expand Up @@ -366,8 +408,15 @@ func PostWebhook(c *gin.Context) {
h.SetStatus(constants.StatusFailure)
h.SetError(err.Error())

b.SetStatus(constants.StatusError)

util.HandleError(c, code, err)

err = scm.FromContext(c).Status(ctx, repo.GetOwner(), b, repo.GetOrg(), repo.GetName())
if err != nil {
l.Debugf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err)
}

return
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/vela-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ func main() {
EnvVars: []string{"VELA_CLONE_IMAGE"},
Name: "clone-image",
Usage: "the clone image to use for the injected clone step",
// renovate: image=target/vela-git
Value: "target/vela-git:v0.8.0@sha256:02de004ae9dbf184c70039cb9ce431c31d6e7580eb9e6ec64a97ebf108aa65cb",
Value: "target/vela-git-slim:v0.12.0@sha256:533786ab3ef17c900b0105fdffbd7143d2601803f28b39e156132ad25819af0f", // renovate: container
},
&cli.StringSliceFlag{
EnvVars: []string{"VELA_REPO_ALLOWLIST"},
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/go-vela/types/yaml"
)

const defaultCloneImage = "target/vela-git:latest"
const defaultCloneImage = "target/vela-git-slim:latest"

func TestNative_CloneStage(t *testing.T) {
// setup types
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"strings"
"time"

yml "github.com/buildkite/yaml"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-retryablehttp"
yml "gopkg.in/yaml.v3"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/types/constants"
Expand Down
Loading

0 comments on commit 0af6469

Please sign in to comment.