Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds branchName in buildrun results if revision is not specified #933

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmd/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type settings struct {
target string
resultFileCommitSha string
resultFileCommitAuthor string
resultFileBranchName string
secretPath string
skipValidation bool
}
Expand All @@ -73,6 +74,7 @@ func init() {
pflag.StringVar(&flagValues.target, "target", "", "The target directory of the clone operation")
pflag.StringVar(&flagValues.resultFileCommitSha, "result-file-commit-sha", "", "A file to write the commit sha to.")
pflag.StringVar(&flagValues.resultFileCommitAuthor, "result-file-commit-author", "", "A file to write the commit author to.")
pflag.StringVar(&flagValues.resultFileBranchName, "result-file-branch-name", "", "A file to write the branch name to.")
pflag.StringVar(&flagValues.secretPath, "secret-path", "", "A directory that contains a secret. Either username and password for basic authentication. Or a SSH private key and optionally a known hosts file. Optional.")

// Optional flag to be able to override the default shallow clone depth,
Expand Down Expand Up @@ -156,6 +158,17 @@ func runGitClone(ctx context.Context) error {
}
}

if strings.TrimSpace(flagValues.revision) == "" && strings.TrimSpace(flagValues.resultFileBranchName) != "" {
output, err := git(ctx, "-C", flagValues.target, "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return err
}

if err := ioutil.WriteFile(flagValues.resultFileBranchName, []byte(output), 0644); err != nil {
return err
}
}

return nil
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/git/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,20 @@ var _ = Describe("Git Resource", func() {
})
})
})

It("should store branch-name into file specified in --result-file-branch-name flag", func() {
withTempFile("branch-name", func(filename string) {

withTempDir(func(target string) {
Expect(run(
"--url", exampleRepo,
"--target", target,
"--result-file-branch-name", filename,
)).ToNot(HaveOccurred())

Expect(filecontent(filename)).To(Equal("main"))
})
})
})
})
})
3 changes: 3 additions & 0 deletions deploy/crds/shipwright.io_buildruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ spec:
git:
description: Git holds the results emitted from from the step definition of a git source
properties:
branchName:
description: BranchName holds the default branch name of the git source this will be set only when revision is not specified in Build object
type: string
commitAuthor:
description: CommitAuthor holds the commit author of a git source
type: string
Expand Down
3 changes: 2 additions & 1 deletion docs/buildrun.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ After the successful completion of a `BuildRun`, the `.status` field contains th
The results from the source step will be surfaced to the `.status.sources` and the results from
the [output step](buildstrategies.md#system-results) will be surfaced to the `.status.output` field of a `BuildRun`.

Example of a `BuildRun` with surfaced results for `git` source:
Example of a `BuildRun` with surfaced results for `git` source (note that the `branchName` is only included if the Build does not specify any `revision`):

```yaml
# [...]
Expand All @@ -293,6 +293,7 @@ status:
git:
commitAuthor: xxx xxxxxx
commitSha: f25822b85021d02059c9ac8a211ef3804ea8fdde
branchName: main
sm43 marked this conversation as resolved.
Show resolved Hide resolved
```

Another example of a `BuildRun` with surfaced results for local source code(`bundle`) source:
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/build/v1alpha1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ type GitSourceResult struct {

// CommitAuthor holds the commit author of a git source
CommitAuthor string `json:"commitAuthor,omitempty"`

// BranchName holds the default branch name of the git source
// this will be set only when revision is not specified in Build object
BranchName string `json:"branchName,omitempty"`
}

// Output holds the results emitted from the output step (build-and-push)
Expand Down
10 changes: 9 additions & 1 deletion pkg/reconciler/buildrun/resources/sources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
commitSHAResult = "commit-sha"
commitAuthorResult = "commit-author"
branchName = "branch-name"
)

// AppendGitStep appends the Git step and results and volume if needed to the TaskSpec
Expand All @@ -33,6 +34,9 @@ func AppendGitStep(
}, tektonv1beta1.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitAuthorResult),
Description: "The author of the last commit of the cloned source.",
}, tektonv1beta1.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, branchName),
Description: "The name of the branch used of the cloned source.",
})

