Skip to content

Commit

Permalink
Merge branch 'main' into refactor/nested-api/hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 12, 2024
2 parents f25719c + b6e5d75 commit 7b5a2b7
Show file tree
Hide file tree
Showing 264 changed files with 1,077 additions and 446 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/[email protected]

# The `spec-gen` target is intended to create an api-spec
# using go-swagger (https://goswagger.io)
Expand Down
36 changes: 18 additions & 18 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type CompileAndPublishConfig struct {
//
//nolint:funlen,gocyclo // ignore function length due to comments, error handling, and general complexity of function
func CompileAndPublish(
c context.Context,
ctx context.Context,
cfg CompileAndPublishConfig,
database database.Interface,
scm scm.Service,
Expand All @@ -66,7 +66,7 @@ func CompileAndPublish(
baseErr := cfg.BaseErr

// confirm current repo owner has at least write access to repo (needed for status update later)
_, err := scm.RepoAccess(c, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName())
_, err := scm.RepoAccess(ctx, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to publish build to queue: repository owner %s no longer has write access to repository %s", u.GetName(), r.GetFullName())

Expand All @@ -87,7 +87,7 @@ func CompileAndPublish(
// if the event is issue_comment and the issue is a pull request,
// call SCM for more data not provided in webhook payload
if strings.EqualFold(cfg.Source, "webhook") && strings.EqualFold(b.GetEvent(), constants.EventComment) {
commit, branch, baseref, headref, err := scm.GetPullRequest(c, r, prNum)
commit, branch, baseref, headref, err := scm.GetPullRequest(ctx, r, prNum)
if err != nil {
retErr := fmt.Errorf("%s: failed to get pull request info for %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -103,7 +103,7 @@ func CompileAndPublish(
// if the source is from a schedule, fetch the commit sha from schedule branch (same as build branch at this moment)
if strings.EqualFold(cfg.Source, "schedule") {
// send API call to capture the commit sha for the branch
_, commit, err := scm.GetBranch(c, r, b.GetBranch())
_, commit, err := scm.GetBranch(ctx, r, b.GetBranch())
if err != nil {
retErr := fmt.Errorf("failed to get commit for repo %s on %s branch: %w", r.GetFullName(), r.GetBranch(), err)

Expand All @@ -119,7 +119,7 @@ func CompileAndPublish(
}

// send API call to capture the number of pending or running builds for the repo
builds, err := database.CountBuildsForRepo(c, r, filters)
builds, err := database.CountBuildsForRepo(ctx, r, filters)
if err != nil {
retErr := fmt.Errorf("%s: unable to get count of builds for repo %s", baseErr, r.GetFullName())

Expand Down Expand Up @@ -156,7 +156,7 @@ func CompileAndPublish(
!strings.EqualFold(b.GetEvent(), constants.EventPull) &&
!strings.EqualFold(b.GetEvent(), constants.EventDelete) {
// send API call to capture list of files changed for the commit
files, err = scm.Changeset(c, r, b.GetCommit())
files, err = scm.Changeset(ctx, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -167,7 +167,7 @@ func CompileAndPublish(
// check if the build event is a pull_request
if strings.EqualFold(b.GetEvent(), constants.EventPull) && prNum > 0 {
// send API call to capture list of files changed for the pull request
files, err = scm.ChangesetPR(c, r, prNum)
files, err = scm.ChangesetPR(ctx, r, prNum)
if err != nil {
retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err)

Expand Down Expand Up @@ -202,10 +202,10 @@ func CompileAndPublish(
}

// send database call to attempt to capture the pipeline if we already processed it before
pipeline, err = database.GetPipelineForRepo(c, b.GetCommit(), r)
pipeline, err = database.GetPipelineForRepo(ctx, b.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet
// send API call to capture the pipeline configuration file
pipelineFile, err = scm.ConfigBackoff(c, u, r, b.GetCommit())
pipelineFile, err = scm.ConfigBackoff(ctx, u, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("%s: unable to get pipeline configuration for %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -216,7 +216,7 @@ func CompileAndPublish(
}

// send API call to capture repo for the counter (grabbing repo again to ensure counter is correct)
repo, err = database.GetRepoForOrg(c, r.GetOrg(), r.GetName())
repo, err = database.GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("%s: unable to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand Down Expand Up @@ -269,7 +269,7 @@ func CompileAndPublish(
WithRepo(repo).
WithUser(u).
WithLabels(cfg.Labels).
Compile(pipelineFile)
Compile(ctx, pipelineFile)
if err != nil {
// format the error message with extra information
err = fmt.Errorf("unable to compile pipeline configuration for %s: %w", repo.GetFullName(), err)
Expand All @@ -295,7 +295,7 @@ func CompileAndPublish(
b.SetStatus(constants.StatusSkipped)

// send API call to set the status on the commit
err = scm.Status(c, u, b, repo.GetOrg(), repo.GetName())
err = scm.Status(ctx, u, b, repo.GetOrg(), repo.GetName())
if err != nil {
logger.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err)
}
Expand All @@ -316,7 +316,7 @@ func CompileAndPublish(
pipeline.SetRef(b.GetRef())

// send API call to create the pipeline
pipeline, err = database.CreatePipeline(c, pipeline)
pipeline, err = database.CreatePipeline(ctx, pipeline)
if err != nil {
retErr := fmt.Errorf("%s: failed to create pipeline for %s: %w", baseErr, repo.GetFullName(), err)

Expand Down Expand Up @@ -348,7 +348,7 @@ func CompileAndPublish(
// using the same Number and thus create a constraint
// conflict; consider deleting the partially created
// build object in the database
err = PlanBuild(c, database, scm, p, b, repo)
err = PlanBuild(ctx, database, scm, p, b, repo)
if err != nil {
retErr := fmt.Errorf("%s: %w", baseErr, err)

Expand All @@ -373,7 +373,7 @@ func CompileAndPublish(
} // end of retry loop

// send API call to update repo for ensuring counter is incremented
repo, err = database.UpdateRepo(c, repo)
repo, err = database.UpdateRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("%s: failed to update repo %s: %w", baseErr, repo.GetFullName(), err)

Expand Down Expand Up @@ -401,7 +401,7 @@ func CompileAndPublish(
}

// send API call to capture the triggered build
b, err = database.GetBuildForRepo(c, repo, b.GetNumber())
b, err = database.GetBuildForRepo(ctx, repo, b.GetNumber())
if err != nil {
retErr := fmt.Errorf("%s: failed to get new build %s/%d: %w", baseErr, repo.GetFullName(), b.GetNumber(), err)

Expand All @@ -414,7 +414,7 @@ func CompileAndPublish(
retErr := fmt.Errorf("unable to set route for build %d for %s: %w", b.GetNumber(), r.GetFullName(), err)

// error out the build
CleanBuild(c, database, b, nil, nil, retErr)
CleanBuild(ctx, database, b, nil, nil, retErr)

return nil, nil, http.StatusBadRequest, retErr
}
Expand All @@ -423,7 +423,7 @@ func CompileAndPublish(
b.SetHost(route)

// publish the pipeline.Build to the build_executables table to be requested by a worker
err = PublishBuildExecutable(c, database, p, b)
err = PublishBuildExecutable(ctx, database, p, b)
if err != nil {
retErr := fmt.Errorf("unable to publish build executable for %s/%d: %w", repo.GetFullName(), b.GetNumber(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/build/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func GetBuildGraph(c *gin.Context) {
WithMetadata(m).
WithRepo(r).
WithUser(u).
Compile(config)
Compile(ctx, config)
if err != nil {
// format the error message with extra information
err = fmt.Errorf("unable to compile pipeline configuration for %s: %w", r.GetFullName(), err)
Expand Down
2 changes: 1 addition & 1 deletion api/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func ListBuildsForOrg(c *gin.Context) {
}

// ensure per_page isn't above or below allowed values
perPage = util.MaxInt(1, util.MinInt(100, perPage))
perPage = max(1, min(100, perPage))

// See if the user is an org admin to bypass individual permission checks
perm, err := scm.FromContext(c).OrgAccess(ctx, u, o)
Expand Down
2 changes: 1 addition & 1 deletion api/build/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func ListBuildsForRepo(c *gin.Context) {
}

// ensure per_page isn't above or below allowed values
perPage = util.MaxInt(1, util.MinInt(100, perPage))
perPage = max(1, min(100, perPage))

// capture before query parameter if present, default to now
before, err := strconv.ParseInt(c.DefaultQuery("before", strconv.FormatInt(time.Now().UTC().Unix(), 10)), 10, 64)
Expand Down
2 changes: 1 addition & 1 deletion api/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func ListDeployments(c *gin.Context) {
}

// ensure per_page isn't above or below allowed values
perPage = util.MaxInt(1, util.MinInt(100, perPage))
perPage = max(1, min(100, perPage))

// send API call to capture the total number of deployments for the repo
t, err := database.FromContext(c).CountDeploymentsForRepo(c, r)
Expand Down
2 changes: 1 addition & 1 deletion api/hook/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func ListHooks(c *gin.Context) {
}

// ensure per_page isn't above or below allowed values
perPage = util.MaxInt(1, util.MinInt(100, perPage))
perPage = max(1, min(100, perPage))

// send API call to capture the list of steps for the build
h, t, err := database.FromContext(c).ListHooksForRepo(ctx, r, page, perPage)
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
2 changes: 1 addition & 1 deletion api/log/list_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func ListLogsForBuild(c *gin.Context) {
}

// ensure per_page isn't above or below allowed values
perPage = util.MaxInt(1, util.MinInt(100, perPage))
perPage = max(1, min(100, perPage))

// send API call to capture the list of logs for the build
bl, t, err := database.FromContext(c).ListLogsForBuild(ctx, b, page, perPage)
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
3 changes: 2 additions & 1 deletion api/pipeline/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func CompilePipeline(c *gin.Context) {
p := pMiddleware.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit())

Expand All @@ -98,7 +99,7 @@ func CompilePipeline(c *gin.Context) {
ruleData := prepareRuleData(c)

// compile the pipeline
pipeline, _, err := compiler.CompileLite(p.GetData(), ruleData, true)
pipeline, _, err := compiler.CompileLite(ctx, p.GetData(), ruleData, true)
if err != nil {
retErr := fmt.Errorf("unable to compile pipeline %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func ExpandPipeline(c *gin.Context) {
p := pipeline.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit())

Expand All @@ -98,7 +99,7 @@ func ExpandPipeline(c *gin.Context) {
ruleData := prepareRuleData(c)

// expand the templates in the pipeline
pipeline, _, err := compiler.CompileLite(p.GetData(), ruleData, false)
pipeline, _, err := compiler.CompileLite(ctx, p.GetData(), ruleData, false)
if err != nil {
retErr := fmt.Errorf("unable to expand pipeline %s: %w", entry, err)

Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func ListPipelines(c *gin.Context) {
// ensure per_page isn't above or below allowed values
//
//nolint:gomnd // ignore magic number
perPage = util.MaxInt(1, util.MinInt(100, perPage))
perPage = max(1, min(100, perPage))

p, t, err := database.FromContext(c).ListPipelinesForRepo(ctx, r, page, perPage)
if err != nil {
Expand Down
Loading

0 comments on commit 7b5a2b7

Please sign in to comment.