From 77a26daa3e0d3ed4b852f596bd58b141c06df914 Mon Sep 17 00:00:00 2001 From: Michael Sauter Date: Fri, 3 Nov 2023 14:05:50 +0100 Subject: [PATCH] Add pipeline run name to ctxt Closes #737. --- CHANGELOG.md | 4 ++++ cmd/artifact-download/artifact_download.go | 2 +- cmd/start/main.go | 2 +- internal/tasktesting/git.go | 3 ++- pkg/pipelinectxt/context.go | 24 ++++++++++++++-------- pkg/pipelinectxt/context_test.go | 3 ++- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c2fbebe..94b96487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ listed in the changelog. ## [Unreleased] +### Added + +- Add PipelineRun name to ODS context ([#737](https://github.com/opendevstack/ods-pipeline/issues/737)) + ### Fixed - Reading username / password did not work when the install script was piped into bash. See [#733](https://github.com/opendevstack/ods-pipeline/pull/733). diff --git a/cmd/artifact-download/artifact_download.go b/cmd/artifact-download/artifact_download.go index b7575a9c..b1f04f07 100644 --- a/cmd/artifact-download/artifact_download.go +++ b/cmd/artifact-download/artifact_download.go @@ -121,6 +121,6 @@ func assembleODSContext(namespace string, workingDir string) (*pipelinectxt.ODSC ctxt := &pipelinectxt.ODSContext{ Namespace: namespace, } - err := ctxt.Assemble(workingDir) + err := ctxt.Assemble(workingDir, "") return ctxt, err } diff --git a/cmd/start/main.go b/cmd/start/main.go index b86ce1fb..989ef6f5 100644 --- a/cmd/start/main.go +++ b/cmd/start/main.go @@ -345,7 +345,7 @@ func checkoutAndAssembleContext( ctxt = baseCtxt.Copy() ctxt.GitFullRef = gitFullRef ctxt.GitCommitSHA = sha - err = ctxt.Assemble(absCheckoutDir) + err = ctxt.Assemble(absCheckoutDir, opts.pipelineRunName) if err != nil { return nil, fmt.Errorf("assemble ODS context: %w", err) } diff --git a/internal/tasktesting/git.go b/internal/tasktesting/git.go index 0ced4cf5..41e0b23b 100644 --- a/internal/tasktesting/git.go +++ b/internal/tasktesting/git.go @@ -34,6 +34,7 @@ func SetupFakeRepo(t *testing.T, ns, wsDir string) *pipelinectxt.ODSContext { GitFullRef: "refs/heads/master", GitRef: "master", GitURL: "http://bitbucket.acme.org/scm/myproject/myrepo.git", + PipelineRun: "foo-n8j4k", } err := ctxt.WriteCache(wsDir) if err != nil { @@ -43,7 +44,7 @@ func SetupFakeRepo(t *testing.T, ns, wsDir string) *pipelinectxt.ODSContext { } func assembleAndCacheOdsCtxtOrFatal(t *testing.T, ctxt *pipelinectxt.ODSContext, wsDir string) { - err := ctxt.Assemble(wsDir) + err := ctxt.Assemble(wsDir, "") if err != nil { t.Fatalf("could not assemble ODS context information: %s", err) } diff --git a/pkg/pipelinectxt/context.go b/pkg/pipelinectxt/context.go index 1d11a088..5988ca23 100644 --- a/pkg/pipelinectxt/context.go +++ b/pkg/pipelinectxt/context.go @@ -28,6 +28,7 @@ type ODSContext struct { GitURL string PullRequestBase string PullRequestKey string + PipelineRun string } // WriteCache writes the ODS context to .ods @@ -50,6 +51,7 @@ func (o *ODSContext) WriteCache(wsDir string) error { BaseDir + "/namespace": o.Namespace, BaseDir + "/pr-base": o.PullRequestBase, BaseDir + "/pr-key": o.PullRequestKey, + BaseDir + "/pipelinerun": o.PipelineRun, } for filename, content := range files { err := writeFile(filepath.Join(wsDir, filename), content) @@ -80,6 +82,7 @@ func (o *ODSContext) ReadCache(wsDir string) error { BaseDir + "/namespace": &o.Namespace, BaseDir + "/pr-base": &o.PullRequestBase, BaseDir + "/pr-key": &o.PullRequestKey, + BaseDir + "/pipelinerun": &o.PipelineRun, } for filename, content := range files { if len(*content) == 0 { @@ -96,36 +99,36 @@ func (o *ODSContext) ReadCache(wsDir string) error { // Assemble builds an ODS context based on given wsDir directory. // The information is gathered from the .git directory. -func (o *ODSContext) Assemble(wsDir string) error { - if len(o.Namespace) == 0 { +func (o *ODSContext) Assemble(wsDir, pipelineRun string) error { + if o.Namespace == "" { ns, err := getTrimmedFileContent(namespaceFile) if err != nil { return fmt.Errorf("could not read %s: %w", namespaceFile, err) } o.Namespace = ns } - if len(o.GitFullRef) == 0 { + if o.GitFullRef == "" { gitHead, err := getTrimmedFileContent(filepath.Join(wsDir, ".git/HEAD")) if err != nil { return fmt.Errorf("could not read .git/HEAD: %w", err) } o.GitFullRef = strings.TrimPrefix(gitHead, "ref: ") } - if len(o.GitRef) == 0 { + if o.GitRef == "" { gitFullRefParts := strings.SplitN(o.GitFullRef, "/", 3) if len(gitFullRefParts) != 3 { return fmt.Errorf("cannot extract git ref from .git/HEAD: %s", o.GitFullRef) } o.GitRef = gitFullRefParts[2] } - if len(o.GitURL) == 0 { + if o.GitURL == "" { gitURL, err := readRemoteOriginURL(filepath.Join(wsDir, ".git/config")) if err != nil { return fmt.Errorf("could not get remote origin URL: %w", err) } o.GitURL = gitURL } - if len(o.GitCommitSHA) == 0 { + if o.GitCommitSHA == "" { gitSHA, err := getTrimmedFileContent(filepath.Join(wsDir, ".git", o.GitFullRef)) if err != nil { return fmt.Errorf("could not read .git/%s: %w", o.GitFullRef, err) @@ -139,15 +142,18 @@ func (o *ODSContext) Assemble(wsDir string) error { pathParts := strings.Split(u.Path, "/") organisation := pathParts[len(pathParts)-2] repository := pathParts[len(pathParts)-1] - if len(o.Project) == 0 { + if o.Project == "" { o.Project = strings.ToLower(organisation) } - if len(o.Repository) == 0 { + if o.Repository == "" { o.Repository = filenameWithoutExtension(repository) } - if len(o.Component) == 0 { + if o.Component == "" { o.Component = strings.TrimPrefix(o.Repository, o.Project+"-") } + if o.PipelineRun == "" { + o.PipelineRun = pipelineRun + } return nil } diff --git a/pkg/pipelinectxt/context_test.go b/pkg/pipelinectxt/context_test.go index 72a4a95a..40d66d42 100644 --- a/pkg/pipelinectxt/context_test.go +++ b/pkg/pipelinectxt/context_test.go @@ -22,7 +22,7 @@ func TestAssemble(t *testing.T) { } defer cleanup() - err = c.Assemble(dir) + err = c.Assemble(dir, "foo-n8j4k") if err != nil { t.Fatal(err) } @@ -37,6 +37,7 @@ func TestAssemble(t *testing.T) { GitURL: "https://example.bitbucket.com/scm/ODS/ods-pipeline.git", PullRequestBase: "", PullRequestKey: "", + PipelineRun: "foo-n8j4k", } if diff := cmp.Diff(wantContext, c); diff != "" { t.Fatalf("context mismatch (-want +got):\n%s", diff)