Skip to content

Commit cb94349

Browse files
authored
Only yield repositories from supported codehosts (#193)
1 parent 37e75cb commit cb94349

File tree

2 files changed

+72
-37
lines changed

2 files changed

+72
-37
lines changed

cmd/src/actions_exec.go

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ Format of the action JSON files:
148148
createPatchSetFlag = flagSet.Bool("create-patchset", false, "Create a patch set from the produced set of patches. When the execution of the action fails in a single repository a prompt will ask to confirm or reject the patch set creation.")
149149
forceCreatePatchSetFlag = flagSet.Bool("force-create-patchset", false, "Force creation of patch set from the produced set of patches, without asking for confirmation even when the execution of the action failed for a subset of repositories.")
150150

151+
includeUnsupportedFlag = flagSet.Bool("include-unsupported", false, "When specified, also repos from unsupported codehosts are processed. Those can be created once the integration is done.")
152+
151153
apiFlags = newAPIFlags(flagSet)
152154
)
153155

@@ -229,14 +231,11 @@ Format of the action JSON files:
229231

230232
// Query repos over which to run action
231233
logger.Infof("Querying %s for repositories matching '%s'...\n", cfg.Endpoint, action.ScopeQuery)
232-
repos, skipped, err := actionRepos(ctx, action.ScopeQuery)
234+
repos, err := actionRepos(ctx, action.ScopeQuery, *includeUnsupportedFlag, logger)
233235
if err != nil {
234236
return err
235237
}
236-
for _, r := range skipped {
237-
logger.Infof("Skipping repository %s because we couldn't determine default branch.\n", r)
238-
}
239-
logger.Infof("%d repositories match. Use 'src actions scope-query' for help with scoping.\n", len(repos))
238+
logger.Infof("Use 'src actions scope-query' for help with scoping.\n")
240239

241240
logger.Start()
242241

@@ -397,10 +396,10 @@ type ActionRepo struct {
397396
BaseRef string
398397
}
399398

