diff --git a/api/build/compile_publish.go b/api/build/compile_publish.go index a4e458c7a..63db6c895 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -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, @@ -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()) @@ -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) @@ -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) @@ -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()) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 } @@ -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) diff --git a/api/build/graph.go b/api/build/graph.go index 5fb3ea1ee..7c612bdf6 100644 --- a/api/build/graph.go +++ b/api/build/graph.go @@ -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) diff --git a/api/pipeline/compile.go b/api/pipeline/compile.go index a7c24af80..5d39427b8 100644 --- a/api/pipeline/compile.go +++ b/api/pipeline/compile.go @@ -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()) @@ -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) diff --git a/api/pipeline/expand.go b/api/pipeline/expand.go index be77aba50..f50bb6c14 100644 --- a/api/pipeline/expand.go +++ b/api/pipeline/expand.go @@ -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()) @@ -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) diff --git a/api/pipeline/template.go b/api/pipeline/template.go index 8dc9742b8..cd3c50b62 100644 --- a/api/pipeline/template.go +++ b/api/pipeline/template.go @@ -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)) diff --git a/api/pipeline/validate.go b/api/pipeline/validate.go index 6ff069347..830aa80c8 100644 --- a/api/pipeline/validate.go +++ b/api/pipeline/validate.go @@ -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()) @@ -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) diff --git a/compiler/engine.go b/compiler/engine.go index 59dca1469..931cef4ad 100644 --- a/compiler/engine.go +++ b/compiler/engine.go @@ -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" @@ -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. @@ -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 @@ -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 diff --git a/compiler/native/compile.go b/compiler/native/compile.go index 0c739c310..d55207634 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -39,7 +39,7 @@ type ModifyResponse struct { } // Compile produces an executable pipeline from a yaml configuration. -func (c *client) Compile(v interface{}) (*pipeline.Build, *library.Pipeline, error) { +func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *library.Pipeline, error) { p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template)) if err != nil { return nil, nil, err @@ -75,7 +75,7 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, *library.Pipeline, err switch { case p.Metadata.RenderInline: - newPipeline, err := c.compileInline(p, c.GetTemplateDepth()) + newPipeline, err := c.compileInline(ctx, p, c.GetTemplateDepth()) if err != nil { return nil, _pipeline, err } @@ -86,19 +86,19 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, *library.Pipeline, err } if len(newPipeline.Stages) > 0 { - return c.compileStages(newPipeline, _pipeline, map[string]*yaml.Template{}, r) + return c.compileStages(ctx, newPipeline, _pipeline, map[string]*yaml.Template{}, r) } - return c.compileSteps(newPipeline, _pipeline, map[string]*yaml.Template{}, r) + return c.compileSteps(ctx, newPipeline, _pipeline, map[string]*yaml.Template{}, r) case len(p.Stages) > 0: - return c.compileStages(p, _pipeline, templates, r) + return c.compileStages(ctx, p, _pipeline, templates, r) default: - return c.compileSteps(p, _pipeline, templates, r) + return c.compileSteps(ctx, p, _pipeline, templates, r) } } // CompileLite produces a partial of an executable pipeline from a yaml configuration. -func (c *client) CompileLite(v interface{}, ruleData *pipeline.RuleData, substitute bool) (*yaml.Build, *library.Pipeline, error) { +func (c *client) CompileLite(ctx context.Context, v interface{}, ruleData *pipeline.RuleData, substitute bool) (*yaml.Build, *library.Pipeline, error) { p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template)) if err != nil { return nil, nil, err @@ -110,7 +110,7 @@ func (c *client) CompileLite(v interface{}, ruleData *pipeline.RuleData, substit _pipeline.SetType(c.repo.GetPipelineType()) if p.Metadata.RenderInline { - newPipeline, err := c.compileInline(p, c.GetTemplateDepth()) + newPipeline, err := c.compileInline(ctx, p, c.GetTemplateDepth()) if err != nil { return nil, _pipeline, err } @@ -129,7 +129,7 @@ func (c *client) CompileLite(v interface{}, ruleData *pipeline.RuleData, substit switch { case len(p.Stages) > 0: // inject the templates into the steps - p, err = c.ExpandStages(p, templates, ruleData) + p, err = c.ExpandStages(ctx, p, templates, ruleData) if err != nil { return nil, _pipeline, err } @@ -167,7 +167,7 @@ func (c *client) CompileLite(v interface{}, ruleData *pipeline.RuleData, substit case len(p.Steps) > 0: // inject the templates into the steps - p, err = c.ExpandSteps(p, templates, ruleData, c.GetTemplateDepth()) + p, err = c.ExpandSteps(ctx, p, templates, ruleData, c.GetTemplateDepth()) if err != nil { return nil, _pipeline, err } @@ -204,7 +204,7 @@ func (c *client) CompileLite(v interface{}, ruleData *pipeline.RuleData, substit } // compileInline parses and expands out inline pipelines. -func (c *client) compileInline(p *yaml.Build, depth int) (*yaml.Build, error) { +func (c *client) compileInline(ctx context.Context, p *yaml.Build, depth int) (*yaml.Build, error) { newPipeline := *p // return if max template depth has been reached @@ -215,7 +215,7 @@ func (c *client) compileInline(p *yaml.Build, depth int) (*yaml.Build, error) { } for _, template := range p.Templates { - bytes, err := c.getTemplate(template, template.Name) + bytes, err := c.getTemplate(ctx, template, template.Name) if err != nil { return nil, err } @@ -242,7 +242,7 @@ func (c *client) compileInline(p *yaml.Build, depth int) (*yaml.Build, error) { // if template parsed contains a template reference, recurse with decremented depth if len(parsed.Templates) > 0 && parsed.Metadata.RenderInline { - parsed, err = c.compileInline(parsed, depth-1) + parsed, err = c.compileInline(ctx, parsed, depth-1) if err != nil { return nil, err } @@ -299,7 +299,7 @@ func (c *client) compileInline(p *yaml.Build, depth int) (*yaml.Build, error) { } // compileSteps executes the workflow for converting a YAML pipeline into an executable struct. -func (c *client) compileSteps(p *yaml.Build, _pipeline *library.Pipeline, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*pipeline.Build, *library.Pipeline, error) { +func (c *client) compileSteps(ctx context.Context, p *yaml.Build, _pipeline *library.Pipeline, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*pipeline.Build, *library.Pipeline, error) { var err error // check if the pipeline disabled the clone @@ -318,7 +318,7 @@ func (c *client) compileSteps(p *yaml.Build, _pipeline *library.Pipeline, tmpls } // inject the templates into the steps - p, err = c.ExpandSteps(p, tmpls, r, c.GetTemplateDepth()) + p, err = c.ExpandSteps(ctx, p, tmpls, r, c.GetTemplateDepth()) if err != nil { return nil, _pipeline, err } @@ -394,7 +394,7 @@ func (c *client) compileSteps(p *yaml.Build, _pipeline *library.Pipeline, tmpls } // compileStages executes the workflow for converting a YAML pipeline into an executable struct. -func (c *client) compileStages(p *yaml.Build, _pipeline *library.Pipeline, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*pipeline.Build, *library.Pipeline, error) { +func (c *client) compileStages(ctx context.Context, p *yaml.Build, _pipeline *library.Pipeline, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*pipeline.Build, *library.Pipeline, error) { var err error // check if the pipeline disabled the clone @@ -413,7 +413,7 @@ func (c *client) compileStages(p *yaml.Build, _pipeline *library.Pipeline, tmpls } // inject the templates into the stages - p, err = c.ExpandStages(p, tmpls, r) + p, err = c.ExpandStages(ctx, p, tmpls, r) if err != nil { return nil, _pipeline, err } diff --git a/compiler/native/compile_test.go b/compiler/native/compile_test.go index 556d7c353..d400d3ce4 100644 --- a/compiler/native/compile_test.go +++ b/compiler/native/compile_test.go @@ -3,6 +3,7 @@ package native import ( + "context" "flag" "fmt" "net/http" @@ -251,7 +252,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -332,7 +333,7 @@ func TestNative_Compile_StagesPipeline_Modification(t *testing.T) { repo: &api.Repo{Name: &author}, build: &api.Build{Author: &name, Number: &number}, } - _, _, err := compiler.Compile(yaml) + _, _, err := compiler.Compile(context.Background(), yaml) if (err != nil) != tt.wantErr { t.Errorf("Compile() error = %v, wantErr %v", err, tt.wantErr) return @@ -400,7 +401,7 @@ func TestNative_Compile_StepsPipeline_Modification(t *testing.T) { repo: tt.args.repo, build: tt.args.libraryBuild, } - _, _, err := compiler.Compile(yaml) + _, _, err := compiler.Compile(context.Background(), yaml) if (err != nil) != tt.wantErr { t.Errorf("Compile() error = %v, wantErr %v", err, tt.wantErr) return @@ -589,7 +590,7 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -848,7 +849,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -1093,7 +1094,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -1214,7 +1215,7 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName(t *testi compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -1335,7 +1336,7 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName_Inline(t compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -1415,7 +1416,7 @@ func TestNative_Compile_InvalidType(t *testing.T) { compiler.WithMetadata(m) - _, _, err = compiler.Compile(invalidYaml) + _, _, err = compiler.Compile(context.Background(), invalidYaml) if err == nil { t.Error("Compile should have returned an err") } @@ -1603,7 +1604,7 @@ func TestNative_Compile_Clone(t *testing.T) { compiler.WithMetadata(m) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -1814,7 +1815,7 @@ func TestNative_Compile_Pipeline_Type(t *testing.T) { pipelineType := tt.args.pipelineType compiler.WithRepo(&api.Repo{PipelineType: &pipelineType}) - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err != nil { t.Errorf("Compile returned err: %v", err) } @@ -1853,7 +1854,7 @@ func TestNative_Compile_NoStepsorStages(t *testing.T) { compiler.repo = &api.Repo{Name: &author} compiler.build = &api.Build{Author: &name, Number: &number} - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err == nil { t.Errorf("Compile should have returned err") } @@ -1886,7 +1887,7 @@ func TestNative_Compile_StepsandStages(t *testing.T) { compiler.repo = &api.Repo{Name: &author} compiler.build = &api.Build{Author: &name, Number: &number} - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if err == nil { t.Errorf("Compile should have returned err") } @@ -2971,7 +2972,7 @@ func Test_Compile_Inline(t *testing.T) { compiler.WithRepo(&api.Repo{PipelineType: &pipelineType}) } - got, _, err := compiler.Compile(yaml) + got, _, err := compiler.Compile(context.Background(), yaml) if (err != nil) != tt.wantErr { t.Errorf("Compile() error = %v, wantErr %v", err, tt.wantErr) return @@ -3847,7 +3848,7 @@ func Test_CompileLite(t *testing.T) { t.Errorf("Reading yaml file return err: %v", err) } - got, _, err := compiler.CompileLite(yaml, tt.args.ruleData, tt.args.substitute) + got, _, err := compiler.CompileLite(context.Background(), yaml, tt.args.ruleData, tt.args.substitute) if (err != nil) != tt.wantErr { t.Errorf("CompileLite() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/compiler/native/expand.go b/compiler/native/expand.go index 365946d60..1c805ca93 100644 --- a/compiler/native/expand.go +++ b/compiler/native/expand.go @@ -3,6 +3,7 @@ package native import ( + "context" "fmt" "strings" @@ -20,7 +21,7 @@ import ( // ExpandStages injects the template for each // templated step in every stage in a yaml configuration. -func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*yaml.Build, error) { +func (c *client) ExpandStages(ctx context.Context, s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*yaml.Build, error) { if len(tmpls) == 0 { return s, nil } @@ -28,7 +29,7 @@ func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template, r // iterate through all stages for _, stage := range s.Stages { // inject the templates into the steps for the stage - p, err := c.ExpandSteps(&yaml.Build{Steps: stage.Steps, Secrets: s.Secrets, Services: s.Services, Environment: s.Environment}, tmpls, r, c.GetTemplateDepth()) + p, err := c.ExpandSteps(ctx, &yaml.Build{Steps: stage.Steps, Secrets: s.Secrets, Services: s.Services, Environment: s.Environment}, tmpls, r, c.GetTemplateDepth()) if err != nil { return nil, err } @@ -44,7 +45,9 @@ func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template, r // ExpandSteps injects the template for each // templated step in a yaml configuration. -func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData, depth int) (*yaml.Build, error) { +// +//nolint:funlen,gocyclo // ignore function length +func (c *client) ExpandSteps(ctx context.Context, s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData, depth int) (*yaml.Build, error) { if len(tmpls) == 0 { return s, nil } @@ -115,7 +118,7 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r * return s, err } - bytes, err := c.getTemplate(tmpl, step.Template.Name) + bytes, err := c.getTemplate(ctx, tmpl, step.Template.Name) if err != nil { return s, err } @@ -142,7 +145,7 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r * templates = append(templates, tmplBuild.Templates...) - tmplBuild, err = c.ExpandSteps(tmplBuild, mapFromTemplates(tmplBuild.Templates), r, depth-1) + tmplBuild, err = c.ExpandSteps(ctx, tmplBuild, mapFromTemplates(tmplBuild.Templates), r, depth-1) if err != nil { return s, err } @@ -210,7 +213,7 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r * return s, nil } -func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) { +func (c *client) getTemplate(ctx context.Context, tmpl *yaml.Template, name string) ([]byte, error) { var ( bytes []byte err error @@ -273,7 +276,7 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) { "host": src.Host, }).Tracef("Using GitHub client to pull template") - bytes, err = c.Github.Template(nil, src) + bytes, err = c.Github.Template(ctx, nil, src) if err != nil { return bytes, err } @@ -291,7 +294,7 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) { } // use private (authenticated) github instance to pull from - bytes, err = c.PrivateGithub.Template(c.user, src) + bytes, err = c.PrivateGithub.Template(ctx, c.user, src) if err != nil { return bytes, err } @@ -312,7 +315,7 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) { "path": src.Name, }).Tracef("Using GitHub client to pull template") - bytes, err = c.Github.Template(nil, src) + bytes, err = c.Github.Template(ctx, nil, src) if err != nil { return bytes, err } @@ -328,7 +331,7 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) { } // use private (authenticated) github instance to pull from - bytes, err = c.PrivateGithub.Template(c.user, src) + bytes, err = c.PrivateGithub.Template(ctx, c.user, src) if err != nil { return bytes, err } diff --git a/compiler/native/expand_test.go b/compiler/native/expand_test.go index 2f036f974..56a9889c3 100644 --- a/compiler/native/expand_test.go +++ b/compiler/native/expand_test.go @@ -3,6 +3,7 @@ package native import ( + "context" "flag" "net/http" "net/http/httptest" @@ -152,6 +153,7 @@ func TestNative_ExpandStages(t *testing.T) { compiler.PrivateGithub = nil _, err = compiler.ExpandStages( + context.Background(), &yaml.Build{ Stages: stages, Services: yaml.ServiceSlice{}, @@ -172,6 +174,7 @@ func TestNative_ExpandStages(t *testing.T) { } build, err := compiler.ExpandStages( + context.Background(), &yaml.Build{ Stages: stages, Services: yaml.ServiceSlice{}, @@ -357,7 +360,14 @@ func TestNative_ExpandSteps(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) + build, err := compiler.ExpandSteps( + context.Background(), + &yaml.Build{ + Steps: steps, + Services: yaml.ServiceSlice{}, + Environment: globalEnvironment, + }, + test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) if err != nil { t.Errorf("ExpandSteps_Type%s returned err: %v", test.name, err) } @@ -626,7 +636,13 @@ func TestNative_ExpandStepsMulti(t *testing.T) { ruledata := new(pipeline.RuleData) ruledata.Branch = "main" - build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls, ruledata, compiler.GetTemplateDepth()) + build, err := compiler.ExpandSteps(context.Background(), + &yaml.Build{ + Steps: steps, + Services: yaml.ServiceSlice{}, + Environment: raw.StringSliceMap{}, + }, + tmpls, ruledata, compiler.GetTemplateDepth()) if err != nil { t.Errorf("ExpandSteps returned err: %v", err) } @@ -717,7 +733,14 @@ func TestNative_ExpandStepsStarlark(t *testing.T) { t.Errorf("Creating new compiler returned err: %v", err) } - build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Secrets: yaml.SecretSlice{}, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) + build, err := compiler.ExpandSteps(context.Background(), + &yaml.Build{ + Steps: steps, + Secrets: yaml.SecretSlice{}, + Services: yaml.ServiceSlice{}, + Environment: raw.StringSliceMap{}, + }, + tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) if err != nil { t.Errorf("ExpandSteps returned err: %v", err) } @@ -897,7 +920,13 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment, Templates: yaml.TemplateSlice{test.tmpls["chain"]}}, test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) + build, err := compiler.ExpandSteps(context.Background(), + &yaml.Build{ + Steps: steps, Services: yaml.ServiceSlice{}, + Environment: globalEnvironment, + Templates: yaml.TemplateSlice{test.tmpls["chain"]}, + }, + test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) if err != nil { t.Errorf("ExpandSteps_Type%s returned err: %v", test.name, err) } @@ -1004,7 +1033,11 @@ func TestNative_ExpandSteps_TemplateCallTemplate_CircularFail(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - _, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) + _, err := compiler.ExpandSteps(context.Background(), + &yaml.Build{ + Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment, + }, + test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) if err == nil { t.Errorf("ExpandSteps_Type%s should have returned an error", test.name) } @@ -1091,7 +1124,13 @@ func TestNative_ExpandSteps_CallTemplateWithRenderInline(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - _, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) + _, err := compiler.ExpandSteps(context.Background(), + &yaml.Build{ + Steps: steps, + Services: yaml.ServiceSlice{}, + Environment: globalEnvironment, + }, + test.tmpls, new(pipeline.RuleData), compiler.GetTemplateDepth()) if err == nil { t.Errorf("ExpandSteps_Type%s should have returned an error", test.name) } diff --git a/compiler/native/native.go b/compiler/native/native.go index 63e526f2e..db28375e5 100644 --- a/compiler/native/native.go +++ b/compiler/native/native.go @@ -3,6 +3,7 @@ package native import ( + "context" "fmt" "time" @@ -63,7 +64,7 @@ func FromCLIContext(ctx *cli.Context) (*client, error) { } // setup github template service - github, err := setupGithub() + github, err := setupGithub(ctx.Context) if err != nil { return nil, err } @@ -92,7 +93,7 @@ func FromCLIContext(ctx *cli.Context) (*client, error) { if ctx.Bool("github-driver") { logrus.Tracef("setting up Private GitHub Client for %s", ctx.String("github-url")) // setup private github service - privGithub, err := setupPrivateGithub(ctx.String("github-url"), ctx.String("github-token")) + privGithub, err := setupPrivateGithub(ctx.Context, ctx.String("github-url"), ctx.String("github-token")) if err != nil { return nil, err } @@ -106,16 +107,16 @@ func FromCLIContext(ctx *cli.Context) (*client, error) { // setupGithub is a helper function to setup the // Github registry service from the CLI arguments. -func setupGithub() (registry.Service, error) { +func setupGithub(ctx context.Context) (registry.Service, error) { logrus.Tracef("creating %s registry client from CLI configuration", "github") - return github.New("", "") + return github.New(ctx, "", "") } // setupPrivateGithub is a helper function to setup the // Github registry service from the CLI arguments. -func setupPrivateGithub(addr, token string) (registry.Service, error) { +func setupPrivateGithub(ctx context.Context, addr, token string) (registry.Service, error) { logrus.Tracef("creating private %s registry client from CLI configuration", "github") - return github.New(addr, token) + return github.New(ctx, addr, token) } // Duplicate creates a clone of the Engine. @@ -194,9 +195,9 @@ func (c *client) WithMetadata(m *internal.Metadata) compiler.Engine { } // WithPrivateGitHub sets the private github client in the Engine. -func (c *client) WithPrivateGitHub(url, token string) compiler.Engine { +func (c *client) WithPrivateGitHub(ctx context.Context, url, token string) compiler.Engine { if len(url) != 0 && len(token) != 0 { - privGithub, _ := setupPrivateGithub(url, token) + privGithub, _ := setupPrivateGithub(ctx, url, token) c.PrivateGithub = privGithub } diff --git a/compiler/native/native_test.go b/compiler/native/native_test.go index 6c5b6d2be..86b143dbe 100644 --- a/compiler/native/native_test.go +++ b/compiler/native/native_test.go @@ -3,6 +3,7 @@ package native import ( + "context" "flag" "reflect" "testing" @@ -20,7 +21,7 @@ func TestNative_New(t *testing.T) { set := flag.NewFlagSet("test", 0) set.String("clone-image", defaultCloneImage, "doc") c := cli.NewContext(nil, set, nil) - public, _ := github.New("", "") + public, _ := github.New(context.Background(), "", "") want := &client{ Github: public, Compiler: settings.CompilerMockEmpty(), @@ -49,8 +50,8 @@ func TestNative_New_PrivateGithub(t *testing.T) { set.String("github-token", token, "doc") set.String("clone-image", defaultCloneImage, "doc") c := cli.NewContext(nil, set, nil) - public, _ := github.New("", "") - private, _ := github.New(url, token) + public, _ := github.New(context.Background(), "", "") + private, _ := github.New(context.Background(), url, token) want := &client{ Github: public, PrivateGithub: private, @@ -81,8 +82,8 @@ func TestNative_DuplicateRetainSettings(t *testing.T) { set.String("github-token", token, "doc") set.String("clone-image", defaultCloneImage, "doc") c := cli.NewContext(nil, set, nil) - public, _ := github.New("", "") - private, _ := github.New(url, token) + public, _ := github.New(context.Background(), "", "") + private, _ := github.New(context.Background(), url, token) want := &client{ Github: public, PrivateGithub: private, @@ -286,7 +287,7 @@ func TestNative_WithPrivateGitHub(t *testing.T) { set.String("clone-image", defaultCloneImage, "doc") c := cli.NewContext(nil, set, nil) - private, _ := github.New(url, token) + private, _ := github.New(context.Background(), url, token) want, _ := FromCLIContext(c) want.PrivateGithub = private @@ -297,7 +298,7 @@ func TestNative_WithPrivateGitHub(t *testing.T) { t.Errorf("Unable to create new compiler: %v", err) } - if !reflect.DeepEqual(got.WithPrivateGitHub(url, token), want) { + if !reflect.DeepEqual(got.WithPrivateGitHub(context.Background(), url, token), want) { t.Errorf("WithRepo is %v, want %v", got, want) } } diff --git a/compiler/registry/github/github.go b/compiler/registry/github/github.go index c2e6737a5..9557fefa6 100644 --- a/compiler/registry/github/github.go +++ b/compiler/registry/github/github.go @@ -26,7 +26,7 @@ type client struct { // with GitHub or a GitHub Enterprise instance. // //nolint:revive // ignore returning unexported client -func New(address, token string) (*client, error) { +func New(ctx context.Context, address, token string) (*client, error) { // create the client object c := &client{ URL: defaultURL, @@ -50,7 +50,7 @@ func New(address, token string) (*client, error) { if len(token) > 0 { // create GitHub OAuth client with user's token - gitClient = c.newClientToken(token) + gitClient = c.newClientToken(ctx, token) } // overwrite the github client @@ -60,14 +60,14 @@ func New(address, token string) (*client, error) { } // newClientToken is a helper function to return the GitHub oauth2 client. -func (c *client) newClientToken(token string) *github.Client { +func (c *client) newClientToken(ctx context.Context, token string) *github.Client { // create the token object for the client ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, ) // create the OAuth client - tc := oauth2.NewClient(context.Background(), ts) + tc := oauth2.NewClient(ctx, ts) // if c.SkipVerify { // tc.Transport.(*oauth2.Transport).Base = &http.Transport{ // Proxy: http.ProxyFromEnvironment, diff --git a/compiler/registry/github/github_test.go b/compiler/registry/github/github_test.go index 03e55f4e1..16514fde2 100644 --- a/compiler/registry/github/github_test.go +++ b/compiler/registry/github/github_test.go @@ -29,7 +29,7 @@ func TestGithub_New(t *testing.T) { } // run test - got, err := New(s.URL, "") + got, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("New returned err: %v", err) @@ -60,7 +60,7 @@ func TestGithub_NewToken(t *testing.T) { } // run test - got, err := New(s.URL, token) + got, err := New(context.Background(), s.URL, token) if err != nil { t.Errorf("New returned err: %v", err) @@ -114,7 +114,7 @@ func TestGithub_NewURL(t *testing.T) { // run tests for _, test := range tests { // run test - got, err := New(test.address, "foobar") + got, err := New(context.Background(), test.address, "foobar") if err != nil { t.Errorf("New returned err: %v", err) diff --git a/compiler/registry/github/parse_test.go b/compiler/registry/github/parse_test.go index 4019f092e..dc30648e7 100644 --- a/compiler/registry/github/parse_test.go +++ b/compiler/registry/github/parse_test.go @@ -3,6 +3,7 @@ package github import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -28,7 +29,7 @@ func TestGithub_Parse(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } @@ -60,7 +61,7 @@ func TestGithub_ParseWithBranch(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } @@ -91,7 +92,7 @@ func TestGithub_Parse_Custom(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } @@ -128,7 +129,7 @@ func TestGithub_Parse_Full(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } @@ -151,7 +152,7 @@ func TestGithub_Parse_Invalid(t *testing.T) { defer s.Close() // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } @@ -186,7 +187,7 @@ func TestGithub_Parse_Hostname(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } @@ -223,7 +224,7 @@ func TestGithub_Parse_Path(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } diff --git a/compiler/registry/github/template.go b/compiler/registry/github/template.go index 7a4543f0a..082d9747f 100644 --- a/compiler/registry/github/template.go +++ b/compiler/registry/github/template.go @@ -14,12 +14,12 @@ import ( ) // Template captures the templated pipeline configuration from the GitHub repo. -func (c *client) Template(u *api.User, s *registry.Source) ([]byte, error) { +func (c *client) Template(ctx context.Context, u *api.User, s *registry.Source) ([]byte, error) { // use default GitHub OAuth client we provide cli := c.Github if u != nil { // create GitHub OAuth client with user's token - cli = c.newClientToken(u.GetToken()) + cli = c.newClientToken(ctx, u.GetToken()) } // create the options to pass diff --git a/compiler/registry/github/template_test.go b/compiler/registry/github/template_test.go index e6b3f2696..9e1234635 100644 --- a/compiler/registry/github/template_test.go +++ b/compiler/registry/github/template_test.go @@ -3,6 +3,7 @@ package github import ( + "context" "net/http" "net/http/httptest" "os" @@ -52,12 +53,12 @@ func TestGithub_Template(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } - got, err := c.Template(u, src) + got, err := c.Template(context.Background(), u, src) if resp.Code != http.StatusOK { t.Errorf("Template returned %v, want %v", resp.Code, http.StatusOK) @@ -114,12 +115,12 @@ func TestGithub_TemplateSourceRef(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } - got, err := c.Template(u, src) + got, err := c.Template(context.Background(), u, src) if resp.Code != http.StatusOK { t.Errorf("Template returned %v, want %v", resp.Code, http.StatusOK) @@ -179,12 +180,12 @@ func TestGithub_TemplateEmptySourceRef(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } - got, err := c.Template(u, src) + got, err := c.Template(context.Background(), u, src) if resp.Code != http.StatusOK { t.Errorf("Template returned %v, want %v", resp.Code, http.StatusOK) @@ -233,12 +234,12 @@ func TestGithub_Template_BadRequest(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } - got, err := c.Template(u, src) + got, err := c.Template(context.Background(), u, src) if resp.Code != http.StatusOK { t.Errorf("Template returned %v, want %v", resp.Code, http.StatusOK) @@ -283,12 +284,12 @@ func TestGithub_Template_NotFound(t *testing.T) { } // run test - c, err := New(s.URL, "") + c, err := New(context.Background(), s.URL, "") if err != nil { t.Errorf("Creating client returned err: %v", err) } - got, err := c.Template(u, src) + got, err := c.Template(context.Background(), u, src) if resp.Code != http.StatusOK { t.Errorf("Template returned %v, want %v", resp.Code, http.StatusOK) diff --git a/compiler/registry/registry.go b/compiler/registry/registry.go index e3116f979..746e51c91 100644 --- a/compiler/registry/registry.go +++ b/compiler/registry/registry.go @@ -3,6 +3,8 @@ package registry import ( + "context" + api "github.com/go-vela/server/api/types" ) @@ -15,5 +17,5 @@ type Service interface { // Template defines a function that captures the // templated pipeline configuration from a repo. - Template(*api.User, *Source) ([]byte, error) + Template(context.Context, *api.User, *Source) ([]byte, error) } diff --git a/router/middleware/pipeline/pipeline.go b/router/middleware/pipeline/pipeline.go index 162c80ea5..e523f8d1a 100644 --- a/router/middleware/pipeline/pipeline.go +++ b/router/middleware/pipeline/pipeline.go @@ -75,7 +75,7 @@ func Establish() gin.HandlerFunc { WithCommit(p). WithMetadata(c.MustGet("metadata").(*internal.Metadata)). WithBuild(b). - Compile(config) + Compile(ctx, config) if err != nil { retErr := fmt.Errorf("unable to compile pipeline configuration for %s: %w", entry, err) diff --git a/scm/github/access.go b/scm/github/access.go index 02bebf8d0..aeaf1f467 100644 --- a/scm/github/access.go +++ b/scm/github/access.go @@ -31,7 +31,7 @@ func (c *client) OrgAccess(ctx context.Context, u *api.User, org string) (string } // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // send API call to capture org access level for user membership, _, err := client.Organizations.GetOrgMembership(ctx, *u.Name, org) @@ -67,9 +67,7 @@ func (c *client) RepoAccess(ctx context.Context, name, token, org, repo string) } // create github oauth client with the given token - // - //nolint:contextcheck // ignore context passing - client := c.newClientToken(token) + client := c.newClientToken(ctx, token) // send API call to capture repo access level for user perm, _, err := client.Repositories.GetPermissionLevel(ctx, org, repo, name) @@ -100,7 +98,7 @@ func (c *client) TeamAccess(ctx context.Context, u *api.User, org, team string) } // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(ctx, u.GetToken()) teams := []*github.Team{} // set the max per page for the options to capture the list of repos @@ -150,7 +148,7 @@ func (c *client) ListUsersTeamsForOrg(ctx context.Context, u *api.User, org stri }).Tracef("capturing %s team membership for org %s", u.GetName(), org) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(ctx, u.GetToken()) teams := []*github.Team{} // set the max per page for the options to capture the list of repos @@ -195,7 +193,7 @@ func (c *client) RepoContributor(ctx context.Context, owner *api.User, sender, o }).Tracef("capturing %s contributor status for repo %s/%s", sender, org, repo) // create GitHub OAuth client with repo owner's token - client := c.newClientToken(owner.GetToken()) + client := c.newClientToken(ctx, owner.GetToken()) // set the max per page for the options to capture the list of repos opts := github.ListContributorsOptions{ diff --git a/scm/github/authentication.go b/scm/github/authentication.go index 7140231bb..d62c2e633 100644 --- a/scm/github/authentication.go +++ b/scm/github/authentication.go @@ -21,7 +21,7 @@ func (c *client) Authorize(ctx context.Context, token string) (string, error) { c.Logger.Trace("authorizing user with token") // create GitHub OAuth client with user's token - client := c.newClientToken(token) + client := c.newClientToken(ctx, token) // send API call to capture the current user making the call u, _, err := client.Users.Get(ctx, "") @@ -56,7 +56,7 @@ func (c *client) Login(ctx context.Context, w http.ResponseWriter, r *http.Reque // Authenticate completes the authentication workflow for the session // and returns the remote user details. -func (c *client) Authenticate(ctx context.Context, w http.ResponseWriter, r *http.Request, oAuthState string) (*api.User, error) { +func (c *client) Authenticate(ctx context.Context, _ http.ResponseWriter, r *http.Request, oAuthState string) (*api.User, error) { c.Logger.Trace("authenticating user") // get the OAuth code diff --git a/scm/github/changeset.go b/scm/github/changeset.go index d23069d82..74c9846ca 100644 --- a/scm/github/changeset.go +++ b/scm/github/changeset.go @@ -21,7 +21,7 @@ func (c *client) Changeset(ctx context.Context, r *api.Repo, sha string) ([]stri }).Tracef("capturing commit changeset for %s/commit/%s", r.GetFullName(), sha) // create GitHub OAuth client with user's token - client := c.newClientToken(r.GetOwner().GetToken()) + client := c.newClientToken(ctx, r.GetOwner().GetToken()) s := []string{} // set the max per page for the options to capture the commit @@ -50,7 +50,7 @@ func (c *client) ChangesetPR(ctx context.Context, r *api.Repo, number int) ([]st }).Tracef("capturing pull request changeset for %s/pull/%d", r.GetFullName(), number) // create GitHub OAuth client with user's token - client := c.newClientToken(r.GetOwner().GetToken()) + client := c.newClientToken(ctx, r.GetOwner().GetToken()) s := []string{} f := []*github.CommitFile{} diff --git a/scm/github/deployment.go b/scm/github/deployment.go index 350dd070c..2f820f218 100644 --- a/scm/github/deployment.go +++ b/scm/github/deployment.go @@ -23,7 +23,7 @@ func (c *client) GetDeployment(ctx context.Context, u *api.User, r *api.Repo, id }).Tracef("capturing deployment %d for repo %s", id, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // send API call to capture the deployment deployment, _, err := client.Repositories.GetDeployment(ctx, r.GetOrg(), r.GetName(), id) @@ -64,7 +64,7 @@ func (c *client) GetDeploymentCount(ctx context.Context, u *api.User, r *api.Rep }).Tracef("counting deployments for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // create variable to track the deployments deployments := []*github.Deployment{} @@ -106,7 +106,7 @@ func (c *client) GetDeploymentList(ctx context.Context, u *api.User, r *api.Repo }).Tracef("listing deployments for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // set pagination options for listing deployments opts := &github.DeploymentsListOptions{ @@ -165,7 +165,7 @@ func (c *client) CreateDeployment(ctx context.Context, u *api.User, r *api.Repo, }).Tracef("creating deployment for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) var payload interface{} if d.Payload == nil { diff --git a/scm/github/github.go b/scm/github/github.go index 86bfa2c0e..caf795723 100644 --- a/scm/github/github.go +++ b/scm/github/github.go @@ -138,7 +138,7 @@ func NewTest(urls ...string) (*client, error) { } // helper function to return the GitHub OAuth client. -func (c *client) newClientToken(token string) *github.Client { +func (c *client) newClientToken(ctx context.Context, token string) *github.Client { // create the token object for the client ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, diff --git a/scm/github/github_test.go b/scm/github/github_test.go index d276717e1..90cf1fdcf 100644 --- a/scm/github/github_test.go +++ b/scm/github/github_test.go @@ -72,7 +72,7 @@ func TestGithub_newClientToken(t *testing.T) { client, _ := NewTest(s.URL) // run test - got := client.newClientToken("foobar") + got := client.newClientToken(context.Background(), "foobar") //nolint:staticcheck // ignore false positive if got == nil { diff --git a/scm/github/org.go b/scm/github/org.go index 2ac0ba714..8c9e95986 100644 --- a/scm/github/org.go +++ b/scm/github/org.go @@ -19,7 +19,7 @@ func (c *client) GetOrgName(ctx context.Context, u *api.User, o string) (string, }).Tracef("retrieving org information for %s", o) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(ctx, u.GetToken()) // send an API call to get the org info orgInfo, resp, err := client.Organizations.Get(ctx, o) diff --git a/scm/github/repo.go b/scm/github/repo.go index c75eed4b3..ad9db4271 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -56,7 +56,7 @@ func (c *client) Config(ctx context.Context, u *api.User, r *api.Repo, ref strin }).Tracef("capturing configuration file for %s/commit/%s", r.GetFullName(), ref) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // default pipeline file names files := []string{".vela.yml", ".vela.yaml"} @@ -103,7 +103,7 @@ func (c *client) Disable(ctx context.Context, u *api.User, org, name string) err }).Tracef("deleting repository webhooks for %s/%s", org, name) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // send API call to capture the hooks for the repo hooks, _, err := client.Repositories.ListHooks(ctx, org, name, nil) @@ -158,7 +158,7 @@ func (c *client) Enable(ctx context.Context, u *api.User, r *api.Repo, h *librar }).Tracef("creating repository webhook for %s/%s", r.GetOrg(), r.GetName()) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // always listen to repository events in case of repo name change events := []string{eventRepository} @@ -232,7 +232,7 @@ func (c *client) Update(ctx context.Context, u *api.User, r *api.Repo, hookID in }).Tracef("updating repository webhook for %s/%s", r.GetOrg(), r.GetName()) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // always listen to repository events in case of repo name change events := []string{eventRepository} @@ -296,7 +296,7 @@ func (c *client) Status(ctx context.Context, u *api.User, b *api.Build, org, nam } // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) context := fmt.Sprintf("%s/%s", c.config.StatusContext, b.GetEvent()) url := fmt.Sprintf("%s/%s/%s/%d", c.config.WebUIAddress, org, name, b.GetNumber()) @@ -408,7 +408,7 @@ func (c *client) StepStatus(ctx context.Context, u *api.User, b *api.Build, s *l } // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) context := fmt.Sprintf("%s/%s/%s", c.config.StatusContext, b.GetEvent(), s.GetReportAs()) url := fmt.Sprintf("%s/%s/%s/%d#%d", c.config.WebUIAddress, org, name, b.GetNumber(), s.GetNumber()) @@ -471,7 +471,7 @@ func (c *client) GetRepo(ctx context.Context, u *api.User, r *api.Repo) (*api.Re }).Tracef("retrieving repository information for %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(ctx, u.GetToken()) // send an API call to get the repo info repo, resp, err := client.Repositories.Get(ctx, r.GetOrg(), r.GetName()) @@ -491,7 +491,7 @@ func (c *client) GetOrgAndRepoName(ctx context.Context, u *api.User, o string, r }).Tracef("retrieving repository information for %s/%s", o, r) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(ctx, u.GetToken()) // send an API call to get the repo info repo, _, err := client.Repositories.Get(ctx, o, r) @@ -509,7 +509,7 @@ func (c *client) ListUserRepos(ctx context.Context, u *api.User) ([]*api.Repo, e }).Tracef("listing source repositories for %s", u.GetName()) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(ctx, u.GetToken()) r := []*github.Repository{} f := []*api.Repo{} @@ -589,7 +589,7 @@ func (c *client) GetPullRequest(ctx context.Context, r *api.Repo, number int) (s }).Tracef("retrieving pull request %d for repo %s", number, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(r.GetOwner().GetToken()) + client := c.newClientToken(ctx, r.GetOwner().GetToken()) pull, _, err := client.PullRequests.Get(ctx, r.GetOrg(), r.GetName(), number) if err != nil { @@ -613,7 +613,7 @@ func (c *client) GetHTMLURL(ctx context.Context, u *api.User, org, repo, name, r }).Tracef("capturing html_url for %s/%s/%s@%s", org, repo, name, ref) // create GitHub OAuth client with user's token - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // set the reference for the options to capture the repository contents opts := &github.RepositoryContentGetOptions{ @@ -649,7 +649,7 @@ func (c *client) GetBranch(ctx context.Context, r *api.Repo, branch string) (str }).Tracef("retrieving branch %s for repo %s", branch, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(r.GetOwner().GetToken()) + client := c.newClientToken(ctx, r.GetOwner().GetToken()) maxRedirects := 3 diff --git a/scm/github/user.go b/scm/github/user.go index e50cc19ff..d014256e4 100644 --- a/scm/github/user.go +++ b/scm/github/user.go @@ -16,7 +16,7 @@ func (c *client) GetUserID(ctx context.Context, name string, token string) (stri }).Tracef("capturing SCM user id for %s", name) // create GitHub OAuth client with user's token - client := c.newClientToken(token) + client := c.newClientToken(ctx, token) // send API call to capture user user, _, err := client.Users.Get(ctx, name) diff --git a/scm/github/webhook.go b/scm/github/webhook.go index d7a986c32..e2cd4359b 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -101,8 +101,7 @@ func (c *client) VerifyWebhook(ctx context.Context, request *http.Request, r *ap // RedeliverWebhook redelivers webhooks from GitHub. func (c *client) RedeliverWebhook(ctx context.Context, u *api.User, r *api.Repo, h *library.Hook) error { // create GitHub OAuth client with user's token - //nolint:contextcheck // do not need to pass context in this instance - client := c.newClientToken(*u.Token) + client := c.newClientToken(ctx, *u.Token) // capture the delivery ID of the hook using GitHub API deliveryID, err := c.getDeliveryID(ctx, client, r, h) diff --git a/scm/github/webhook_test.go b/scm/github/webhook_test.go index b2f8f5e98..745b176a2 100644 --- a/scm/github/webhook_test.go +++ b/scm/github/webhook_test.go @@ -1543,7 +1543,7 @@ func TestGithub_GetDeliveryID(t *testing.T) { client, _ := NewTest(s.URL, "https://foo.bar.com") - ghClient := client.newClientToken(*u.Token) + ghClient := client.newClientToken(context.Background(), *u.Token) // run test got, err := client.getDeliveryID(context.TODO(), ghClient, _repo, _hook)