Skip to content

Commit 10672e8

Browse files
authored
Actually skip unsupported repos if -allow-unsupported is not set (#403)
* Actually skip unsupported repos if -allow-unsupported is not set I think multiple changes to this method lead to a regression where unsupported repositories were correctly being reported as unsupported but were still added to the final list of repositories, regardless of whether `-allow-unsupported` was set or not. * Add PR URL to changelog entry
1 parent 6cccdc7 commit 10672e8

File tree

4 files changed

+112
-15
lines changed

4 files changed

+112
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All notable changes to `src-cli` are documented in this file.
2222
### Fixed
2323

2424
- Two race conditions in the terminal UI of `src campaign [apply|preview]` have been fixed. [#399](https://github.com/sourcegraph/src-cli/pull/399)
25+
- A regression caused repositories on unsupported code host to not be skipped by `src campaign [apply|preview]`, regardless of whether `-allow-unsupported` was set or not. [#403](https://github.com/sourcegraph/src-cli/pull/403)
2526

2627
### Removed
2728

internal/campaigns/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
// returned directly as an error value if needed.
1313
type UnsupportedRepoSet map[*graphql.Repository]struct{}
1414

15+
func (e UnsupportedRepoSet) includes(r *graphql.Repository) bool {
16+
_, ok := e[r]
17+
return ok
18+
}
19+
1520
func (e UnsupportedRepoSet) Error() string {
1621
repos := []string{}
1722
typeSet := map[string]struct{}{}

internal/campaigns/service.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,12 @@ func (svc *Service) ResolveRepositories(ctx context.Context, spec *CampaignSpec)
391391

392392
if other, ok := seen[repo.ID]; !ok {
393393
seen[repo.ID] = repo
394+
394395
switch st := strings.ToLower(repo.ExternalRepository.ServiceType); st {
395396
case "github", "gitlab", "bitbucketserver":
396397
default:
397-
unsupported.appendRepo(repo)
398398
if !svc.allowUnsupported {
399-
continue
399+
unsupported.appendRepo(repo)
400400
}
401401
}
402402
} else {
@@ -410,10 +410,12 @@ func (svc *Service) ResolveRepositories(ctx context.Context, spec *CampaignSpec)
410410

411411
final := make([]*graphql.Repository, 0, len(seen))
412412
for _, repo := range seen {
413-
final = append(final, repo)
413+
if !unsupported.includes(repo) {
414+
final = append(final, repo)
415+
}
414416
}
415417

416-
if unsupported.hasUnsupported() && !svc.allowUnsupported {
418+
if unsupported.hasUnsupported() {
417419
return final, unsupported
418420
}
419421

internal/campaigns/service_test.go

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,8 @@ func TestSetDefaultQueryCount(t *testing.T) {
3030
}
3131

3232
func TestResolveRepositorySearch(t *testing.T) {
33-
mux := http.NewServeMux()
34-
mux.HandleFunc("/.api/graphql", func(w http.ResponseWriter, r *http.Request) {
35-
w.Header().Set("Content-Type", "application/json")
36-
w.Write([]byte(testResolveRepositorySearchResult))
37-
})
38-
39-
ts := httptest.NewServer(mux)
40-
defer ts.Close()
41-
42-
var clientBuffer bytes.Buffer
43-
client := api.NewClient(api.ClientOpts{Endpoint: ts.URL, Out: &clientBuffer})
33+
client, done := mockGraphQLClient(testResolveRepositorySearchResult)
34+
defer done()
4435

4536
svc := &Service{client: client}
4637

@@ -119,3 +110,101 @@ const testResolveRepositorySearchResult = `{
119110
}
120111
}
121112
`
113+
114+
func mockGraphQLClient(response string) (client api.Client, done func()) {
115+
mux := http.NewServeMux()
116+
mux.HandleFunc("/.api/graphql", func(w http.ResponseWriter, r *http.Request) {
117+
w.Header().Set("Content-Type", "application/json")
118+
w.Write([]byte(response))
119+
})
120+
121+
ts := httptest.NewServer(mux)
122+
123+
var clientBuffer bytes.Buffer
124+
client = api.NewClient(api.ClientOpts{Endpoint: ts.URL, Out: &clientBuffer})
125+
126+
return client, ts.Close
127+
}
128+
129+
func TestResolveRepositories_Unsupported(t *testing.T) {
130+
client, done := mockGraphQLClient(testResolveRepositoriesUnsupported)
131+
defer done()
132+
133+
spec := &CampaignSpec{
134+
On: []OnQueryOrRepository{
135+
{RepositoriesMatchingQuery: "testquery"},
136+
},
137+
}
138+
139+
t.Run("allowUnsupported:true", func(t *testing.T) {
140+
svc := &Service{client: client, allowUnsupported: true}
141+
142+
repos, err := svc.ResolveRepositories(context.Background(), spec)
143+
if err != nil {
144+
t.Fatalf("unexpected error: %s", err)
145+
}
146+
if len(repos) != 4 {
147+
t.Fatalf("wrong number of repos. want=%d, have=%d", 4, len(repos))
148+
}
149+
})
150+
151+
t.Run("allowUnsupported:false", func(t *testing.T) {
152+
svc := &Service{client: client, allowUnsupported: false}
153+
154+
repos, err := svc.ResolveRepositories(context.Background(), spec)
155+
repoSet, ok := err.(UnsupportedRepoSet)
156+
if !ok {
157+
t.Fatalf("err is not UnsupportedRepoSet")
158+
}
159+
if len(repoSet) != 1 {
160+
t.Fatalf("wrong number of repos. want=%d, have=%d", 1, len(repoSet))
161+
}
162+
if len(repos) != 3 {
163+
t.Fatalf("wrong number of repos. want=%d, have=%d", 4, len(repos))
164+
}
165+
})
166+
}
167+
168+
const testResolveRepositoriesUnsupported = `{
169+
"data": {
170+
"search": {
171+
"results": {
172+
"results": [
173+
{
174+
"__typename": "Repository",
175+
"id": "UmVwb3NpdG9yeToxMw==",
176+
"name": "bitbucket.sgdev.org/SOUR/automation-testing",
177+
"url": "/bitbucket.sgdev.org/SOUR/automation-testing",
178+
"externalRepository": { "serviceType": "bitbucketserver" },
179+
"defaultBranch": { "name": "refs/heads/master", "target": { "oid": "b978d56de5578a935ca0bf07b56528acc99d5a61" } }
180+
},
181+
{
182+
"__typename": "Repository",
183+
"id": "UmVwb3NpdG9yeTo0",
184+
"name": "github.com/sourcegraph/automation-testing",
185+
"url": "/github.com/sourcegraph/automation-testing",
186+
"externalRepository": { "serviceType": "github" },
187+
"defaultBranch": { "name": "refs/heads/master", "target": { "oid": "6ac8a32ecaf6c4dc5ce050b9af51bce3db8efd63" } }
188+
},
189+
{
190+
"__typename": "Repository",
191+
"id": "UmVwb3NpdG9yeTo2MQ==",
192+
"name": "gitlab.sgdev.org/sourcegraph/automation-testing",
193+
"url": "/gitlab.sgdev.org/sourcegraph/automation-testing",
194+
"externalRepository": { "serviceType": "gitlab" },
195+
"defaultBranch": { "name": "refs/heads/master", "target": { "oid": "3b79a5d479d2af9cfe91e0aad4e9dddca7278150" } }
196+
},
197+
{
198+
"__typename": "Repository",
199+
"id": "UmVwb3NpdG9yeToxNDg=",
200+
"name": "git-codecommit.us-est-1.amazonaws.com/stripe-go",
201+
"url": "/git-codecommit.us-est-1.amazonaws.com/stripe-go",
202+
"externalRepository": { "serviceType": "awscodecommit" },
203+
"defaultBranch": { "name": "refs/heads/master", "target": { "oid": "3b79a5d479d2af9cfe91e0aad4e9dddca7278150" } }
204+
}
205+
]
206+
}
207+
}
208+
}
209+
}
210+
`

0 commit comments

Comments
 (0)