Skip to content

Commit 007f16c

Browse files
authored
Skip repositories being cloned in action exec instead of returning error (#173)
This fixes https://github.com/sourcegraph/sourcegraph/issues/9149 When a repository is being cloned while we execute the `scopeQuery` of an action, the GraphQL query returns something like this: { "errors": [ { "message": "repository does not exist: github.com/sourcegraphtest/alwayscloningtest", "path": [ "search", "results", "results", 2, "defaultBranch" ] } ], "data": { "search": { "results": { "results": [ { "__typename": "Repository", "id": "UmVwb3NpdG9yeTox", "name": "github.com/sourcegraph/automation-testing", "defaultBranch": { "name": "refs/heads/master", "target": { "oid": "3a0d12026c1349c4aefb712145433cc26c330a06" } } }, { "__typename": "Repository", "id": "UmVwb3NpdG9yeTozOTU=", "name": "github.com/sourcegraphtest/AlwaysCloningTest", "defaultBranch": null } ] } } } } There is an error for `AlwaysCloningTest` but `AlwaysCloningTest` is also being returned in the data, except that its `defaultBranch` is `null`. So instead of returning an error when one of the repositories is being cloned and doesn't have a default branch, we simply check for the existence of the `defaultBranch` attribute and if it's not there, we skip the repository.
1 parent c9e3eb8 commit 007f16c

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

cmd/src/actions_exec.go

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,40 +465,65 @@ query ActionRepos($query: String!) {
465465
`
466466
type Repository struct {
467467
ID, Name string
468-
DefaultBranch struct {
468+
DefaultBranch *struct {
469469
Name string
470470
Target struct{ OID string }
471471
}
472472
}
473473
var result struct {
474-
Search struct {
475-
Results struct {
476-
Results []struct {
477-
Typename string `json:"__typename"`
478-
ID, Name string
479-
DefaultBranch struct {
480-
Name string
481-
Target struct{ OID string }
474+
Data struct {
475+
Search struct {
476+
Results struct {
477+
Results []struct {
478+
Typename string `json:"__typename"`
479+
ID, Name string
480+
DefaultBranch *struct {
481+
Name string
482+
Target struct{ OID string }
483+
}
484+
Repository Repository `json:"repository"`
482485
}
483-
Repository Repository `json:"repository"`
484486
}
485487
}
486-
}
488+
} `json:"data,omitempty"`
489+
490+
Errors []struct {
491+
Message string
492+
Path []interface{}
493+
} `json:"errors,omitempty"`
487494
}
488495

489496
if err := (&apiRequest{
490497
query: query,
491498
vars: map[string]interface{}{
492499
"query": scopeQuery,
493500
},
494-
result: &result,
501+
// Do not unpack errors and return error. Instead we want to go through
502+
// the results and check whether they're complete.
503+
// If we don't do this and the query returns an error for _one_
504+
// repository because that is still cloning, we don't get any repositories.
505+
// Instead we simply want to skip those repositories that are still
506+
// being cloned.
507+
dontUnpackErrors: true,
508+
result: &result,
495509
}).do(); err != nil {
496-
return nil, nil, err
510+
511+
// Ignore exitCodeError with error == nil, because we explicitly set
512+
// dontUnpackErrors, which can lead to an empty exitCodeErr being
513+
// returned.
514+
exitCodeErr, ok := err.(*exitCodeError)
515+
if !ok {
516+
return nil, nil, err
517+
}
518+
if exitCodeErr.error != nil {
519+
return nil, nil, exitCodeErr
520+
}
497521
}
498522

499523
skipped := []string{}
500524
reposByID := map[string]ActionRepo{}
501-
for _, searchResult := range result.Search.Results.Results {
525+
for _, searchResult := range result.Data.Search.Results.Results {
526+
502527
var repo Repository
503528
if searchResult.Repository.ID != "" {
504529
repo = searchResult.Repository
@@ -510,8 +535,8 @@ query ActionRepos($query: String!) {
510535
}
511536
}
512537

513-
if repo.DefaultBranch.Name == "" {
514-
skipped = append(skipped, repo.Name)
538+
if repo.DefaultBranch == nil || repo.DefaultBranch.Name == "" {
539+
skipped = append(skipped, searchResult.Repository.Name)
515540
continue
516541
}
517542

cmd/src/cmd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ type exitCodeError struct {
124124
exitCode int
125125
}
126126

127+
func (e *exitCodeError) Error() string {
128+
if e.error != nil {
129+
return fmt.Sprintf("%s (exit code: %d)", e.error, e.exitCode)
130+
}
131+
return fmt.Sprintf("exit code: %d", e.exitCode)
132+
}
133+
127134
const (
128135
graphqlErrorsExitCode = 2
129136
)

0 commit comments

Comments
 (0)