Skip to content

Auth test #154

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

Closed
wants to merge 29 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -39,6 +39,35 @@ jobs:
- run: npm test
```

Set up auth with npm:
```yaml
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
version: '10.x'
registry-url: <registry url>
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
```

Set up auth with yarn:
```yaml
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
version: '10.x'
registry-url: <registry url>
- run: npm install -g yarn
- run: yarn install
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.YARN_TOKEN }}
```

# License

The scripts and documentation in this project are released under the [MIT License](LICENSE)
21 changes: 21 additions & 0 deletions __tests__/__snapshots__/authutil.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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/"
`;
62 changes: 62 additions & 0 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
13 changes: 2 additions & 11 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
@@ -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());
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -5,6 +5,10 @@ inputs:
version:
description: 'Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0, lts'
default: '10.x'
registry-url:
description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN'
scope:
description: 'Optional scope for authenticating against scoped registries'
runs:
using: 'node12'
main: 'lib/setup-node.js'
44 changes: 44 additions & 0 deletions lib/authutil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
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) {
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc);
}
exports.configAuthentication = configAuthentication;
function writeRegistryToFile(registryUrl, fileLocation) {
let scope = core.getInput('scope');
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
core.debug(`Setting auth in ${fileLocation}`);
let newContents = '';

// 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 += `${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');
}
5 changes: 5 additions & 0 deletions lib/setup-node.js
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const installer = __importStar(require("./installer"));
const auth = __importStar(require("./authutil"));
const path = __importStar(require("path"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
@@ -30,6 +31,10 @@ function run() {
// TODO: installer doesn't support proxy
yield installer.getNode(version);
}
const registryUrl = core.getInput('registry-url');
if (registryUrl) {
auth.configAuthentication(registryUrl);
}
// TODO: setup proxy from runner proxy config
const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);
15 changes: 15 additions & 0 deletions node_modules/.bin/which

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/.bin/which.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions node_modules/@actions/github/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions node_modules/@actions/github/lib/context.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions node_modules/@actions/github/lib/context.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/@actions/github/lib/context.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions node_modules/@actions/github/lib/github.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Oops, something went wrong.