Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(github): automatic ghcr.io auth when using github.com #25017

Merged
merged 10 commits into from
Oct 9, 2023
32 changes: 32 additions & 0 deletions lib/modules/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ exports[`modules/platform/github/index initPlatform() should support default end
{
"endpoint": "https://api.github.com/",
"gitAuthor": undefined,
"hostRules": [
{
"hostType": "docker",
"matchHost": "ghcr.io",
"password": "123test",
"username": "USERNAME",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -40,6 +48,14 @@ exports[`modules/platform/github/index initPlatform() should support default end
{
"endpoint": "https://api.github.com/",
"gitAuthor": undefined,
"hostRules": [
{
"hostType": "docker",
"matchHost": "ghcr.io",
"password": "123test",
"username": "USERNAME",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -49,6 +65,14 @@ exports[`modules/platform/github/index initPlatform() should support default end
{
"endpoint": "https://api.github.com/",
"gitAuthor": "undefined <user@domain.com>",
"hostRules": [
{
"hostType": "docker",
"matchHost": "ghcr.io",
"password": "123test",
"username": "USERNAME",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -58,6 +82,14 @@ exports[`modules/platform/github/index initPlatform() should support gitAuthor a
{
"endpoint": "https://api.github.com/",
"gitAuthor": "renovate@whitesourcesoftware.com",
"hostRules": [
{
"hostType": "docker",
"matchHost": "ghcr.io",
"password": "123test",
"username": "USERNAME",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand Down
16 changes: 16 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,28 @@ describe('modules/platform/github/index', () => {
).toEqual({
endpoint: 'https://api.github.com/',
gitAuthor: 'my-app[bot] <12345+my-app[bot]@users.noreply.github.com>',
hostRules: [
{
hostType: 'docker',
matchHost: 'ghcr.io',
password: 'ghs_123test',
username: 'USERNAME',
},
],
renovateUsername: 'my-app[bot]',
token: 'x-access-token:ghs_123test',
});
expect(await github.initPlatform({ token: 'ghs_123test' })).toEqual({
endpoint: 'https://api.github.com/',
gitAuthor: 'my-app[bot] <12345+my-app[bot]@users.noreply.github.com>',
hostRules: [
{
hostType: 'docker',
matchHost: 'ghcr.io',
password: 'ghs_123test',
username: 'USERNAME',
},
],
renovateUsername: 'my-app[bot]',
token: 'x-access-token:ghs_123test',
});
Expand Down
12 changes: 11 additions & 1 deletion lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,17 @@ export async function initPlatform({
renovateUsername,
token,
};

if (platformResult.endpoint === 'https://api.github.com/') {
logger.debug('Adding GitHub token as GHCR password');
platformResult.hostRules = [
rarkins marked this conversation as resolved.
Show resolved Hide resolved
{
matchHost: 'ghcr.io',
hostType: 'docker',
username: 'USERNAME',
password: token.replace(/^x-access-token:/, ''),
},
];
}
return platformResult;
}

Expand Down
31 changes: 31 additions & 0 deletions lib/modules/platform/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,35 @@ describe('modules/platform/index', () => {
renovateUsername: 'abc',
});
});

it('merges platform hostRules with additionalHostRules', async () => {
const config = {
platform: 'github' as PlatformId,
endpoint: 'https://api.github.com',
gitAuthor: 'user@domain.com',
username: 'abc',
token: '123',
};

expect(await platform.initPlatform(config)).toEqual({
endpoint: 'https://api.github.com/',
gitAuthor: 'user@domain.com',
hostRules: [
{
hostType: 'docker',
matchHost: 'ghcr.io',
password: '123',
username: 'USERNAME',
},
{
hostType: 'github',
matchHost: 'api.github.com',
token: '123',
username: 'abc',
},
],
platform: 'github',
renovateUsername: 'abc',
});
});
});
6 changes: 5 additions & 1 deletion lib/modules/platform/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MergeStrategy } from '../../config/types';
import type { BranchStatus, VulnerabilityAlert } from '../../types';
import type { BranchStatus, HostRule, VulnerabilityAlert } from '../../types';
import type { CommitFilesConfig, CommitSha } from '../../util/git/types';

type VulnerabilityKey = string;
Expand All @@ -23,6 +23,10 @@ export interface PlatformResult {
renovateUsername?: string;
token?: string;
gitAuthor?: string;
/*
* return these only if _additional_ rules/hosts are required
*/
hostRules?: HostRule[];
}

export interface RepoResult {
Expand Down