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(managers/npm): add entries with protocol prefix in temporary .yarnrc.yml file #30058

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/modules/manager/npm/post-update/rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ describe('modules/manager/npm/post-update/rules', () => {

additionalYarnRcYml: {
npmRegistries: {
'//https://registry.npmjs.org/': {
npmAuthToken: 'token123',
},
'//https://registry.other.org/': {
npmAuthIdent: 'basictoken123',
},
'//registry.company.com/': {
npmAuthIdent: 'user123:pass123',
},
Expand All @@ -115,6 +121,12 @@ describe('modules/manager/npm/post-update/rules', () => {
],
"additionalYarnRcYml": {
"npmRegistries": {
"//https://registry.npmjs.org/": {
"npmAuthToken": "token123",
},
"//https://registry.other.org/": {
"npmAuthIdent": "basictoken123",
},
"//registry.company.com/": {
"npmAuthIdent": "user123:pass123",
},
Expand Down
90 changes: 61 additions & 29 deletions lib/modules/manager/npm/post-update/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,83 @@ import * as hostRules from '../../../../util/host-rules';
import { regEx } from '../../../../util/regex';
import { toBase64 } from '../../../../util/string';
import { isHttpUrl } from '../../../../util/url';
import type { YarnRcYmlFile } from './types';

export interface HostRulesResult {
additionalNpmrcContent: string[];
additionalYarnRcYml?: any;
}

export function processHostRules(): HostRulesResult {
let additionalYarnRcYml: any;
const additionalYarnRcYml: YarnRcYmlFile = { npmRegistries: {} };

// Determine the additional npmrc content to add based on host rules
const additionalNpmrcContent = [];
const npmHostRules = hostRules.findAll({
hostType: 'npm',
});
for (const hostRule of npmHostRules) {
if (hostRule.resolvedHost) {
let uri = hostRule.matchHost;
uri =
is.string(uri) && isHttpUrl(uri)
? uri.replace(regEx(/^https?:/), '')
: // TODO: types (#22198)
`//${uri}/`;
if (hostRule.token) {
const key = hostRule.authType === 'Basic' ? '_auth' : '_authToken';
additionalNpmrcContent.push(`${uri}:${key}=${hostRule.token}`);
additionalYarnRcYml ||= { npmRegistries: {} };
if (hostRule.authType === 'Basic') {
additionalYarnRcYml.npmRegistries[uri] = {
npmAuthIdent: hostRule.token,
};
} else {
additionalYarnRcYml.npmRegistries[uri] = {
npmAuthToken: hostRule.token,
};
}
} else if (is.string(hostRule.username) && is.string(hostRule.password)) {
const password = toBase64(hostRule.password);
additionalNpmrcContent.push(`${uri}:username=${hostRule.username}`);
additionalNpmrcContent.push(`${uri}:_password=${password}`);
additionalYarnRcYml ||= { npmRegistries: {} };
additionalYarnRcYml.npmRegistries[uri] = {
npmAuthIdent: `${hostRule.username}:${hostRule.password}`,
if (!hostRule.resolvedHost) {
continue;
}

const matchedHost = hostRule.matchHost;
// Should never be necessary as if we have a resolvedHost, there has to be a matchHost
// istanbul ignore next
if (!matchedHost) {
continue;
}

const uri = `//${matchedHost}/`;
let cleanedUri = uri;
if (isHttpUrl(matchedHost)) {
cleanedUri = matchedHost.replace(regEx(/^https?:/), '');
}

if (hostRule.token) {
const key = hostRule.authType === 'Basic' ? '_auth' : '_authToken';
additionalNpmrcContent.push(`${cleanedUri}:${key}=${hostRule.token}`);

if (hostRule.authType === 'Basic') {
const registry = {
npmAuthIdent: hostRule.token,
};
additionalYarnRcYml.npmRegistries[cleanedUri] = registry;
additionalYarnRcYml.npmRegistries[uri] = registry;

continue;
}

const registry = {
npmAuthToken: hostRule.token,
};
additionalYarnRcYml.npmRegistries[cleanedUri] = registry;
additionalYarnRcYml.npmRegistries[uri] = registry;

continue;
}

if (is.string(hostRule.username) && is.string(hostRule.password)) {
rarkins marked this conversation as resolved.
Show resolved Hide resolved
const password = toBase64(hostRule.password);
additionalNpmrcContent.push(
`${cleanedUri}:username=${hostRule.username}`,
);
additionalNpmrcContent.push(`${cleanedUri}:_password=${password}`);

const registries = {
npmAuthIdent: `${hostRule.username}:${hostRule.password}`,
};
additionalYarnRcYml.npmRegistries[cleanedUri] = registries;
additionalYarnRcYml.npmRegistries[uri] = registries;
}
}
return { additionalNpmrcContent, additionalYarnRcYml };

const hasYarnRcNpmRegistries =
Object.keys(additionalYarnRcYml.npmRegistries).length > 0;
return {
additionalNpmrcContent,
additionalYarnRcYml: hasYarnRcNpmRegistries
? additionalYarnRcYml
: undefined,
};
}
7 changes: 7 additions & 0 deletions lib/modules/manager/npm/post-update/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export interface PnpmLockFile {
optionalDependencies: PnpmDependencySchema;
}

export interface YarnRcNpmRegistry {
npmAlwaysAuth?: boolean;
npmAuthIdent?: string;
npmAuthToken?: string;
}

export interface YarnRcYmlFile {
yarnPath?: string | null;
npmRegistries: Record<string, YarnRcNpmRegistry>;
}