400-
func actionRepos(ctx context.Context, scopeQuery string) ([]ActionRepo, []string, error) {
399+
func actionRepos(ctx context.Context, scopeQuery string, includeUnsupported bool, logger *actionLogger) ([]ActionRepo, error) {
401400
hasCount, err := regexp.MatchString(`count:\d+`, scopeQuery)
402401
if err != nil {
403-
return nil, nil, err
402+
return nil, err
404403
}
405404

406405
if !hasCount {
@@ -414,30 +413,37 @@ query ActionRepos($query: String!) {
414413
results {
415414
__typename
416415
... on Repository {
417-
id
418-
name
419-
defaultBranch {
420-
name
421-
target { oid }
422-
}
416+
...repositoryFields
423417
}
424418
... on FileMatch {
425419
repository {
426-
id
427-
name
428-
defaultBranch {
429-
name
430-
target { oid }
431-
}
420+
...repositoryFields
432421
}
433422
}
434423
}
435424
}
436425
}
437426
}
427+
428+
fragment repositoryFields on Repository {
429+
id
430+
name
431+
externalRepository {
432+
serviceType
433+
}
434+
defaultBranch {
435+
name
436+
target {
437+
oid
438+
}
439+
}
440+
}
438441
`
439442
type Repository struct {
440-
ID, Name string
443+
ID, Name string
444+
ExternalRepository struct {
445+
ServiceType string
446+
}
441447
DefaultBranch *struct {
442448
Name string
443449
Target struct{ OID string }
@@ -448,8 +454,11 @@ query ActionRepos($query: String!) {
448454
Search struct {
449455
Results struct {
450456
Results []struct {
451-
Typename string `json:"__typename"`
452-
ID, Name string
457+
Typename string `json:"__typename"`
458+
ID, Name string
459+
ExternalRepository struct {
460+
ServiceType string
461+
}
453462
DefaultBranch *struct {
454463
Name string
455464
Target struct{ OID string }
@@ -486,14 +495,15 @@ query ActionRepos($query: String!) {
486495
// returned.
487496
exitCodeErr, ok := err.(*exitCodeError)
488497
if !ok {
489-
return nil, nil, err
498+
return nil, err
490499
}
491500
if exitCodeErr.error != nil {
492-
return nil, nil, exitCodeErr
501+
return nil, exitCodeErr
493502
}
494503
}
495504

496505
skipped := []string{}
506+
unsupported := []string{}
497507
reposByID := map[string]ActionRepo{}
498508
for _, searchResult := range result.Data.Search.Results.Results {
499509

@@ -502,14 +512,21 @@ query ActionRepos($query: String!) {
502512
repo = searchResult.Repository
503513
} else {
504514
repo = Repository{
505-
ID: searchResult.ID,
506-
Name: searchResult.Name,
507-
DefaultBranch: searchResult.DefaultBranch,
515+
ID: searchResult.ID,
516+
Name: searchResult.Name,
517+
ExternalRepository: searchResult.ExternalRepository,
518+
DefaultBranch: searchResult.DefaultBranch,
508519
}
509520
}
510521

522+
// Skip repos from unsupported code hosts but don't report them explicitly.
523+
if !includeUnsupported && strings.ToLower(repo.ExternalRepository.ServiceType) != "github" && strings.ToLower(repo.ExternalRepository.ServiceType) != "bitbucketserver" {
524+
unsupported = append(unsupported, repo.Name)
525+
continue
526+
}
527+
511528
if repo.DefaultBranch == nil || repo.DefaultBranch.Name == "" {
512-
skipped = append(skipped, searchResult.Repository.Name)
529+
skipped = append(skipped, repo.Name)
513530
continue
514531
}
515532

@@ -532,12 +549,30 @@ query ActionRepos($query: String!) {
532549
for _, repo := range reposByID {
533550
repos = append(repos, repo)
534551
}
552+
for _, r := range skipped {
553+
logger.Infof("Skipping repository %s because we couldn't determine default branch.\n", r)
554+
}
555+
for _, r := range unsupported {
556+
logger.Infof("# Skipping repository %s because it's on a not supported code host.\n", r)
557+
}
558+
matchesStr := fmt.Sprintf("%d repositories match.", len(repos))
559+
unsupportedCount := len(unsupported)
560+
if includeUnsupported {
561+
if unsupportedCount > 0 {
562+
matchesStr += fmt.Sprintf(" (Including %d on unsupported code hosts.)", unsupportedCount)
563+
}
564+
} else {
565+
if unsupportedCount > 0 {
566+
matchesStr += " (Some repositories were filtered out because their code host is not supported by campaigns. Use -include-unsupported to generate patches for them anyways.)"
567+
}
568+
}
569+
logger.Infof("%s\n", matchesStr)
535570

536571
if len(repos) == 0 && !*verbose {
537572
yellow.Fprintf(os.Stderr, "WARNING: No repositories matched by scopeQuery\n")
538573
}
539574

540-
return repos, skipped, nil
575+
return repos, nil
541576
}
542577

543578
func sumDiffStats(fileDiffs []*diff.FileDiff) diff.Stat {

cmd/src/actions_scope_query.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Examples:
3131
}
3232

3333
var (
34-
fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)")
34+
fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)")
35+
includeUnsupportedFlag = flagSet.Bool("include-unsupported", false, "When specified, also repos from unsupported codehosts are processed. Those can be created once the integration is done.")
3536
)
3637

3738
handler := func(args []string) error {
@@ -60,18 +61,17 @@ Examples:
6061

6162
if *verbose {
6263
log.Printf("# scopeQuery in action definition: %s\n", action.ScopeQuery)
64+
65+
if *includeUnsupportedFlag {
66+
log.Printf("# Including repositories on unsupported codehost.\n")
67+
}
6368
}
6469

65-
repos, skipped, err := actionRepos(ctx, action.ScopeQuery)
70+
logger := newActionLogger(*verbose, false)
71+
repos, err := actionRepos(ctx, action.ScopeQuery, *includeUnsupportedFlag, logger)
6672
if err != nil {
6773
return err
6874
}
69-
for _, r := range skipped {
70-
log.Printf("# Skipping repository %s because we couldn't determine default branch.\n", r)
71-
}
72-
if *verbose {
73-
log.Printf("# %d repositories match.", len(repos))
74-
}
7575
for _, repo := range repos {
7676
fmt.Println(repo.Name)
7777
}

0 commit comments

Comments
 (0)