Skip to content

Commit 01dee16

Browse files
fix(github): Fix issue with users not getting picked up in GitHub config (#428)
* fix * changelog
1 parent 65d3cd9 commit 01dee16

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
- Fixed issue where `users` specified in a GitHub config were not getting picked up when a `token` is also specified. [#428](https://github.com/sourcebot-dev/sourcebot/pull/428)
12+
1013
### Added
1114
- [ask sb] Added OpenAI Compatible Language Provider. [#424](https://github.com/sourcebot-dev/sourcebot/pull/424)
1215

packages/backend/src/github.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ export const getGitHubReposFromConfig = async (config: GithubConnectionConfig, o
106106
}
107107

108108
if (config.users) {
109-
const isAuthenticated = config.token !== undefined;
110-
const { validRepos, notFoundUsers } = await getReposOwnedByUsers(config.users, isAuthenticated, octokit, signal);
109+
const { validRepos, notFoundUsers } = await getReposOwnedByUsers(config.users, octokit, signal);
111110
allRepos = allRepos.concat(validRepos);
112111
notFound.users = notFoundUsers;
113112
}
@@ -219,32 +218,27 @@ export const shouldExcludeRepo = ({
219218
return false;
220219
}
221220

222-
const getReposOwnedByUsers = async (users: string[], isAuthenticated: boolean, octokit: Octokit, signal: AbortSignal) => {
221+
const getReposOwnedByUsers = async (users: string[], octokit: Octokit, signal: AbortSignal) => {
223222
const results = await Promise.allSettled(users.map(async (user) => {
224223
try {
225224
logger.debug(`Fetching repository info for user ${user}...`);
226225

227226
const { durationMs, data } = await measure(async () => {
228227
const fetchFn = async () => {
229-
if (isAuthenticated) {
230-
return octokit.paginate(octokit.repos.listForAuthenticatedUser, {
231-
username: user,
232-
visibility: 'all',
233-
affiliation: 'owner',
234-
per_page: 100,
235-
request: {
236-
signal,
237-
},
238-
});
239-
} else {
240-
return octokit.paginate(octokit.repos.listForUser, {
241-
username: user,
242-
per_page: 100,
243-
request: {
244-
signal,
245-
},
246-
});
247-
}
228+
// @note: We need to use GitHub's search API here since it is the only way
229+
// to get all repositories (private and public) owned by a user that supports
230+
// the username as a parameter.
231+
// @see: https://github.com/orgs/community/discussions/24382#discussioncomment-3243958
232+
// @see: https://api.github.com/search/repositories?q=user:USERNAME
233+
const searchResults = await octokit.paginate(octokit.rest.search.repos, {
234+
q: `user:${user}`,
235+
per_page: 100,
236+
request: {
237+
signal,
238+
},
239+
});
240+
241+
return searchResults as OctokitRepository[];
248242
};
249243

250244
return fetchWithRetry(fetchFn, `user ${user}`, logger);

0 commit comments

Comments
 (0)