From 9c950f557cdfa1d99096907f1f914fb3469c1c81 Mon Sep 17 00:00:00 2001 From: "David J. Felix" Date: Wed, 28 Aug 2019 20:03:12 -0400 Subject: [PATCH 1/2] Add setting for always-auth - https://docs.npmjs.com/misc/config#always-auth - Allow private repos for stuff like artifactory to work --- action.yml | 3 +++ lib/authutil.js | 9 +++++---- lib/setup-node.js | 3 ++- src/authutil.ts | 25 +++++++++++++++++++++---- src/setup-node.ts | 3 ++- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/action.yml b/action.yml index 49c9efe69..d6feb020a 100644 --- a/action.yml +++ b/action.yml @@ -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: + 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' diff --git a/lib/authutil.js b/lib/authutil.js index bb85fff30..1be060ac9 100644 --- a/lib/authutil.js +++ b/lib/authutil.js @@ -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, fileLocation: 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; @@ -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 diff --git a/lib/setup-node.js b/lib/setup-node.js index dc65699b4..aa2cf3063 100644 --- a/lib/setup-node.js +++ b/lib/setup-node.js @@ -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'); diff --git a/src/authutil.ts b/src/authutil.ts index 99f78efe1..0dbeb4870 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -4,7 +4,14 @@ import * as path from 'path'; import * as core from '@actions/core'; import * as github from '@actions/github'; -export function configAuthentication(registryUrl: string) { +interface ConfigureAuthenticationParams { + registryUrl: string; + alwaysAuth: string; +} +export function configAuthentication({ + registryUrl, + alwaysAuth +}: ConfigureAuthenticationParams) { const npmrc: string = path.resolve( process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc' @@ -13,10 +20,19 @@ export function configAuthentication(registryUrl: string) { registryUrl += '/'; } - writeRegistryToFile(registryUrl, npmrc); + writeRegistryToFile({registryUrl, fileLocation: npmrc, alwaysAuth}); } -function writeRegistryToFile(registryUrl: string, fileLocation: string) { +interface WriteRegistryToFileParams { + registryUrl: string; + fileLocation: string; + alwaysAuth: string; +} +function writeRegistryToFile({ + registryUrl, + fileLocation, + alwaysAuth +}: WriteRegistryToFileParams) { let scope: string = core.getInput('scope'); if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) { scope = github.context.repo.owner; @@ -45,7 +61,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 diff --git a/src/setup-node.ts b/src/setup-node.ts index f82413a0d..b3ee36d58 100644 --- a/src/setup-node.ts +++ b/src/setup-node.ts @@ -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 From 17f3fe18374cb57dea5cccaf37fd884c74f055c8 Mon Sep 17 00:00:00 2001 From: "David J. Felix" Date: Thu, 29 Aug 2019 19:36:24 -0400 Subject: [PATCH 2/2] Fix tests for always-auth --- __tests__/__snapshots__/authutil.test.ts.snap | 18 ++++++++++--- __tests__/authutil.test.ts | 14 +++++++--- lib/authutil.js | 6 ++--- lib/setup-node.js | 2 +- src/authutil.ts | 26 +++++-------------- src/setup-node.ts | 2 +- 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/__tests__/__snapshots__/authutil.test.ts.snap b/__tests__/__snapshots__/authutil.test.ts.snap index 3059617b6..c142cf4aa 100644 --- a/__tests__/__snapshots__/authutil.test.ts.snap +++ b/__tests__/__snapshots__/authutil.test.ts.snap @@ -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" `; diff --git a/__tests__/authutil.test.ts b/__tests__/authutil.test.ts index 2e88011e4..c75d5b65a 100644 --- a/__tests__/authutil.test.ts +++ b/__tests__/authutil.test.ts @@ -33,13 +33,13 @@ 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(); @@ -47,16 +47,22 @@ describe('installer tests', () => { 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(); + }); }); diff --git a/lib/authutil.js b/lib/authutil.js index 1be060ac9..6da4630b6 100644 --- a/lib/authutil.js +++ b/lib/authutil.js @@ -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, alwaysAuth }) { +function configAuthentication(registryUrl, alwaysAuth) { const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc'); if (!registryUrl.endsWith('/')) { registryUrl += '/'; } - writeRegistryToFile({ registryUrl, fileLocation: npmrc, alwaysAuth }); + writeRegistryToFile(registryUrl, npmrc, alwaysAuth); } exports.configAuthentication = configAuthentication; -function writeRegistryToFile({ registryUrl, fileLocation, alwaysAuth }) { +function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) { let scope = core.getInput('scope'); if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) { scope = github.context.repo.owner; diff --git a/lib/setup-node.js b/lib/setup-node.js index aa2cf3063..d7b35185e 100644 --- a/lib/setup-node.js +++ b/lib/setup-node.js @@ -37,7 +37,7 @@ function run() { const registryUrl = core.getInput('registry-url'); const alwaysAuth = core.getInput('always-auth'); if (registryUrl) { - auth.configAuthentication({ registryUrl, alwaysAuth }); + auth.configAuthentication(registryUrl, alwaysAuth); } // TODO: setup proxy from runner proxy config const matchersPath = path.join(__dirname, '..', '.github'); diff --git a/src/authutil.ts b/src/authutil.ts index 0dbeb4870..07e0b24cb 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -4,14 +4,7 @@ import * as path from 'path'; import * as core from '@actions/core'; import * as github from '@actions/github'; -interface ConfigureAuthenticationParams { - registryUrl: string; - alwaysAuth: string; -} -export function configAuthentication({ - registryUrl, - alwaysAuth -}: ConfigureAuthenticationParams) { +export function configAuthentication(registryUrl: string, alwaysAuth: string) { const npmrc: string = path.resolve( process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc' @@ -20,19 +13,14 @@ export function configAuthentication({ registryUrl += '/'; } - writeRegistryToFile({registryUrl, fileLocation: npmrc, alwaysAuth}); + writeRegistryToFile(registryUrl, npmrc, alwaysAuth); } -interface WriteRegistryToFileParams { - registryUrl: string; - fileLocation: string; - alwaysAuth: string; -} -function writeRegistryToFile({ - registryUrl, - fileLocation, - alwaysAuth -}: WriteRegistryToFileParams) { +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; diff --git a/src/setup-node.ts b/src/setup-node.ts index b3ee36d58..51deccbe2 100644 --- a/src/setup-node.ts +++ b/src/setup-node.ts @@ -21,7 +21,7 @@ async function run() { const registryUrl: string = core.getInput('registry-url'); const alwaysAuth: string = core.getInput('always-auth'); if (registryUrl) { - auth.configAuthentication({registryUrl, alwaysAuth}); + auth.configAuthentication(registryUrl, alwaysAuth); } // TODO: setup proxy from runner proxy config