Skip to content

Commit

Permalink
Merge pull request #92 from buildpulse/tags
Browse files Browse the repository at this point in the history
Support for adding tags to builds
  • Loading branch information
siddhantdange authored Jul 30, 2023
2 parents 3dee63d + e7df4f3 commit 893c9b1
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 12 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ To use `test-reporter` with another CI provider, the following environment varia
| `REPOSITORY_NAME` | Name of the repository |

The following are flags that can be set. Make sure to **set flags after CLI args**.
| Environment Variable | Required | Description |
|----------------------|-----------------------------------|----------------------------------------------|
| `account-id` || BuildPulse account ID (see dashboard) |
| `repository-id` || BuildPulse repository ID (see dashboard) |
| `repository-dir` | Only if `tree` not set | Path to repository directory |
| `tree` | Only if `repository-dir` not set | Git tree SHA |
| `coverage-files` | Only if using BuildPulse Coverage | **Space-separated** paths to coverage files. |
| Flag | Required | Description |
|----------------------|-----------------------------------|-------------------------------------------------|
| `account-id` || BuildPulse account ID (see dashboard) |
| `repository-id` || BuildPulse repository ID (see dashboard) |
| `repository-dir` | Only if `tree` not set | Path to repository directory |
| `tree` | Only if `repository-dir` not set | Git tree SHA |
| `coverage-files` | Only if using BuildPulse Coverage | **Space-separated** paths to coverage files. |
| `tags` | | **Space-separated** tags to apply to the build. |

