Skip to content

Add setting for always-auth #48

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

Merged
merged 2 commits into from
Sep 3, 2019
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
18 changes: 14 additions & 4 deletions __tests__/__snapshots__/authutil.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@

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

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

exports[`installer tests Configures scoped npm registries 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
@myscope:registry=https://registry.npmjs.org/"
@myscope:registry=https://registry.npmjs.org/
always-auth=false"
`;

exports[`installer tests Sets up npmrc for always-auth true 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/
always-auth=true"
`;

exports[`installer tests Sets up npmrc for npmjs 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/"
registry=https://registry.npmjs.org/
always-auth=false"
`;
14 changes: 10 additions & 4 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,36 @@ describe('installer tests', () => {
});

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

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

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

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

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

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

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

it('Sets up npmrc for always-auth true', async () => {
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
expect(fs.existsSync(rcFile)).toBe(true);
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
});
});
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: 'Setup Node.js environment'
description: 'Setup a Node.js environment and add it to the PATH, additionally providing proxy support'
author: 'GitHub'
inputs:
always-auth:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - this should go after scope in the parameter ordering. That way its grouped with the other auth parameters and comes after registry-url which is required in all auth cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counter nit: Opinion: At the top it leaves the map in alphabetical order.

description: 'Set always-auth in npmrc'
default: 'false'
node-version:
description: 'Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0'
default: '10.x'
Expand Down
9 changes: 5 additions & 4 deletions lib/authutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const os = __importStar(require("os"));
const path = __importStar(require("path"));
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
function configAuthentication(registryUrl) {
function configAuthentication(registryUrl, alwaysAuth) {
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc);
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
}
exports.configAuthentication = configAuthentication;
function writeRegistryToFile(registryUrl, fileLocation) {
function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
let scope = core.getInput('scope');
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
Expand All @@ -47,7 +47,8 @@ function writeRegistryToFile(registryUrl, fileLocation) {
const registryString = scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
newContents += `${authString}${os.EOL}${registryString}`;
const alwaysAuthString = `always-auth=${alwaysAuth}`;
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
fs.writeFileSync(fileLocation, newContents);
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
// Export empty node_auth_token so npm doesn't complain about not being able to find it
Expand Down
3 changes: 2 additions & 1 deletion lib/setup-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ function run() {
yield installer.getNode(version);
}
const registryUrl = core.getInput('registry-url');
const alwaysAuth = core.getInput('always-auth');
if (registryUrl) {
auth.configAuthentication(registryUrl);
auth.configAuthentication(registryUrl, alwaysAuth);
}
// TODO: setup proxy from runner proxy config
const matchersPath = path.join(__dirname, '..', '.github');
Expand Down
13 changes: 9 additions & 4 deletions src/authutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
import * as core from '@actions/core';
import * as github from '@actions/github';

export function configAuthentication(registryUrl: string) {
export function configAuthentication(registryUrl: string, alwaysAuth: string) {
const npmrc: string = path.resolve(
process.env['RUNNER_TEMP'] || process.cwd(),
'.npmrc'
Expand All @@ -13,10 +13,14 @@ export function configAuthentication(registryUrl: string) {
registryUrl += '/';
}

writeRegistryToFile(registryUrl, npmrc);
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
}

function writeRegistryToFile(registryUrl: string, fileLocation: string) {
function writeRegistryToFile(
registryUrl: string,
fileLocation: string,
alwaysAuth: string
) {
let scope: string = core.getInput('scope');
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
Expand Down Expand Up @@ -45,7 +49,8 @@ function writeRegistryToFile(registryUrl: string, fileLocation: string) {
const registryString: string = scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
newContents += `${authString}${os.EOL}${registryString}`;
const alwaysAuthString: string = `always-auth=${alwaysAuth}`;
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
fs.writeFileSync(fileLocation, newContents);
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
// Export empty node_auth_token so npm doesn't complain about not being able to find it
Expand Down
3 changes: 2 additions & 1 deletion src/setup-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ async function run() {
}

const registryUrl: string = core.getInput('registry-url');
const alwaysAuth: string = core.getInput('always-auth');
if (registryUrl) {
auth.configAuthentication(registryUrl);
auth.configAuthentication(registryUrl, alwaysAuth);
}

// TODO: setup proxy from runner proxy config
Expand Down