1
0

Compare commits

...

10 Commits

Author SHA1 Message Date
Danny McCormick
ae3937cacb Update authutil.js 2019-08-06 16:39:01 -04:00
Danny McCormick
40a00ae373 Add tests 2019-08-06 15:27:51 -04:00
Danny McCormick
f1da8b89e2 Try changes again 2019-08-06 14:46:42 -04:00
Danny McCormick
869ebd91d5 Remove auth token 2019-08-06 14:25:05 -04:00
Danny McCormick
d157c73da6 Try just adding auth token 2019-08-06 14:17:51 -04:00
Danny McCormick
0dfe4cf4d4 Keep in root of repo 2019-08-06 14:14:58 -04:00
Danny McCormick
4d381b188a Use userconfig and append trailing slash 2019-08-06 14:08:18 -04:00
Iheanyi Ekechukwu
b9164e8546 Fix the registry string. 2019-08-06 12:23:49 -04:00
Iheanyi Ekechukwu
c970c05a19 Add single quotes for authString 2019-08-06 12:07:22 -04:00
Danny McCormick
e7609c8e84 Update authutil.js 2019-08-06 11:52:37 -04:00
5 changed files with 103 additions and 36 deletions

View File

@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`installer tests Appends trailing slash to registry 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/"
`;
exports[`installer tests Automatically configures GPR scope 1`] = `
"npm.pkg.github.com/:_authToken=\${NODE_AUTH_TOKEN}
@owner:registry=npm.pkg.github.com/"
`;
exports[`installer tests Configures scoped npm registries 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
@myScope:registry=https://registry.npmjs.org/"
`;
exports[`installer tests Sets up npmrc for npmjs 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/"
`;

View File

@@ -0,0 +1,62 @@
import io = require('@actions/io');
import fs = require('fs');
import path = require('path');
const tempDir = path.join(
__dirname,
'runner',
path.join(
Math.random()
.toString(36)
.substring(7)
),
'temp'
);
const rcFile = path.join(tempDir, '.npmrc');
process.env['GITHUB_REPOSITORY'] = 'owner/repo';
process.env['RUNNER_TEMP'] = tempDir;
import * as auth from '../src/authutil';
describe('installer tests', () => {
beforeAll(async () => {
await io.rmRF(tempDir);
await io.mkdirP(tempDir);
}, 100000);
beforeEach(() => {
if (fs.existsSync(rcFile)) {
fs.unlinkSync(rcFile);
}
process.env['INPUT_SCOPE'] = '';
});
it('Sets up npmrc for npmjs', async () => {
await auth.configAuthentication('https://registry.npmjs.org/');
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');
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');
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');
expect(fs.existsSync(rcFile)).toBe(true);
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
});
});

View File

@@ -4,7 +4,7 @@ import os = require('os');
import path = require('path');
const toolDir = path.join(
process.cwd(),
__dirname,
'runner',
path.join(
Math.random()
@@ -14,7 +14,7 @@ const toolDir = path.join(
'tools'
);
const tempDir = path.join(
process.cwd(),
__dirname,
'runner',
path.join(
Math.random()
@@ -36,15 +36,6 @@ describe('installer tests', () => {
await io.rmRF(tempDir);
}, 100000);
afterAll(async () => {
try {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
} catch {
console.log('Failed to remove test directories');
}
}, 100000);
it('Acquires version of node if no matching version is installed', async () => {
await installer.getNode('10.16.0');
const nodeDir = path.join(toolDir, 'node', '10.16.0', os.arch());

View File

@@ -13,11 +13,10 @@ const path = __importStar(require("path"));
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
function configAuthentication(registryUrl) {
// const npmrc: string = path.resolve(
// process.env['RUNNER_TEMP'] || process.cwd(),
// '.npmrc'
// );
const npmrc = path.resolve(process.cwd(), '.npmrc');
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc);
}
exports.configAuthentication = configAuthentication;
@@ -31,23 +30,15 @@ function writeRegistryToFile(registryUrl, fileLocation) {
}
core.debug(`Setting auth in ${fileLocation}`);
let newContents = '';
if (fs.existsSync(fileLocation)) {
const curContents = fs.readFileSync(fileLocation, 'utf8');
curContents.split(os.EOL).forEach((line) => {
// Add current contents unless they are setting the registry
if (!line.toLowerCase().startsWith('registry')) {
newContents += line + os.EOL;
}
});
}
// Remove http: or https: from front of registry.
const authString = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
const registryString = scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
newContents += `${registryString}${os.EOL}${authString}`;
newContents += `${authString}${os.EOL}${registryString}`;
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
// core.exportVariable('NODE_AUTH_TOKEN', 'XXXXX-XXXXX-XXXXX-XXXXX');
core.exportVariable('NODE_AUTH_TOKEN', 'XXXXX-XXXXX-XXXXX-XXXXX');
}

View File

@@ -5,11 +5,13 @@ import * as core from '@actions/core';
import * as github from '@actions/github';
export function configAuthentication(registryUrl: string) {
// const npmrc: string = path.resolve(
// process.env['RUNNER_TEMP'] || process.cwd(),
// '.npmrc'
// );
const npmrc: string = path.resolve(process.cwd(), '.npmrc');
const npmrc: string = path.resolve(
process.env['RUNNER_TEMP'] || process.cwd(),
'.npmrc'
);
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc);
}
@@ -36,13 +38,13 @@ function writeRegistryToFile(registryUrl: string, fileLocation: string) {
}
// Remove http: or https: from front of registry.
const authString =
registryUrl.replace(/(^\w+:|^)/, '') + `:_authToken=${NODE_AUTH_TOKEN}`;
registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
const registryString = scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
newContents += `${registryString}${os.EOL}${authString}`;
newContents += `${authString}${os.EOL}${registryString}`;
fs.writeFileSync(fileLocation, newContents);
// core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
// Export empty node_auth_token so npm doesn't complain about not being able to find it
// core.exportVariable('NODE_AUTH_TOKEN', 'XXXXX-XXXXX-XXXXX-XXXXX');
core.exportVariable('NODE_AUTH_TOKEN', 'XXXXX-XXXXX-XXXXX-XXXXX');
}