Skip to content

Commit 19718ea

Browse files
authored
api: move GraphQL functionality to a new package (#258)
1 parent 79c45f5 commit 19718ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+919
-859
lines changed

cmd/src/actions_exec.go

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/ghodss/yaml"
2222
"github.com/mattn/go-isatty"
2323
"github.com/pkg/errors"
24+
"github.com/sourcegraph/src-cli/internal/api"
2425
"github.com/sourcegraph/src-cli/internal/campaigns"
2526
)
2627

@@ -123,7 +124,7 @@ Format of the action JSON files:
123124

124125
includeUnsupportedFlag = flagSet.Bool("include-unsupported", false, "When specified, also repos from unsupported codehosts are processed. Those can be created once the integration is done.")
125126

126-
apiFlags = newAPIFlags(flagSet)
127+
apiFlags = api.NewFlags(flagSet)
127128
)
128129

129130
handler := func(args []string) error {
@@ -212,6 +213,7 @@ Format of the action JSON files:
212213
os.Exit(2)
213214
}()
214215

216+
client := cfg.apiClient(apiFlags, flagSet.Output())
215217
logger := campaigns.NewActionLogger(*verbose, *keepLogsFlag)
216218

217219
// Fetch Docker images etc.
@@ -232,7 +234,7 @@ Format of the action JSON files:
232234

233235
// Query repos over which to run action
234236
logger.Infof("Querying %s for repositories matching '%s'...\n", cfg.Endpoint, action.ScopeQuery)
235-
repos, err := actionRepos(ctx, action.ScopeQuery, *includeUnsupportedFlag, logger)
237+
repos, err := actionRepos(ctx, client, action.ScopeQuery, *includeUnsupportedFlag, logger)
236238
if err != nil {
237239
return err
238240
}
@@ -310,7 +312,7 @@ Format of the action JSON files:
310312
return err
311313
}
312314

313-
return createPatchSetFromPatches(apiFlags, patches, tmpl, 100)
315+
return createPatchSetFromPatches(ctx, client, patches, tmpl, 100)
314316
}
315317

316318
// Register the command.
@@ -321,7 +323,7 @@ Format of the action JSON files:
321323
})
322324
}
323325

324-
func actionRepos(ctx context.Context, scopeQuery string, includeUnsupported bool, logger *campaigns.ActionLogger) ([]campaigns.ActionRepo, error) {
326+
func actionRepos(ctx context.Context, client api.Client, scopeQuery string, includeUnsupported bool, logger *campaigns.ActionLogger) ([]campaigns.ActionRepo, error) {
325327
hasCount, err := regexp.MatchString(`count:\d+`, scopeQuery)
326328
if err != nil {
327329
return nil, err
@@ -403,31 +405,13 @@ fragment repositoryFields on Repository {
403405
} `json:"errors,omitempty"`
404406
}
405407

406-
if err := (&apiRequest{
407-
query: query,
408-
vars: map[string]interface{}{
409-
"query": scopeQuery,
410-
},
411-
// Do not unpack errors and return error. Instead we want to go through
412-
// the results and check whether they're complete.
413-
// If we don't do this and the query returns an error for _one_
414-
// repository because that is still cloning, we don't get any repositories.
415-
// Instead we simply want to skip those repositories that are still
416-
// being cloned.
417-
dontUnpackErrors: true,
418-
result: &result,
419-
}).do(); err != nil {
420-
421-
// Ignore exitCodeError with error == nil, because we explicitly set
422-
// dontUnpackErrors, which can lead to an empty exitCodeErr being
423-
// returned.
424-
exitCodeErr, ok := err.(*exitCodeError)
425-
if !ok {
426-
return nil, err
427-
}
428-
if exitCodeErr.error != nil {
429-
return nil, exitCodeErr
430-
}
408+
ok, err := client.NewRequest(query, map[string]interface{}{
409+
"query": scopeQuery,
410+
}).DoRaw(ctx, &result)
411+
if err != nil {
412+
return nil, err
413+
} else if !ok {
414+
return nil, nil
431415
}
432416

433417
skipped := []string{}
@@ -449,7 +433,7 @@ fragment repositoryFields on Repository {
449433

450434
// Skip repos from unsupported code hosts but don't report them explicitly.
451435
if !includeUnsupported {
452-
ok, err := isCodeHostSupportedForCampaigns(repo.ExternalRepository.ServiceType)
436+
ok, err := isCodeHostSupportedForCampaigns(ctx, client, repo.ExternalRepository.ServiceType)
453437
if err != nil {
454438
return nil, errors.Wrap(err, "failed code host check")
455439
}
@@ -541,11 +525,13 @@ var codeHostCampaignVersions = map[string]*minimumVersionDate{
541525
},
542526
}
543527

544-
func isCodeHostSupportedForCampaigns(kind string) (bool, error) {
528+
func isCodeHostSupportedForCampaigns(ctx context.Context, client api.Client, kind string) (bool, error) {
545529
// TODO(LawnGnome): this is a temporary hack; I intend to improve our
546530
// testing story including mocking requests to Sourcegraph as part of
547531
// https://github.com/sourcegraph/sourcegraph/issues/12333
548-
return isCodeHostSupportedForCampaignsImpl(kind, getSourcegraphVersion)
532+
return isCodeHostSupportedForCampaignsImpl(kind, func() (string, error) {
533+
return getSourcegraphVersion(ctx, client)
534+
})
549535
}
550536

551537
func isCodeHostSupportedForCampaignsImpl(kind string, getVersion func() (string, error)) (bool, error) {

cmd/src/actions_scope_query.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/ghodss/yaml"
1212
"github.com/pkg/errors"
13+
"github.com/sourcegraph/src-cli/internal/api"
1314
"github.com/sourcegraph/src-cli/internal/campaigns"
1415
)
1516

@@ -35,6 +36,7 @@ Examples:
3536
var (
3637
fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)")
3738
includeUnsupportedFlag = flagSet.Bool("include-unsupported", false, "When specified, also repos from unsupported codehosts are processed. Those can be created once the integration is done.")
39+
apiFlags = api.NewFlags(flagSet)
3840
)
3941

4042
handler := func(args []string) error {
@@ -71,6 +73,7 @@ Examples:
7173
}
7274

7375
ctx := context.Background()
76+
client := cfg.apiClient(apiFlags, flagSet.Output())
7477

7578
if *verbose {
7679
log.Printf("# scopeQuery in action definition: %s\n", action.ScopeQuery)
@@ -81,7 +84,7 @@ Examples:
8184
}
8285

8386
logger := campaigns.NewActionLogger(*verbose, false)
84-
repos, err := actionRepos(ctx, action.ScopeQuery, *includeUnsupportedFlag, logger)
87+
repos, err := actionRepos(ctx, client, action.ScopeQuery, *includeUnsupportedFlag, logger)
8588
if err != nil {
8689
return err
8790
}

0 commit comments

Comments
 (0)