Skip to content

Commit

Permalink
Add pipeline run name to ctxt
Browse files Browse the repository at this point in the history
Closes #737.
  • Loading branch information
michaelsauter committed Nov 3, 2023
1 parent f7b51c3 commit 77a26da
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion cmd/artifact-download/artifact_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/tasktesting/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
24 changes: 15 additions & 9 deletions pkg/pipelinectxt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ODSContext struct {
GitURL string
PullRequestBase string
PullRequestKey string
PipelineRun string
}

// WriteCache writes the ODS context to .ods
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/pipelinectxt/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit 77a26da

Please sign in to comment.