Skip to content

Commit

Permalink
feat(platform/github): automatic github registry authentication using…
Browse files Browse the repository at this point in the history
… the given github token (#18926)
  • Loading branch information
PhilipAbed committed Dec 20, 2022
1 parent 0b53d19 commit 1b232a2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
66 changes: 66 additions & 0 deletions lib/modules/platform/github/__snapshots__/index.spec.ts.snap
Expand Up @@ -13,6 +13,7 @@ exports[`modules/platform/github/index initPlatform() should support custom endp
{
"endpoint": "https://ghe.renovatebot.com/",
"gitAuthor": "undefined <user@domain.com>",
"hostRules": [],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -22,6 +23,7 @@ exports[`modules/platform/github/index initPlatform() should support custom endp
{
"endpoint": "https://ghe.renovatebot.com/",
"gitAuthor": "undefined <user@domain.com>",
"hostRules": [],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -31,6 +33,22 @@ exports[`modules/platform/github/index initPlatform() should support default end
{
"endpoint": "https://api.github.com/",
"gitAuthor": undefined,
"hostRules": [
{
"matchHost": "ghcr.io",
"password": "123test",
"username": "dummy",
},
{
"matchHost": "pkg.github.com",
"token": "123test",
},
{
"matchHost": "nuget.pkg.github.com",
"password": "123test",
"username": "dummy",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -40,6 +58,22 @@ exports[`modules/platform/github/index initPlatform() should support default end
{
"endpoint": "https://api.github.com/",
"gitAuthor": undefined,
"hostRules": [
{
"matchHost": "ghcr.io",
"password": "123test",
"username": "dummy",
},
{
"matchHost": "pkg.github.com",
"token": "123test",
},
{
"matchHost": "nuget.pkg.github.com",
"password": "123test",
"username": "dummy",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -49,6 +83,22 @@ exports[`modules/platform/github/index initPlatform() should support default end
{
"endpoint": "https://api.github.com/",
"gitAuthor": "undefined <user@domain.com>",
"hostRules": [
{
"matchHost": "ghcr.io",
"password": "123test",
"username": "dummy",
},
{
"matchHost": "pkg.github.com",
"token": "123test",
},
{
"matchHost": "nuget.pkg.github.com",
"password": "123test",
"username": "dummy",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand All @@ -58,6 +108,22 @@ exports[`modules/platform/github/index initPlatform() should support gitAuthor a
{
"endpoint": "https://api.github.com/",
"gitAuthor": "renovate@whitesourcesoftware.com",
"hostRules": [
{
"matchHost": "ghcr.io",
"password": "123test",
"username": "dummy",
},
{
"matchHost": "pkg.github.com",
"token": "123test",
},
{
"matchHost": "nuget.pkg.github.com",
"password": "123test",
"username": "dummy",
},
],
"renovateUsername": "renovate-bot",
"token": "123test",
}
Expand Down
29 changes: 25 additions & 4 deletions lib/modules/platform/github/index.ts
Expand Up @@ -21,7 +21,11 @@ import {
REPOSITORY_RENAMED,
} from '../../../constants/error-messages';
import { logger } from '../../../logger';
import type { BranchStatus, VulnerabilityAlert } from '../../../types';
import type {
BranchStatus,
HostRule,
VulnerabilityAlert,
} from '../../../types';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import * as git from '../../../util/git';
import { listCommitTree, pushCommitToRenovateRef } from '../../../util/git';
Expand Down Expand Up @@ -166,14 +170,31 @@ export async function initPlatform({
}
}
logger.debug({ platformConfig, renovateUsername }, 'Platform config');
const platformResult: PlatformResult = {
const gitHubRules: HostRule[] = [];
if (!platformConfig.isGhe) {
gitHubRules.push({
matchHost: 'ghcr.io',
username: 'dummy',
password: token,
});
gitHubRules.push({
matchHost: 'pkg.github.com',
token,
});
gitHubRules.push({
matchHost: 'nuget.pkg.github.com',
username: 'dummy',
password: token,
});
}

return {
endpoint: platformConfig.endpoint,
gitAuthor: gitAuthor ?? discoveredGitAuthor,
renovateUsername,
token,
hostRules: gitHubRules,
};

return platformResult;
}

// Get all repositories that the user has access to
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/types.ts
@@ -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,7 @@ export interface PlatformResult {
renovateUsername?: string;
token?: string;
gitAuthor?: string;
hostRules?: HostRule[];
}

export interface RepoResult {
Expand Down

0 comments on commit 1b232a2

Please sign in to comment.