Example:
```
Expand Down
1 change: 1 addition & 0 deletions cmd/test-reporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ FLAGS
--repository-dir Path to local git clone of the repository (default: ".")
--tree SHA-1 hash of the git tree that produced the test results (for use only if a local git clone does not exist)
--coverage-files Paths to coverage files or directories containing coverage files (space-separated)
--tags Tags to apply to the build (space-separated)
ENVIRONMENT VARIABLES
Set the following environment variables:
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/submit/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Submit struct {
paths []string
coveragePathsString string
coveragePaths []string
tagsString string
bucket string
accountID uint64
repositoryID uint64
Expand All @@ -97,6 +98,7 @@ func NewSubmit(version *metadata.Version, log logger.Logger) *Submit {
s.fs.StringVar(&s.tree, "tree", "", "SHA-1 hash of git tree")
s.fs.StringVar(&s.coveragePathsString, "coverage-files", "", "Paths to coverage files (space-separated)")
s.fs.BoolVar(&s.disableCoverageAutoDiscovery, "disable-coverage-auto", false, "Disables coverage file autodiscovery")
s.fs.StringVar(&s.tagsString, "tags", "", "Tags to apply to the build (space-separated)")
s.fs.SetOutput(io.Discard) // Disable automatic writing to STDERR

s.logger.Printf("Current version: %s", s.version.String())
Expand Down Expand Up @@ -156,6 +158,8 @@ func (s *Submit) Init(args []string, envs map[string]string, commitResolverFacto

if len(s.coveragePathsString) > 0 {
s.coveragePaths = strings.Split(s.coveragePathsString, " ")
} else {
s.coveragePaths = []string{}
}

id, ok := envs["BUILDPULSE_ACCESS_KEY_ID"]
Expand Down Expand Up @@ -242,7 +246,8 @@ func (s *Submit) bundle() (string, error) {
//////////////////////////////////////////////////////////////////////////////

s.logger.Printf("Gathering metadata to describe the build")
meta, err := metadata.NewMetadata(s.version, s.envs, s.commitResolver, time.Now, s.logger)
tags := strings.Split(s.tagsString, " ")
meta, err := metadata.NewMetadata(s.version, s.envs, tags, s.commitResolver, time.Now, s.logger)
if err != nil {
return "", err
}
Expand Down
51 changes: 51 additions & 0 deletions internal/cmd/submit/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestSubmit_Init(t *testing.T) {
assert.Equal(t, exampleEnv, s.envs)
assert.Equal(t, ".", s.repositoryPath)
assert.Equal(t, "Repository", s.commitResolver.Source())
assert.Equal(t, s.coveragePaths, []string{})
})

t.Run("WithCoveragePathString", func(t *testing.T) {
Expand Down Expand Up @@ -78,6 +79,22 @@ func TestSubmit_Init(t *testing.T) {
assert.True(t, s.disableCoverageAutoDiscovery)
})

t.Run("WithTagsString", func(t *testing.T) {
s := NewSubmit(&metadata.Version{}, logger.New())
err := s.Init([]string{"testdata/example-reports-dir/example-*.xml", "--account-id", "42", "--repository-id", "8675309", "--tags", "tag1 tag2"}, exampleEnv, new(stubCommitResolverFactory))
require.NoError(t, err)
assert.ElementsMatch(t, []string{"testdata/example-reports-dir/example-1.xml"}, s.paths)
assert.EqualValues(t, 42, s.accountID)
assert.EqualValues(t, 8675309, s.repositoryID)
assert.Equal(t, "buildpulse-uploads", s.bucket)
assert.Equal(t, "some-access-key-id", s.credentials.AccessKeyID)
assert.Equal(t, "some-secret-access-key", s.credentials.SecretAccessKey)
assert.Equal(t, exampleEnv, s.envs)
assert.Equal(t, ".", s.repositoryPath)
assert.Equal(t, "Repository", s.commitResolver.Source())
assert.Equal(t, s.tagsString, "tag1 tag2")
})

t.Run("WithMultiplePathArgs", func(t *testing.T) {
s := NewSubmit(&metadata.Version{}, logger.New())
err := s.Init(
Expand Down Expand Up @@ -609,6 +626,40 @@ func Test_bundle(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, string(logdata), "Gathering metadata to describe the build")
})

t.Run("bundle with tags", func(t *testing.T) {
envs := map[string]string{
"GITHUB_ACTIONS": "true",
"GITHUB_SHA": "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb",
}

log := logger.New()
s := &Submit{
logger: log,
version: &metadata.Version{Number: "v1.2.3"},
commitResolver: metadata.NewStaticCommitResolver(&metadata.Commit{TreeSHA: "ccccccccccccccccccccdddddddddddddddddddd"}, log),
envs: envs,
paths: []string{"testdata/example-reports-dir/example-1.xml"},
bucket: "buildpulse-uploads",
accountID: 42,
repositoryID: 8675309,
tagsString: "tag1 tag2",
}

path, err := s.bundle()
require.NoError(t, err)

unzipDir := t.TempDir()
err = archiver.Unarchive(path, unzipDir)
require.NoError(t, err)

// Verify buildpulse.yml is present and contains expected content
yaml, err := os.ReadFile(filepath.Join(unzipDir, "buildpulse.yml"))
require.NoError(t, err)

assert.Contains(t, string(yaml), "- tag1")
assert.Contains(t, string(yaml), "- tag2")
})
}

func Test_upload(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion internal/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Metadata struct {
RepoNameWithOwner string `yaml:":repo_name_with_owner"`
ReporterOS string `yaml:":reporter_os"`
ReporterVersion string `yaml:":reporter_version"`
Tags []string `yaml:":tags,omitempty"`
Timestamp time.Time `yaml:":timestamp"`
TreeSHA string `yaml:":tree,omitempty"`

Expand All @@ -36,7 +37,7 @@ type Metadata struct {
}

// NewMetadata creates a new Metadata instance from the given args.
func NewMetadata(version *Version, envs map[string]string, resolver CommitResolver, now func() time.Time, logger logger.Logger) (*Metadata, error) {
func NewMetadata(version *Version, envs map[string]string, tags []string, resolver CommitResolver, now func() time.Time, logger logger.Logger) (*Metadata, error) {
m := &Metadata{logger: logger}

if err := m.initProviderData(envs); err != nil {
Expand All @@ -50,6 +51,8 @@ func NewMetadata(version *Version, envs map[string]string, resolver CommitResolv
m.initTimestamp(now)
m.initVersionData(version)

m.Tags = tags

return m, nil
}

Expand Down
75 changes: 72 additions & 3 deletions internal/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestNewMetadata(t *testing.T) {
tests := []struct {
name string
envs map[string]string
tags []string
fixture string
}{
{
Expand Down Expand Up @@ -192,7 +193,7 @@ func TestNewMetadata(t *testing.T) {
)

version := &Version{Number: "v1.2.3", GoOS: "linux"}
meta, err := NewMetadata(version, tt.envs, commitResolver, now, logger.New())
meta, err := NewMetadata(version, tt.envs, tt.tags, commitResolver, now, logger.New())
assert.NoError(t, err)

yaml, err := meta.MarshalYAML()
Expand All @@ -203,7 +204,7 @@ func TestNewMetadata(t *testing.T) {
}

func TestNewMetadata_unsupportedProvider(t *testing.T) {
_, err := NewMetadata(&Version{}, map[string]string{}, newCommitResolverStub(), time.Now, logger.New())
_, err := NewMetadata(&Version{}, map[string]string{}, []string{}, newCommitResolverStub(), time.Now, logger.New())
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "missing required environment variables")
}
Expand Down Expand Up @@ -241,7 +242,7 @@ func TestNewMetadata_customCheckName(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
meta, err := NewMetadata(&Version{}, tt.envs, newCommitResolverStub(), time.Now, logger.New())
meta, err := NewMetadata(&Version{}, tt.envs, []string{}, newCommitResolverStub(), time.Now, logger.New())
assert.NoError(t, err)

yaml, err := meta.MarshalYAML()
Expand All @@ -254,3 +255,71 @@ func TestNewMetadata_customCheckName(t *testing.T) {
func newCommitResolverStub() CommitResolver {
return NewStaticCommitResolver(&Commit{}, logger.New())
}

func TestNewMetadata_appliesTags(t *testing.T) {
tests := []struct {
name string
envs map[string]string
tags []string
fixture string
}{
{
name: "GitHubActions",
envs: map[string]string{
"GITHUB_ACTIONS": "true",
"GITHUB_ACTOR": "some-user",
"GITHUB_BASE_REF": "refs/heads/main",
"GITHUB_EVENT_NAME": "push",
"GITHUB_HEAD_REF": "refs/heads/some-feature",
"GITHUB_REF": "refs/heads/some-feature",
"GITHUB_REPOSITORY": "some-owner/some-repo",
"GITHUB_RUN_ATTEMPT": "1",
"GITHUB_RUN_ID": "8675309",
"GITHUB_RUN_NUMBER": "42",
"GITHUB_SERVER_URL": "https://github.com",
"GITHUB_SHA": "1f192ff735f887dd7a25229b2ece0422d17931f5",
"GITHUB_WORKFLOW": "build",
},
fixture: "./testdata/github_tags.yml",
tags: []string{"tag1", "tag2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
now := func() time.Time {
return time.Date(2020, 7, 11, 1, 2, 3, 0, time.UTC)
}

expected, err := os.ReadFile(tt.fixture)
require.NoError(t, err)

authoredAt, err := time.Parse(time.RFC3339, "2020-07-09T04:05:06-05:00")
require.NoError(t, err)

committedAt, err := time.Parse(time.RFC3339, "2020-07-10T07:08:09+13:00")
require.NoError(t, err)

commitResolver := NewStaticCommitResolver(
&Commit{
AuthoredAt: authoredAt,
AuthorEmail: "[email protected]",
AuthorName: "Some Author",
CommittedAt: committedAt,
CommitterEmail: "[email protected]",
CommitterName: "Some Committer",
Message: "Some message",
TreeSHA: "0da9df599c02da5e7f5058b7108dcd5e1929a0fe",
},
logger.New(),
)

version := &Version{Number: "v1.2.3", GoOS: "linux"}
meta, err := NewMetadata(version, tt.envs, tt.tags, commitResolver, now, logger.New())
assert.NoError(t, err)

yaml, err := meta.MarshalYAML()
assert.NoError(t, err)
assert.Equal(t, string(expected), string(yaml))
})
}
}
31 changes: 31 additions & 0 deletions internal/metadata/testdata/github_tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
:authored_at: 2020-07-09T04:05:06-05:00
:author_email: [email protected]
:author_name: Some Author
:branch: some-feature
:build_url: https://github.com/some-owner/some-repo/actions/runs/8675309/attempts/1
:check: github-actions
:ci_provider: github-actions
:commit_message: Some message
:commit_metadata_source: Static
:commit: 1f192ff735f887dd7a25229b2ece0422d17931f5
:committed_at: 2020-07-10T07:08:09+13:00
:committer_email: [email protected]
:committer_name: Some Committer
:repo_name_with_owner: some-owner/some-repo
:reporter_os: linux
:reporter_version: v1.2.3
:tags:
- tag1
- tag2
:timestamp: 2020-07-11T01:02:03Z
:tree: 0da9df599c02da5e7f5058b7108dcd5e1929a0fe
:github_actor: some-user
:github_base_ref: refs/heads/main
:github_event_name: push
:github_head_ref: refs/heads/some-feature
:github_ref: refs/heads/some-feature
:github_repo_url: https://github.com/some-owner/some-repo
:github_run_attempt: 1
:github_run_id: 8675309
:github_run_number: 42
:github_workflow: build

0 comments on commit 893c9b1

Please sign in to comment.