Skip to content

Commit d2d9379

Browse files
Merge branch 'main' into feature/ins-58
2 parents d3e5c76 + 24c73b0 commit d2d9379

File tree

6 files changed

+619
-552
lines changed

6 files changed

+619
-552
lines changed

pkg/engine/github.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func (e *Engine) ScanGitHub(ctx context.Context, c sources.GithubConfig) (source
2020
Repositories: c.Repos,
2121
ScanUsers: c.IncludeMembers,
2222
IgnoreRepos: c.ExcludeRepos,
23-
IncludeRepos: c.IncludeRepos,
2423
IncludeForks: c.IncludeForks,
2524
IncludeIssueComments: c.IncludeIssueComments,
2625
IncludePullRequestComments: c.IncludePullRequestComments,

pkg/pb/sourcespb/sources.pb.go

Lines changed: 529 additions & 523 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sources/github/github.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, so
253253

254254
s.filteredRepoCache = s.newFilteredRepoCache(aCtx,
255255
simple.NewCache[string](),
256-
append(s.conn.GetRepositories(), s.conn.GetIncludeRepos()...),
256+
s.conn.GetRepositories(),
257257
s.conn.GetIgnoreRepos(),
258258
)
259259
s.repos = s.conn.Repositories
@@ -433,33 +433,42 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e
433433
case *unauthenticatedConnector:
434434
s.enumerateUnauthenticated(ctx, dedupeReporter)
435435
}
436-
s.repos = make([]string, 0, s.filteredRepoCache.Count())
437-
438-
// Double make sure that all enumerated repositories in the
439-
// filteredRepoCache have an entry in the repoInfoCache.
440-
for _, repo := range s.filteredRepoCache.Values() {
441-
// Extract the repository name from the URL for filtering
442-
repoName := repo
443-
if strings.Contains(repo, "/") {
444-
// Try to extract org/repo name from URL
445-
if strings.Contains(repo, "github.com") {
446-
parts := strings.Split(repo, "/")
447-
if len(parts) >= 2 {
448-
repoName = parts[len(parts)-2] + "/" + strings.TrimSuffix(parts[len(parts)-1], ".git")
436+
// If explicit repositories were provided, use them directly without filtering
437+
// Otherwise, rebuild s.repos from the filteredRepoCache
438+
if len(s.conn.Repositories) > 0 {
439+
// Explicit repositories bypass filtering - use them as-is
440+
s.repos = s.conn.Repositories
441+
ctx.Logger().V(1).Info("Using explicit repositories", "count", len(s.repos))
442+
} else {
443+
// No explicit repositories - rebuild from enumerated cache with filtering
444+
s.repos = make([]string, 0, s.filteredRepoCache.Count())
445+
446+
// Double make sure that all enumerated repositories in the
447+
// filteredRepoCache have an entry in the repoInfoCache.
448+
for _, repo := range s.filteredRepoCache.Values() {
449+
// Extract the repository name from the URL for filtering
450+
repoName := repo
451+
if strings.Contains(repo, "/") {
452+
// Try to extract org/repo name from URL
453+
if strings.Contains(repo, "github.com") {
454+
parts := strings.Split(repo, "/")
455+
if len(parts) >= 2 {
456+
repoName = parts[len(parts)-2] + "/" + strings.TrimSuffix(parts[len(parts)-1], ".git")
457+
}
449458
}
450459
}
451-
}
452460

453-
// Final filter check - only include repositories that pass the filter
454-
if s.filteredRepoCache.wantRepo(repoName) {
455-
ctx = context.WithValue(ctx, "repo", repo)
461+
// Final filter check - only include repositories that pass the filter
462+
if s.filteredRepoCache.wantRepo(repoName) {
463+
ctx = context.WithValue(ctx, "repo", repo)
456464

457-
repo, err := s.ensureRepoInfoCache(ctx, repo, &unitErrorReporter{reporter})
458-
if err != nil {
459-
ctx.Logger().Error(err, "error caching repo info")
460-
_ = dedupeReporter.UnitErr(ctx, fmt.Errorf("error caching repo info: %w", err))
465+
repo, err := s.ensureRepoInfoCache(ctx, repo, &unitErrorReporter{reporter})
466+
if err != nil {
467+
ctx.Logger().Error(err, "error caching repo info")
468+
_ = dedupeReporter.UnitErr(ctx, fmt.Errorf("error caching repo info: %w", err))
469+
}
470+
s.repos = append(s.repos, repo)
461471
}
462-
s.repos = append(s.repos, repo)
463472
}
464473
}
465474
githubReposEnumerated.WithLabelValues(s.name).Set(float64(len(s.repos)))

pkg/sources/github/github_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ func TestSource_Validate(t *testing.T) {
957957
}
958958
for _, tt := range tests {
959959
t.Run(tt.name, func(t *testing.T) {
960-
connector, err := newConnector(tt.sourceConfig)
960+
connector, err := newConnector(ctx, tt.sourceConfig)
961961
require.NoError(t, err)
962962
tt.sourceConfig.connector = connector
963963

pkg/sources/github/github_test.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/google/go-cmp/cmp"
2121
"github.com/google/go-github/v67/github"
2222
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
2324
"golang.org/x/sync/errgroup"
2425
"google.golang.org/protobuf/types/known/anypb"
2526
"gopkg.in/h2non/gock.v1"
@@ -111,7 +112,7 @@ func TestAddReposByOrg(t *testing.T) {
111112
assert.True(t, gock.IsDone())
112113
}
113114

114-
func TestAddReposByOrg_IncludeRepos(t *testing.T) {
115+
func TestAddReposByOrg_Repositories(t *testing.T) {
115116
defer gock.Off()
116117

117118
gock.New("https://api.github.com").
@@ -127,7 +128,7 @@ func TestAddReposByOrg_IncludeRepos(t *testing.T) {
127128
Credential: &sourcespb.GitHub_Token{
128129
Token: "super secret token",
129130
},
130-
IncludeRepos: []string{"super-secret-org/super*"},
131+
Repositories: []string{"super-secret-org/super-secret-repo", "super-secret-org/super-secret-repo2"},
131132
Organizations: []string{"super-secret-org"},
132133
})
133134
err := s.getReposByOrg(context.Background(), "super-secret-org", noopReporter())
@@ -748,7 +749,7 @@ func BenchmarkEnumerate(b *testing.B) {
748749
}
749750
}
750751

751-
func TestEnumerateWithToken_IncludeRepos(t *testing.T) {
752+
func TestEnumerateWithToken_Repositories(t *testing.T) {
752753
defer gock.Off()
753754

754755
gock.New("https://api.github.com").
@@ -1046,6 +1047,55 @@ func TestRepositoryFiltering(t *testing.T) {
10461047
assert.False(t, cache5.wantRepo("other/repo1"))
10471048
}
10481049

1050+
func TestExplicitRepositoryBypass(t *testing.T) {
1051+
// Test that explicit repositories are included in enumeration
1052+
ctx := context.Background()
1053+
1054+
// Set up mocks for the API calls
1055+
gock.New("https://api.github.com").
1056+
Get("/user").
1057+
Reply(200).
1058+
JSON(map[string]string{"login": "test-user"})
1059+
1060+
gock.New("https://api.github.com").
1061+
Get("/repos/org/explicit-repo").
1062+
Reply(200).
1063+
JSON(map[string]any{
1064+
"full_name": "org/explicit-repo",
1065+
"clone_url": "https://github.com/org/explicit-repo.git",
1066+
"size": 1,
1067+
})
1068+
1069+
gock.New("https://api.github.com").
1070+
Get("/repos/org/another-explicit").
1071+
Reply(200).
1072+
JSON(map[string]any{
1073+
"full_name": "org/another-explicit",
1074+
"clone_url": "https://github.com/org/another-explicit.git",
1075+
"size": 1,
1076+
})
1077+
1078+
// Create a source with explicit repositories
1079+
source := initTestSource(&sourcespb.GitHub{
1080+
Credential: &sourcespb.GitHub_Token{
1081+
Token: "super secret token",
1082+
},
1083+
Repositories: []string{
1084+
"https://github.com/org/explicit-repo.git",
1085+
"https://github.com/org/another-explicit.git",
1086+
},
1087+
})
1088+
1089+
// Test the Enumerate method
1090+
err := source.Enumerate(ctx, noopReporter())
1091+
require.NoError(t, err)
1092+
1093+
// Verify that explicit repositories are included in the enumeration
1094+
assert.Len(t, source.repos, 2, "Should have 2 explicit repositories")
1095+
assert.Contains(t, source.repos, "https://github.com/org/explicit-repo.git")
1096+
assert.Contains(t, source.repos, "https://github.com/org/another-explicit.git")
1097+
}
1098+
10491099
func noopReporter() sources.UnitReporter {
10501100
return sources.VisitorReporter{
10511101
VisitUnit: func(context.Context, sources.SourceUnit) error {

proto/sources.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ message GitHub {
266266
string head = 9;
267267
string base = 10;
268268
repeated string ignore_repos = 11;
269-
repeated string include_repos = 12;
269+
// DEPRECATED: include_repos is deprecated in favor of repositories.
270+
// It was used for filtering when the repositories field wasn't populating the UI-input results correctly
271+
// It can be removed when we no longer depend on it
272+
repeated string include_repos = 12 [deprecated = true];
270273
bool include_pull_request_comments = 14;
271274
bool include_issue_comments = 15;
272275
bool include_gist_comments = 16;

0 commit comments

Comments
 (0)