|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + mockapi "github.com/sourcegraph/src-cli/internal/api/mock" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestListRepositoriesSkipsRepositoryWhenDefaultBranchCannotBeResolved(t *testing.T) { |
| 14 | + client := new(mockapi.Client) |
| 15 | + request := &mockapi.Request{Response: `{ |
| 16 | + "data": { |
| 17 | + "repositories": { |
| 18 | + "nodes": [ |
| 19 | + { |
| 20 | + "id": "UmVwb3NpdG9yeTox", |
| 21 | + "name": "github.com/sourcegraph/ok", |
| 22 | + "url": "/github.com/sourcegraph/ok", |
| 23 | + "description": "", |
| 24 | + "language": "Go", |
| 25 | + "createdAt": "2026-03-31T00:00:00Z", |
| 26 | + "updatedAt": null, |
| 27 | + "externalRepository": { |
| 28 | + "id": "RXh0ZXJuYWxSZXBvc2l0b3J5OjE=", |
| 29 | + "serviceType": "github", |
| 30 | + "serviceID": "https://github.com/" |
| 31 | + }, |
| 32 | + "defaultBranch": { |
| 33 | + "name": "refs/heads/main", |
| 34 | + "displayName": "main" |
| 35 | + }, |
| 36 | + "viewerCanAdminister": false, |
| 37 | + "keyValuePairs": [] |
| 38 | + }, |
| 39 | + { |
| 40 | + "id": "UmVwb3NpdG9yeToy", |
| 41 | + "name": "github.com/sourcegraph/broken", |
| 42 | + "url": "/github.com/sourcegraph/broken", |
| 43 | + "description": "", |
| 44 | + "language": "Go", |
| 45 | + "createdAt": "2026-03-31T00:00:00Z", |
| 46 | + "updatedAt": null, |
| 47 | + "externalRepository": { |
| 48 | + "id": "RXh0ZXJuYWxSZXBvc2l0b3J5OjI=", |
| 49 | + "serviceType": "github", |
| 50 | + "serviceID": "https://github.com/" |
| 51 | + }, |
| 52 | + "defaultBranch": null, |
| 53 | + "viewerCanAdminister": false, |
| 54 | + "keyValuePairs": [] |
| 55 | + } |
| 56 | + ] |
| 57 | + } |
| 58 | + }, |
| 59 | + "errors": [ |
| 60 | + { |
| 61 | + "message": "failed to resolve HEAD for github.com/sourcegraph/broken", |
| 62 | + "path": ["repositories", "nodes", 1, "defaultBranch"] |
| 63 | + } |
| 64 | + ] |
| 65 | + }`} |
| 66 | + |
| 67 | + client.On( |
| 68 | + "NewRequest", |
| 69 | + mock.MatchedBy(func(query string) bool { |
| 70 | + return strings.Contains(query, "defaultBranch") |
| 71 | + }), |
| 72 | + mock.MatchedBy(func(vars map[string]any) bool { |
| 73 | + indexed, ok := vars["indexed"].(bool) |
| 74 | + if !ok || !indexed { |
| 75 | + return false |
| 76 | + } |
| 77 | + notIndexed, ok := vars["notIndexed"].(bool) |
| 78 | + return ok && !notIndexed |
| 79 | + }), |
| 80 | + ).Return(request).Once() |
| 81 | + |
| 82 | + request.On("DoRaw", context.Background(), mock.Anything). |
| 83 | + Return(true, nil). |
| 84 | + Once() |
| 85 | + |
| 86 | + repos, skipped, err := listRepositories(context.Background(), client, reposListOptions{ |
| 87 | + first: 1000, |
| 88 | + cloned: true, |
| 89 | + notCloned: true, |
| 90 | + indexed: true, |
| 91 | + notIndexed: false, |
| 92 | + orderBy: "REPOSITORY_NAME", |
| 93 | + }) |
| 94 | + require.NoError(t, err) |
| 95 | + require.Len(t, repos, 1) |
| 96 | + require.Len(t, skipped, 1) |
| 97 | + require.Equal(t, "github.com/sourcegraph/ok", repos[0].Name) |
| 98 | + require.Equal(t, "github.com/sourcegraph/broken", skipped[0].Name) |
| 99 | + client.AssertExpectations(t) |
| 100 | + request.AssertExpectations(t) |
| 101 | +} |
0 commit comments