Skip to content

Commit 7a3ce83

Browse files
DavidJFelixDanny McCormick
authored and
Danny McCormick
committed
Add setting for always-auth (#48)
* Add setting for always-auth - https://docs.npmjs.com/misc/config#always-auth - Allow private repos for stuff like artifactory to work * Fix tests for always-auth
1 parent 57adacb commit 7a3ce83

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

__tests__/__snapshots__/authutil.test.ts.snap

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@
22

33
exports[`installer tests Appends trailing slash to registry 1`] = `
44
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
5-
registry=https://registry.npmjs.org/"
5+
registry=https://registry.npmjs.org/
6+
always-auth=false"
67
`;
78

89
exports[`installer tests Automatically configures GPR scope 1`] = `
910
"npm.pkg.github.com/:_authToken=\${NODE_AUTH_TOKEN}
10-
@ownername:registry=npm.pkg.github.com/"
11+
@ownername:registry=npm.pkg.github.com/
12+
always-auth=false"
1113
`;
1214

1315
exports[`installer tests Configures scoped npm registries 1`] = `
1416
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
15-
@myscope:registry=https://registry.npmjs.org/"
17+
@myscope:registry=https://registry.npmjs.org/
18+
always-auth=false"
19+
`;
20+
21+
exports[`installer tests Sets up npmrc for always-auth true 1`] = `
22+
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
23+
registry=https://registry.npmjs.org/
24+
always-auth=true"
1625
`;
1726

1827
exports[`installer tests Sets up npmrc for npmjs 1`] = `
1928
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
20-
registry=https://registry.npmjs.org/"
29+
registry=https://registry.npmjs.org/
30+
always-auth=false"
2131
`;

__tests__/authutil.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,36 @@ describe('installer tests', () => {
3333
});
3434

3535
it('Sets up npmrc for npmjs', async () => {
36-
await auth.configAuthentication('https://registry.npmjs.org/');
36+
await auth.configAuthentication('https://registry.npmjs.org/', 'false');
3737
expect(fs.existsSync(rcFile)).toBe(true);
3838
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
3939
});
4040

4141
it('Appends trailing slash to registry', async () => {
42-
await auth.configAuthentication('https://registry.npmjs.org');
42+
await auth.configAuthentication('https://registry.npmjs.org', 'false');
4343

4444
expect(fs.existsSync(rcFile)).toBe(true);
4545
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
4646
});
4747

4848
it('Configures scoped npm registries', async () => {
4949
process.env['INPUT_SCOPE'] = 'myScope';
50-
await auth.configAuthentication('https://registry.npmjs.org');
50+
await auth.configAuthentication('https://registry.npmjs.org', 'false');
5151

5252
expect(fs.existsSync(rcFile)).toBe(true);
5353
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
5454
});
5555

5656
it('Automatically configures GPR scope', async () => {
57-
await auth.configAuthentication('npm.pkg.github.com');
57+
await auth.configAuthentication('npm.pkg.github.com', 'false');
5858

5959
expect(fs.existsSync(rcFile)).toBe(true);
6060
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
6161
});
62+
63+
it('Sets up npmrc for always-auth true', async () => {
64+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
65+
expect(fs.existsSync(rcFile)).toBe(true);
66+
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
67+
});
6268
});

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: 'Setup Node.js environment'
22
description: 'Setup a Node.js environment and add it to the PATH, additionally providing proxy support'
33
author: 'GitHub'
44
inputs:
5+
always-auth:
6+
description: 'Set always-auth in npmrc'
7+
default: 'false'
58
node-version:
69
description: 'Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0'
710
default: '10.x'

lib/authutil.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const os = __importStar(require("os"));
1212
const path = __importStar(require("path"));
1313
const core = __importStar(require("@actions/core"));
1414
const github = __importStar(require("@actions/github"));
15-
function configAuthentication(registryUrl) {
15+
function configAuthentication(registryUrl, alwaysAuth) {
1616
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
1717
if (!registryUrl.endsWith('/')) {
1818
registryUrl += '/';
1919
}
20-
writeRegistryToFile(registryUrl, npmrc);
20+
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
2121
}
2222
exports.configAuthentication = configAuthentication;
23-
function writeRegistryToFile(registryUrl, fileLocation) {
23+
function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
2424
let scope = core.getInput('scope');
2525
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
2626
scope = github.context.repo.owner;
@@ -47,7 +47,8 @@ function writeRegistryToFile(registryUrl, fileLocation) {
4747
const registryString = scope
4848
? `${scope}:registry=${registryUrl}`
4949
: `registry=${registryUrl}`;
50-
newContents += `${authString}${os.EOL}${registryString}`;
50+
const alwaysAuthString = `always-auth=${alwaysAuth}`;
51+
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
5152
fs.writeFileSync(fileLocation, newContents);
5253
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
5354
// Export empty node_auth_token so npm doesn't complain about not being able to find it

lib/setup-node.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ function run() {
3535
yield installer.getNode(version);
3636
}
3737
const registryUrl = core.getInput('registry-url');
38+
const alwaysAuth = core.getInput('always-auth');
3839
if (registryUrl) {
39-
auth.configAuthentication(registryUrl);
40+
auth.configAuthentication(registryUrl, alwaysAuth);
4041
}
4142
// TODO: setup proxy from runner proxy config
4243
const matchersPath = path.join(__dirname, '..', '.github');

src/authutil.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import * as core from '@actions/core';
55
import * as github from '@actions/github';
66

7-
export function configAuthentication(registryUrl: string) {
7+
export function configAuthentication(registryUrl: string, alwaysAuth: string) {
88
const npmrc: string = path.resolve(
99
process.env['RUNNER_TEMP'] || process.cwd(),
1010
'.npmrc'
@@ -13,10 +13,14 @@ export function configAuthentication(registryUrl: string) {
1313
registryUrl += '/';
1414
}
1515

16-
writeRegistryToFile(registryUrl, npmrc);
16+
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
1717
}
1818

19-
function writeRegistryToFile(registryUrl: string, fileLocation: string) {
19+
function writeRegistryToFile(
20+
registryUrl: string,
21+
fileLocation: string,
22+
alwaysAuth: string
23+
) {
2024
let scope: string = core.getInput('scope');
2125
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
2226
scope = github.context.repo.owner;
@@ -45,7 +49,8 @@ function writeRegistryToFile(registryUrl: string, fileLocation: string) {
4549
const registryString: string = scope
4650
? `${scope}:registry=${registryUrl}`
4751
: `registry=${registryUrl}`;
48-
newContents += `${authString}${os.EOL}${registryString}`;
52+
const alwaysAuthString: string = `always-auth=${alwaysAuth}`;
53+
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
4954
fs.writeFileSync(fileLocation, newContents);
5055
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
5156
// Export empty node_auth_token so npm doesn't complain about not being able to find it

src/setup-node.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ async function run() {
1919
}
2020

2121
const registryUrl: string = core.getInput('registry-url');
22+
const alwaysAuth: string = core.getInput('always-auth');
2223
if (registryUrl) {
23-
auth.configAuthentication(registryUrl);
24+
auth.configAuthentication(registryUrl, alwaysAuth);
2425
}
2526

2627
// TODO: setup proxy from runner proxy config

0 commit comments

Comments
 (0)