Skip to content

Commit

Permalink
🐛 fix github provider by nil errors
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Dec 18, 2024
1 parent 9e11410 commit cf83344
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 3 additions & 1 deletion internal/workerpool/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func (c *collector[R]) GetValues() (slice []R) {
func (c *collector[R]) GetErrors() (slice []error) {
results := c.GetResults()
for i := range results {
slice = append(slice, results[i].Error)
if results[i].Error != nil {
slice = append(slice, results[i].Error)
}
}
return
}
34 changes: 33 additions & 1 deletion internal/workerpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ func TestPoolHandleErrors(t *testing.T) {
}
}

func TestPoolMultipleTasksWithEmptyResultsAndNilErrors(t *testing.T) {
type test struct {
data int
}
pool := workerpool.New[*test](5)
pool.Start()
defer pool.Close()

tasks := []workerpool.Task[*test]{
func() (*test, error) { return nil, nil },
func() (*test, error) { return &test{1}, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
func() (*test, error) { return nil, nil },
}

for _, task := range tasks {
pool.Submit(task)
}

// Wait for error collector to process
pool.Wait()

assert.Empty(t, pool.GetErrors())
}

func TestPoolMultipleTasksWithErrors(t *testing.T) {
type test struct {
data int
Expand Down Expand Up @@ -102,7 +134,7 @@ func TestPoolMultipleTasksWithErrors(t *testing.T) {
pool.GetValues(),
)
assert.ElementsMatch(t,
[]error{nil, nil, errors.New("task error"), nil},
[]error{errors.New("task error")},
pool.GetErrors(),
)
}
Expand Down
9 changes: 5 additions & 4 deletions providers/github/resources/github_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,12 @@ func (g *mqlGithubOrganization) repositories() ([]interface{}, error) {

// check if any request failed
if errs := workerPool.GetErrors(); len(errs) != 0 {
err := errors.Join(errs...)
if strings.Contains(err.Error(), "404") {
return nil, nil
if err := errors.Join(errs...); err != nil {
if strings.Contains(err.Error(), "404") {
return nil, nil
}
return nil, err
}
return nil, err
}
}

Expand Down

0 comments on commit cf83344

Please sign in to comment.