// initialize the step from the template
Expand All @@ -51,6 +55,8 @@ func AppendGitStep(
fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitSHAResult),
"--result-file-commit-author",
fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitAuthorResult),
"--result-file-branch-name",
fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, branchName),
}

// Check if a revision is defined
Expand Down Expand Up @@ -92,13 +98,15 @@ func AppendGitStep(
func AppendGitResult(buildRun *buildv1alpha1.BuildRun, name string, results []tektonv1beta1.TaskRunResult) {
commitAuthor := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitAuthorResult))
commitSha := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitSHAResult))
branchName := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, branchName))

if strings.TrimSpace(commitAuthor) != "" || strings.TrimSpace(commitSha) != "" {
if strings.TrimSpace(commitAuthor) != "" || strings.TrimSpace(commitSha) != "" || strings.TrimSpace(branchName) != "" {
SaschaSchwarze0 marked this conversation as resolved.
Show resolved Hide resolved
buildRun.Status.Sources = append(buildRun.Status.Sources, buildv1alpha1.SourceResult{
Name: name,
Git: &buildv1alpha1.GitSourceResult{
CommitAuthor: commitAuthor,
CommitSha: commitSha,
BranchName: branchName,
},
})
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/reconciler/buildrun/resources/sources/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ var _ = Describe("Git", func() {
}, "default")
})

It("adds results for the commit sha and commit author", func() {
Expect(len(taskSpec.Results)).To(Equal(2))
It("adds results for the commit sha, commit author and branch name", func() {
Expect(len(taskSpec.Results)).To(Equal(3))
Expect(taskSpec.Results[0].Name).To(Equal("shp-source-default-commit-sha"))
Expect(taskSpec.Results[1].Name).To(Equal("shp-source-default-commit-author"))
Expect(taskSpec.Results[2].Name).To(Equal("shp-source-default-branch-name"))
})

It("adds a step", func() {
Expand All @@ -53,6 +54,8 @@ var _ = Describe("Git", func() {
"$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author",
"$(results.shp-source-default-commit-author.path)",
"--result-file-branch-name",
"$(results.shp-source-default-branch-name.path)",
}))
})
})
Expand All @@ -74,10 +77,11 @@ var _ = Describe("Git", func() {
}, "default")
})

It("adds results for the commit sha and commit author", func() {
Expect(len(taskSpec.Results)).To(Equal(2))
It("adds results for the commit sha, commit author and branch name", func() {
Expect(len(taskSpec.Results)).To(Equal(3))
Expect(taskSpec.Results[0].Name).To(Equal("shp-source-default-commit-sha"))
Expect(taskSpec.Results[1].Name).To(Equal("shp-source-default-commit-author"))
Expect(taskSpec.Results[2].Name).To(Equal("shp-source-default-branch-name"))
})

It("adds a volume for the secret", func() {
Expand All @@ -100,6 +104,8 @@ var _ = Describe("Git", func() {
"$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author",
"$(results.shp-source-default-commit-author.path)",
"--result-file-branch-name",
"$(results.shp-source-default-branch-name.path)",
"--secret-path",
"/workspace/shp-source-secret",
}))
Expand Down
4 changes: 3 additions & 1 deletion pkg/reconciler/buildrun/resources/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -92,6 +92,8 @@ var _ = Describe("GenerateTaskrun", func() {
"$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author",
"$(results.shp-source-default-commit-author.path)",
"--result-file-branch-name",
"$(results.shp-source-default-branch-name.path)",
}))
})

Expand Down
2 changes: 2 additions & 0 deletions test/e2e/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func validateBuildRunResultsFromGitSource(testBuildRun *buildv1alpha1.BuildRun)
Expect(result.Value).To(Equal(testBuildRun.Status.Sources[0].Git.CommitSha))
case "shp-source-default-commit-author":
Expect(result.Value).To(Equal(testBuildRun.Status.Sources[0].Git.CommitAuthor))
case "shp-source-default-branch-name":
Expect(result.Value).To(Equal(testBuildRun.Status.Sources[0].Git.BranchName))
case "shp-image-digest":
Expect(result.Value).To(Equal(testBuildRun.Status.Output.Digest))
case "shp-image-size":
Expand Down