-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathservice.go
915 lines (782 loc) · 24.9 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
package service
import (
"context"
"encoding/json"
"fmt"
"html/template"
"os"
"os/exec"
"path"
"strings"
"sync"
"time"
"github.com/grafana/regexp"
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
onlib "github.com/sourcegraph/sourcegraph/lib/batches/on"
templatelib "github.com/sourcegraph/sourcegraph/lib/batches/template"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/docker"
"github.com/sourcegraph/src-cli/internal/batches/executor"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
"github.com/sourcegraph/src-cli/internal/batches/repozip"
"github.com/sourcegraph/src-cli/internal/retrier"
)
type Service struct {
allowUnsupported bool
allowIgnored bool
client api.Client
features batches.FeatureFlags
imageCache docker.ImageCache
}
type Opts struct {
AllowUnsupported bool
AllowIgnored bool
Client api.Client
}
var (
ErrMalformedOnQueryOrRepository = errors.New("malformed 'on' field; missing either a repository name or a query")
)
func New(opts *Opts) *Service {
return &Service{
allowUnsupported: opts.AllowUnsupported,
allowIgnored: opts.AllowIgnored,
client: opts.Client,
imageCache: docker.NewImageCache(),
}
}
// The reason we ask for batchChanges here is to surface errors about trying to use batch
// changes in an unsupported environment sooner, since the version check is typically the
// first thing we do.
const sourcegraphVersionQuery = `query SourcegraphVersion {
site {
productVersion
}
batchChanges(first: 1) {
nodes {
id
}
}
}
`
// getSourcegraphVersion queries the Sourcegraph GraphQL API to get the
// current version of the Sourcegraph instance.
func (svc *Service) getSourcegraphVersion(ctx context.Context) (string, error) {
var result struct {
Site struct {
ProductVersion string
}
}
ok, err := svc.client.NewQuery(sourcegraphVersionQuery).Do(ctx, &result)
if err != nil || !ok {
return "", err
}
return result.Site.ProductVersion, err
}
// DetermineFeatureFlags fetches the version of the configured Sourcegraph
// instance and then sets flags on the Service itself to use features available
// in that version, e.g. gzip compression.
func (svc *Service) DetermineFeatureFlags(ctx context.Context) error {
version, err := svc.getSourcegraphVersion(ctx)
if err != nil {
return errors.Wrap(err, "failed to query Sourcegraph version to check for available features")
}
return svc.features.SetFromVersion(version)
}
// TODO(campaigns-deprecation): this shim can be removed in Sourcegraph 4.0.
func (svc *Service) newOperations() graphql.Operations {
return graphql.NewOperations(
svc.client,
svc.features.BatchChanges,
svc.features.UseGzipCompression,
)
}
func (svc *Service) newRequest(query string, vars map[string]interface{}) api.Request {
if svc.features.UseGzipCompression {
return svc.client.NewGzippedRequest(query, vars)
}
return svc.client.NewRequest(query, vars)
}
func (svc *Service) ApplyBatchChange(ctx context.Context, spec graphql.BatchSpecID) (*graphql.BatchChange, error) {
return svc.newOperations().ApplyBatchChange(ctx, spec)
}
func (svc *Service) CreateBatchSpec(ctx context.Context, namespace, spec string, ids []graphql.ChangesetSpecID) (graphql.BatchSpecID, string, error) {
result, err := svc.newOperations().CreateBatchSpec(ctx, namespace, spec, ids)
if err != nil {
return "", "", err
}
return result.ID, result.ApplyURL, nil
}
const createChangesetSpecMutation = `
mutation CreateChangesetSpec($spec: String!) {
createChangesetSpec(changesetSpec: $spec) {
... on HiddenChangesetSpec {
id
}
... on VisibleChangesetSpec {
id
}
}
}
`
func (svc *Service) CreateChangesetSpec(ctx context.Context, spec *batcheslib.ChangesetSpec, maxRetries int) (graphql.ChangesetSpecID, error) {
return svc.createChangesetSpec(ctx, spec, maxRetries, 5*time.Second, 1.6)
}
func (svc *Service) createChangesetSpec(ctx context.Context, spec *batcheslib.ChangesetSpec, maxRetries int, retryWait time.Duration, multiplier float64) (graphql.ChangesetSpecID, error) {
raw, err := json.Marshal(spec)
if err != nil {
return "", errors.Wrap(err, "marshalling changeset spec JSON")
}
req := svc.newRequest(createChangesetSpecMutation, map[string]interface{}{
"spec": string(raw),
})
var result struct {
CreateChangesetSpec struct {
ID string
}
}
if err := retrier.Retry(func() error {
ok, err := req.Do(ctx, &result)
if !ok {
return errors.New("no data is available")
}
return err
}, maxRetries, retryWait, multiplier); err != nil {
return "", err
}
return graphql.ChangesetSpecID(result.CreateChangesetSpec.ID), nil
}
// EnsureDockerImages iterates over the steps within the batch spec to ensure the
// images exist and to determine the exact content digest to be used when running
// each step, including any required by the service itself.
//
// Progress information is reported back to the given progress function.
func (svc *Service) EnsureDockerImages(
ctx context.Context,
steps []batcheslib.Step,
parallelism int,
progress func(done, total int),
) (map[string]docker.Image, error) {
// Figure out the image names used in the batch spec.
names := map[string]struct{}{}
for i := range steps {
names[steps[i].Container] = struct{}{}
}
total := len(names)
progress(0, total)
// Set up the channels that will be used in the parallel goroutines handling
// the pulls.
type image struct {
name string
image docker.Image
err error
}
complete := make(chan image)
inputs := make(chan string, total)
// Set up a worker context that we can use to terminate the workers if an
// error occurs.
workerCtx, cancel := context.WithCancel(ctx)
defer cancel()
// Spawn worker goroutines to call EnsureImage on each image name.
if parallelism < 1 {
parallelism = 1
}
if parallelism > total {
parallelism = total
}
var wg sync.WaitGroup
for i := 0; i < parallelism; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-workerCtx.Done():
// If the worker context has been cancelled, then we just want to
// return immediately, rather than continuing to read from inputs.
return
case name, more := <-inputs:
if !more {
return
}
img, err := svc.EnsureImage(workerCtx, name)
select {
case <-workerCtx.Done():
return
case complete <- image{
name: name,
image: img,
err: err,
}:
// All good; let's move onto the next input.
}
}
}
}()
}
go func() {
wg.Wait()
close(complete)
}()
// Send the image names to the worker goroutines.
go func() {
for name := range names {
inputs <- name
}
close(inputs)
}()
// Receive the results of the image pulls and build the return value.
i := 0
images := make(map[string]docker.Image)
for image := range complete {
if image.err != nil {
// If EnsureImage errored, then we'll early return here and let the
// worker context clean things up.
return nil, image.err
}
images[image.name] = image.image
i += 1
progress(i, total)
}
return images, nil
}
func (svc *Service) EnsureImage(ctx context.Context, name string) (docker.Image, error) {
img := svc.imageCache.Get(name)
if err := img.Ensure(ctx); err != nil {
return nil, errors.Wrapf(err, "pulling image %q", name)
}
return img, nil
}
func (svc *Service) DetermineWorkspaces(ctx context.Context, repos []*graphql.Repository, spec *batcheslib.BatchSpec) ([]RepoWorkspace, error) {
return findWorkspaces(ctx, spec, svc, repos)
}
func (svc *Service) BuildTasks(ctx context.Context, attributes *templatelib.BatchChangeAttributes, workspaces []RepoWorkspace) []*executor.Task {
return buildTasks(ctx, attributes, workspaces)
}
func (svc *Service) NewCoordinator(opts executor.NewCoordinatorOpts) *executor.Coordinator {
opts.RepoArchiveRegistry = repozip.NewArchiveRegistry(svc.client, opts.CacheDir, opts.CleanArchives)
opts.Features = svc.features
opts.EnsureImage = svc.EnsureImage
return executor.NewCoordinator(opts)
}
func (svc *Service) CreateImportChangesetSpecs(ctx context.Context, batchSpec *batcheslib.BatchSpec) ([]*batcheslib.ChangesetSpec, error) {
return batcheslib.BuildImportChangesetSpecs(ctx, batchSpec.ImportChangesets, func(ctx context.Context, repoNames []string) (_ map[string]string, errs error) {
repoNameIDs := map[string]string{}
for _, name := range repoNames {
repo, err := svc.resolveRepositoryName(ctx, name)
if err != nil {
wrapped := errors.Wrapf(err, "resolving repository name %q", name)
errs = errors.Append(errs, wrapped)
continue
}
repoNameIDs[name] = repo.ID
}
return repoNameIDs, errs
})
}
// ValidateChangesetSpecs validates that among all branch changesets there are no
// duplicates in branch names in a single repo.
func (svc *Service) ValidateChangesetSpecs(repos []*graphql.Repository, specs []*batcheslib.ChangesetSpec) error {
repoByID := make(map[string]*graphql.Repository, len(repos))
for _, repo := range repos {
repoByID[repo.ID] = repo
}
byRepoAndBranch := make(map[string]map[string][]*batcheslib.ChangesetSpec)
for _, spec := range specs {
// We don't need to validate imported changesets, as they can
// never have a critical branch name overlap.
if spec.Type() == batcheslib.ChangesetSpecDescriptionTypeExisting {
continue
}
if _, ok := byRepoAndBranch[spec.HeadRepository]; !ok {
byRepoAndBranch[spec.HeadRepository] = make(map[string][]*batcheslib.ChangesetSpec)
}
byRepoAndBranch[spec.HeadRepository][spec.HeadRef] = append(byRepoAndBranch[spec.HeadRepository][spec.HeadRef], spec)
}
duplicates := make(map[*graphql.Repository]map[string]int)
for repoID, specsByBranch := range byRepoAndBranch {
for branch, specs := range specsByBranch {
if len(specs) < 2 {
continue
}
r := repoByID[repoID]
if _, ok := duplicates[r]; !ok {
duplicates[r] = make(map[string]int)
}
duplicates[r][branch] = len(specs)
}
}
if len(duplicates) > 0 {
return &duplicateBranchesErr{duplicates: duplicates}
}
return nil
}
type duplicateBranchesErr struct {
duplicates map[*graphql.Repository]map[string]int
}
func (e *duplicateBranchesErr) Error() string {
var out strings.Builder
fmt.Fprintf(&out, "Multiple changeset specs have the same branch:\n\n")
for repo, branches := range e.duplicates {
for branch, duplicates := range branches {
branch = strings.TrimPrefix(branch, "refs/heads/")
fmt.Fprintf(&out, "\t* %s: %d changeset specs have the branch %q\n", repo.Name, duplicates, branch)
}
}
fmt.Fprint(&out, "\nMake sure that the changesetTemplate.branch field in the batch spec produces unique values for each changeset in a single repository and rerun this command.")
return out.String()
}
func (svc *Service) ParseBatchSpec(data []byte) (*batcheslib.BatchSpec, error) {
spec, err := batcheslib.ParseBatchSpec(data, batcheslib.ParseBatchSpecOptions{
AllowArrayEnvironments: svc.features.AllowArrayEnvironments,
AllowTransformChanges: svc.features.AllowTransformChanges,
AllowConditionalExec: svc.features.AllowConditionalExec,
})
if err != nil {
return nil, errors.Wrap(err, "parsing batch spec")
}
return spec, nil
}
const exampleSpecTmpl = `name: NAME-OF-YOUR-BATCH-CHANGE
description: DESCRIPTION-OF-YOUR-BATCH-CHANGE
# "on" specifies on which repositories to execute the "steps".
on:
# Example: find all repositories that contain a README.md file.
- repositoriesMatchingQuery: file:README.md
# "steps" are run in each repository. Each step is run in a Docker container
# with the repository as the working directory. Once complete, each
# repository's resulting diff is captured.
steps:
# Example: append "Hello World" to every README.md
- run: echo "Hello World" | tee -a $(find -name README.md)
container: alpine:3
# "changesetTemplate" describes the changeset (e.g., GitHub pull request) that
# will be created for each repository.
changesetTemplate:
title: Hello World
body: This adds Hello World to the README
branch: BRANCH-NAME-IN-EACH-REPOSITORY # Push the commit to this branch.
commit:
author:
name: {{ .Author.Name }}
email: {{ .Author.Email }}
message: Append Hello World to all README.md files
`
const exampleSpecPublishFlagTmpl = `
# Change published to true once you're ready to create changesets on the code host.
published: false
`
func (svc *Service) GenerateExampleSpec(ctx context.Context, fileName string) error {
// Try to create file. Bail out, if it already exists.
f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
if os.IsExist(err) {
return fmt.Errorf("file %s already exists", fileName)
}
return errors.Wrapf(err, "failed to create file %s", fileName)
}
defer f.Close()
t := exampleSpecTmpl
if !svc.features.AllowOptionalPublished {
t += exampleSpecPublishFlagTmpl
}
tmpl, err := template.New("").Parse(t)
if err != nil {
return err
}
author := batcheslib.GitCommitAuthor{
Name: "Sourcegraph",
Email: "[email protected]",
}
// Try to get better default values from git, ignore any errors.
gitAuthorName, err1 := getGitConfig("user.name")
gitAuthorEmail, err2 := getGitConfig("user.email")
if err1 == nil && err2 == nil && gitAuthorName != "" && gitAuthorEmail != "" {
author.Name = gitAuthorName
author.Email = gitAuthorEmail
}
err = tmpl.Execute(f, map[string]interface{}{"Author": author})
if err != nil {
return errors.Wrap(err, "failed to write batch spec to file")
}
return nil
}
const namespaceQuery = `
query NamespaceQuery($name: String!) {
user(username: $name) {
id
}
organization(name: $name) {
id
}
}
`
const usernameQuery = `
query GetCurrentUserID {
currentUser {
id
}
}
`
func (svc *Service) ResolveNamespace(ctx context.Context, namespace string) (string, error) {
if namespace == "" {
// if no namespace is provided, default to logged in user as namespace
var resp struct {
Data struct {
CurrentUser struct {
ID string `json:"id"`
} `json:"currentUser"`
} `json:"data"`
}
if ok, err := svc.client.NewRequest(usernameQuery, nil).DoRaw(ctx, &resp); err != nil || !ok {
return "", errors.WithMessage(err, "failed to resolve namespace: no user logged in")
}
if resp.Data.CurrentUser.ID == "" {
return "", errors.New("cannot resolve current user")
}
return resp.Data.CurrentUser.ID, nil
}
var result struct {
Data struct {
User *struct{ ID string }
Organization *struct{ ID string }
}
Errors []interface{}
}
if ok, err := svc.client.NewRequest(namespaceQuery, map[string]interface{}{
"name": namespace,
}).DoRaw(ctx, &result); err != nil || !ok {
return "", err
}
if result.Data.User != nil {
return result.Data.User.ID, nil
}
if result.Data.Organization != nil {
return result.Data.Organization.ID, nil
}
return "", fmt.Errorf("failed to resolve namespace %q: no user or organization found", namespace)
}
func (svc *Service) ResolveRepositories(ctx context.Context, spec *batcheslib.BatchSpec) ([]*graphql.Repository, error) {
agg := onlib.NewRepoRevisionAggregator()
unsupported := batches.UnsupportedRepoSet{}
ignored := batches.IgnoredRepoSet{}
// TODO: this could be trivially parallelised in the future.
for _, on := range spec.On {
repos, ruleType, err := svc.ResolveRepositoriesOn(ctx, &on)
if err != nil {
return nil, errors.Wrapf(err, "resolving %q", on.String())
}
result := agg.NewRuleRevisions(ruleType)
reposWithBranch := make([]*graphql.Repository, 0, len(repos))
for _, repo := range repos {
if !repo.HasBranch() {
continue
}
reposWithBranch = append(reposWithBranch, repo)
}
var repoBatchIgnores map[*graphql.Repository][]string
if !svc.allowIgnored {
repoBatchIgnores, err = svc.FindDirectoriesInRepos(ctx, ".batchignore", reposWithBranch...)
if err != nil {
return nil, err
}
}
for _, repo := range reposWithBranch {
result.AddRepoRevision(repo.ID, repo)
switch st := strings.ToLower(repo.ExternalRepository.ServiceType); st {
case "github", "gitlab", "bitbucketserver":
default:
if !svc.allowUnsupported {
unsupported.Append(repo)
}
}
if !svc.allowIgnored {
if locations, ok := repoBatchIgnores[repo]; ok && len(locations) > 0 {
ignored.Append(repo)
}
}
}
}
final := []*graphql.Repository{}
for _, rev := range agg.Revisions() {
repo := rev.(*graphql.Repository)
if !unsupported.Includes(repo) && !ignored.Includes(repo) {
final = append(final, repo)
}
}
if unsupported.HasUnsupported() {
return final, unsupported
}
if ignored.HasIgnored() {
return final, ignored
}
return final, nil
}
func (svc *Service) ResolveRepositoriesOn(ctx context.Context, on *batcheslib.OnQueryOrRepository) ([]*graphql.Repository, onlib.RepositoryRuleType, error) {
if on.RepositoriesMatchingQuery != "" {
repo, err := svc.resolveRepositorySearch(ctx, on.RepositoriesMatchingQuery)
return repo, onlib.RepositoryRuleTypeQuery, err
} else if on.Repository != "" {
branches, err := on.GetBranches()
if err != nil {
return nil, onlib.RepositoryRuleTypeExplicit, err
}
if len(branches) > 0 {
repos := make([]*graphql.Repository, len(branches))
for i, branch := range branches {
repo, err := svc.resolveRepositoryNameAndBranch(ctx, on.Repository, branch)
if err != nil {
return nil, onlib.RepositoryRuleTypeExplicit, err
}
repos[i] = repo
}
return repos, onlib.RepositoryRuleTypeExplicit, nil
} else {
repo, err := svc.resolveRepositoryName(ctx, on.Repository)
if err != nil {
return nil, onlib.RepositoryRuleTypeExplicit, err
}
return []*graphql.Repository{repo}, onlib.RepositoryRuleTypeExplicit, nil
}
}
// This shouldn't happen on any batch spec that has passed validation, but,
// alas, software.
return nil, onlib.RepositoryRuleTypeExplicit, ErrMalformedOnQueryOrRepository
}
const repositoryNameQuery = `
query Repository($name: String!, $queryCommit: Boolean!, $rev: String!) {
repository(name: $name) {
...repositoryFields
}
}
` + graphql.RepositoryFieldsFragment
func (svc *Service) resolveRepositoryName(ctx context.Context, name string) (*graphql.Repository, error) {
var result struct{ Repository *graphql.Repository }
if ok, err := svc.client.NewRequest(repositoryNameQuery, map[string]interface{}{
"name": name,
"queryCommit": false,
"rev": "",
}).Do(ctx, &result); err != nil || !ok {
return nil, err
}
if result.Repository == nil {
return nil, errors.New("no repository found")
}
return result.Repository, nil
}
func (svc *Service) resolveRepositoryNameAndBranch(ctx context.Context, name, branch string) (*graphql.Repository, error) {
var result struct{ Repository *graphql.Repository }
if ok, err := svc.client.NewRequest(repositoryNameQuery, map[string]interface{}{
"name": name,
"queryCommit": true,
"rev": branch,
}).Do(ctx, &result); err != nil || !ok {
return nil, err
}
if result.Repository == nil {
return nil, errors.New("no repository found")
}
if result.Repository.Commit.OID == "" {
return nil, fmt.Errorf("no branch matching %q found for repository %s", branch, name)
}
result.Repository.Branch = graphql.Branch{
Name: branch,
Target: result.Repository.Commit,
}
return result.Repository, nil
}
// TODO: search result alerts.
const repositorySearchQuery = `
query ChangesetRepos(
$query: String!,
$queryCommit: Boolean!,
$rev: String!,
) {
search(query: $query, version: V2) {
results {
results {
__typename
... on Repository {
...repositoryFields
}
... on FileMatch {
file { path }
repository {
...repositoryFields
}
}
}
}
}
}
` + graphql.RepositoryFieldsFragment
func (svc *Service) resolveRepositorySearch(ctx context.Context, query string) ([]*graphql.Repository, error) {
var result struct {
Search struct {
Results struct {
Results []searchResult
}
}
}
if ok, err := svc.client.NewRequest(repositorySearchQuery, map[string]interface{}{
"query": setDefaultQueryCount(query),
"queryCommit": false,
"rev": "",
}).Do(ctx, &result); err != nil || !ok {
return nil, err
}
ids := map[string]*graphql.Repository{}
var repos []*graphql.Repository
for _, r := range result.Search.Results.Results {
existing, ok := ids[r.ID]
if !ok {
repo := r.Repository
repos = append(repos, &repo)
ids[r.ID] = &repo
} else {
for file := range r.FileMatches {
existing.FileMatches[file] = true
}
}
}
return repos, nil
}
// findDirectoriesResult maps the name of the GraphQL query to its results. The
// name is the repository's ID.
type findDirectoriesResult map[string]struct {
Results struct{ Results []searchResult }
}
const searchQueryTmpl = `%s: search(query: %q, version: V2) {
results {
results {
__typename
... on FileMatch {
file { path }
}
}
}
}
`
const findDirectoriesInReposBatchSize = 50
// FindDirectoriesInRepos returns a map of repositories and the locations of
// files matching the given file name in the repository.
// The locations are paths relative to the root of the directory.
// No "/" at the beginning.
// A dot (".") represents the root directory.
func (svc *Service) FindDirectoriesInRepos(ctx context.Context, fileName string, repos ...*graphql.Repository) (map[*graphql.Repository][]string, error) {
// Build up unique identifiers that are safe to use as GraphQL query aliases.
reposByQueryID := map[string]*graphql.Repository{}
queryIDByRepo := map[*graphql.Repository]string{}
for i, repo := range repos {
queryID := fmt.Sprintf("repo_%d", i)
reposByQueryID[queryID] = repo
queryIDByRepo[repo] = queryID
}
findInBatch := func(batch []*graphql.Repository, results map[*graphql.Repository][]string) error {
var a strings.Builder
a.WriteString("query DirectoriesContainingFile {\n")
for _, repo := range batch {
query := fmt.Sprintf(`file:(^|/)%s$ repo:^%s$@%s type:path count:99999`, regexp.QuoteMeta(fileName), regexp.QuoteMeta(repo.Name), repo.Rev())
a.WriteString(fmt.Sprintf(searchQueryTmpl, queryIDByRepo[repo], query))
}
a.WriteString("}")
var result findDirectoriesResult
if ok, err := svc.client.NewQuery(a.String()).Do(ctx, &result); err != nil || !ok {
return err
}
for queryID, search := range result {
repo, ok := reposByQueryID[queryID]
if !ok {
return fmt.Errorf("result for query %q did not match any repository", queryID)
}
files := map[string]struct{}{}
for _, r := range search.Results.Results {
for file := range r.FileMatches {
files[file] = struct{}{}
}
}
var dirs []string
for f := range files {
dir := path.Dir(f)
// "." means the path is root, but in the executor we use "" to signify root.
if dir == "." {
dir = ""
}
// We use path.Dir and not filepath.Dir here, because while
// src-cli might be executed on Windows, we need the paths to
// be Unix paths, since they will be used inside Docker
// containers.
dirs = append(dirs, dir)
}
results[repo] = dirs
}
return nil
}
results := make(map[*graphql.Repository][]string)
for start := 0; start < len(repos); start += findDirectoriesInReposBatchSize {
if err := ctx.Err(); err != nil {
return nil, err
}
end := start + findDirectoriesInReposBatchSize
if end > len(repos) {
end = len(repos)
}
batch := repos[start:end]
err := findInBatch(batch, results)
if err != nil {
return results, err
}
}
return results, nil
}
var defaultQueryCountRegex = regexp.MustCompile(`\bcount:(\d+|all)\b`)
const hardCodedCount = " count:999999"
func setDefaultQueryCount(query string) string {
if defaultQueryCountRegex.MatchString(query) {
return query
}
return query + hardCodedCount
}
type searchResult struct {
graphql.Repository
}
func (sr *searchResult) UnmarshalJSON(data []byte) error {
var tn struct {
Typename string `json:"__typename"`
}
if err := json.Unmarshal(data, &tn); err != nil {
return err
}
switch tn.Typename {
case "FileMatch":
var result struct {
Repository graphql.Repository
File struct {
Path string
}
}
if err := json.Unmarshal(data, &result); err != nil {
return err
}
sr.Repository = result.Repository
sr.Repository.FileMatches = map[string]bool{result.File.Path: true}
return nil
case "Repository":
if err := json.Unmarshal(data, &sr.Repository); err != nil {
return err
}
sr.Repository.FileMatches = map[string]bool{}
return nil
default:
return errors.Errorf("unknown GraphQL type %q", tn.Typename)
}
}
func getGitConfig(attribute string) (string, error) {
cmd := exec.Command("git", "config", "--get", attribute)
out, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}