Skip to content

Commit

Permalink
feat(github): Add autodiscovery support for Github App (#13406)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
3 people authored Jan 14, 2022
1 parent 68dfc27 commit 9b1e318
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
1 change: 0 additions & 1 deletion docs/usage/getting-started/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ Alternatively as environment variable `RENOVATE_TOKEN`, or via CLI `--token=`.
**`repositories: ["orgname/repo-1","orgname/repo-2"]`**

List of repositories to run on.
Auto discovery does not work with a GitHub App.
Alternatively as comma-separated environment variable `RENOVATE_REPOSITORIES`.
The GitHub App installation token is scoped at most to a single organization and running on multiple organizations requires multiple invocations of `renovate` with different `token` and `repositories` parameters.

Expand Down
25 changes: 25 additions & 0 deletions lib/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ describe('platform/github/index', () => {
expect(repos).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should return an array of repos when using Github App endpoint', async () => {
//Use Github App token
await github.initPlatform({
endpoint: githubApiHost,
username: 'renovate-bot',
gitAuthor: 'Renovate Bot',
token: 'x-access-token:123test',
});
httpMock
.scope(githubApiHost)
.get('/installation/repositories?per_page=100')
.reply(200, {
repositories: [
{
full_name: 'a/b',
},
{
full_name: 'c/d',
},
],
});

const repos = await github.getRepos();
expect(repos).toStrictEqual(['a/b', 'c/d']);
});
});

function initRepoMock(
Expand Down
22 changes: 17 additions & 5 deletions lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export async function initPlatform({
throw new Error('Init: You must configure a GitHub personal access token');
}

platformConfig.isGHApp = token.startsWith('x-access-token:');

if (endpoint) {
platformConfig.endpoint = ensureTrailingSlash(endpoint);
githubHttp.setBaseUrl(platformConfig.endpoint);
Expand Down Expand Up @@ -148,11 +150,21 @@ export async function initPlatform({
export async function getRepos(): Promise<string[]> {
logger.debug('Autodiscovering GitHub repositories');
try {
const res = await githubApi.getJson<{ full_name: string }[]>(
'user/repos?per_page=100',
{ paginate: 'all' }
);
return res.body.map((repo) => repo.full_name);
if (platformConfig.isGHApp) {
const res = await githubApi.getJson<{
repositories: { full_name: string }[];
}>(`installation/repositories?per_page=100`, {
paginationField: 'repositories',
paginate: 'all',
});
return res.body.repositories.map((repo) => repo.full_name);
} else {
const res = await githubApi.getJson<{ full_name: string }[]>(
`user/repos?per_page=100`,
{ paginate: 'all' }
);
return res.body.map((repo) => repo.full_name);
}
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, `GitHub getRepos error`);
throw err;
Expand Down
1 change: 1 addition & 0 deletions lib/platform/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface PlatformConfig {
endpoint: string;
isGhe?: boolean;
gheVersion?: string | null;
isGHApp?: boolean;
}

export interface LocalRepoConfig {
Expand Down

0 comments on commit 9b1e318

Please sign in to comment.