Skip to content

Commit

Permalink
enhance: supply parent context to scm funcs (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Aug 19, 2024
1 parent 8a04a89 commit 8b07da9
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 153 deletions.
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
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/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func GetTemplates(c *gin.Context) {
templates[name] = template.ToLibrary()

// create a compiler registry client for parsing (no address or token needed for Parse)
registry, err := github.New("", "")
registry, err := github.New(ctx, "", "")
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("%s: unable to create compiler github client: %w", baseErr, err))

Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func ValidatePipeline(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 @@ -96,7 +97,7 @@ func ValidatePipeline(c *gin.Context) {
ruleData := prepareRuleData(c)

// validate 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 validate pipeline %s: %w", entry, err)

Expand Down
12 changes: 7 additions & 5 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package compiler

import (
"context"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/api/types/settings"
"github.com/go-vela/server/internal"
Expand All @@ -20,12 +22,12 @@ type Engine interface {
// Compile defines a function that produces an executable
// representation of a pipeline from an object. This calls
// Parse internally to convert the object to a yaml configuration.
Compile(interface{}) (*pipeline.Build, *library.Pipeline, error)
Compile(context.Context, interface{}) (*pipeline.Build, *library.Pipeline, error)

// CompileLite defines a function that produces an light executable
// representation of a pipeline from an object. This calls
// Parse internally to convert the object to a yaml configuration.
CompileLite(interface{}, *pipeline.RuleData, bool) (*yaml.Build, *library.Pipeline, error)
CompileLite(context.Context, interface{}, *pipeline.RuleData, bool) (*yaml.Build, *library.Pipeline, error)

// Duplicate defines a function that
// creates a clone of the Engine.
Expand Down Expand Up @@ -71,10 +73,10 @@ type Engine interface {

// ExpandStages defines a function that injects the template
// for each templated step in every stage in a yaml configuration.
ExpandStages(*yaml.Build, map[string]*yaml.Template, *pipeline.RuleData) (*yaml.Build, error)
ExpandStages(context.Context, *yaml.Build, map[string]*yaml.Template, *pipeline.RuleData) (*yaml.Build, error)
// ExpandSteps defines a function that injects the template
// for each templated step in a yaml configuration with the provided template depth.
ExpandSteps(*yaml.Build, map[string]*yaml.Template, *pipeline.RuleData, int) (*yaml.Build, error)
ExpandSteps(context.Context, *yaml.Build, map[string]*yaml.Template, *pipeline.RuleData, int) (*yaml.Build, error)

// Init Compiler Interface Functions

Expand Down Expand Up @@ -147,7 +149,7 @@ type Engine interface {
WithLabels([]string) Engine
// WithPrivateGitHub defines a function that sets
// the private github client in the Engine.
WithPrivateGitHub(string, string) Engine
WithPrivateGitHub(context.Context, string, string) Engine
// GetSettings defines a function that returns new api settings
// with the compiler Engine fields filled.
GetSettings() settings.Compiler
Expand Down
Loading

0 comments on commit 8b07da9

Please sign in to comment.