Skip to content

Commit

Permalink
fix: eventual race condition in artifacts (#3310)
Browse files Browse the repository at this point in the history
* fix: eventual race condition in artifacts

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: locks

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Aug 16, 2022
1 parent bb4bbde commit c83663c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
10 changes: 6 additions & 4 deletions internal/artifact/artifact.go
Expand Up @@ -268,13 +268,15 @@ func New() Artifacts {

// List return the actual list of artifacts.
func (artifacts Artifacts) List() []*Artifact {
artifacts.lock.Lock()
defer artifacts.lock.Unlock()
return artifacts.items
}

// GroupByID groups the artifacts by their ID.
func (artifacts Artifacts) GroupByID() map[string][]*Artifact {
result := map[string][]*Artifact{}
for _, a := range artifacts.items {
for _, a := range artifacts.List() {
id := a.ID()
if id == "" {
continue
Expand All @@ -287,7 +289,7 @@ func (artifacts Artifacts) GroupByID() map[string][]*Artifact {
// GroupByPlatform groups the artifacts by their platform.
func (artifacts Artifacts) GroupByPlatform() map[string][]*Artifact {
result := map[string][]*Artifact{}
for _, a := range artifacts.items {
for _, a := range artifacts.List() {
plat := a.Goos + a.Goarch + a.Goarm + a.Gomips + a.Goamd64
result[plat] = append(result[plat], a)
}
Expand Down Expand Up @@ -485,7 +487,7 @@ func (artifacts *Artifacts) Filter(filter Filter) Artifacts {
}

result := New()
for _, a := range artifacts.items {
for _, a := range artifacts.List() {
if filter(a) {
result.items = append(result.items, a)
}
Expand All @@ -507,7 +509,7 @@ type VisitFn func(a *Artifact) error

// Visit executes the given function for each artifact in the list.
func (artifacts Artifacts) Visit(fn VisitFn) error {
for _, artifact := range artifacts.items {
for _, artifact := range artifacts.List() {
if err := fn(artifact); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/pipe/aur/aur_test.go
Expand Up @@ -465,13 +465,13 @@ func TestRunPipe(t *testing.T) {
}

func TestRunPipeNoBuilds(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ctx := context.New(
config.Project{
ProjectName: "foo",
AURs: []config.AUR{{}},
},
}
)
ctx.TokenType = context.TokenTypeGitHub
client := client.NewMock()
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
Expand Down
8 changes: 4 additions & 4 deletions internal/pipe/brew/brew_test.go
Expand Up @@ -747,9 +747,8 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
}

func TestRunPipeNoBuilds(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ctx := context.New(
config.Project{
Brews: []config.Homebrew{
{
Tap: config.RepoRef{
Expand All @@ -759,7 +758,8 @@ func TestRunPipeNoBuilds(t *testing.T) {
},
},
},
}
)
ctx.TokenType = context.TokenTypeGitHub
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
Expand Down
6 changes: 3 additions & 3 deletions internal/pipe/docker/docker_test.go
Expand Up @@ -1252,13 +1252,13 @@ func TestDefaultDockerfile(t *testing.T) {
}

func TestDraftRelease(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
ctx := context.New(
config.Project{
Release: config.Release{
Draft: true,
},
},
}
)

require.False(t, pipe.IsSkip(Pipe{}.Publish(ctx)))
}
Expand Down
8 changes: 4 additions & 4 deletions internal/pipe/krew/krew_test.go
Expand Up @@ -731,9 +731,8 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
}

func TestRunPipeNoBuilds(t *testing.T) {
ctx := &context.Context{
TokenType: context.TokenTypeGitHub,
Config: config.Project{
ctx := context.New(
config.Project{
Krews: []config.Krew{
{
Name: manifestName(t),
Expand All @@ -746,7 +745,8 @@ func TestRunPipeNoBuilds(t *testing.T) {
},
},
},
}
)
ctx.TokenType = context.TokenTypeGitHub
client := client.NewMock()
require.Equal(t, ErrNoArchivesFound, runAll(ctx, client))
require.False(t, client.CreatedFile)
Expand Down

0 comments on commit c83663c

Please sign in to comment.