-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add auth support #21
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
Add auth support #21
Changes from all commits
2b9c956
409b7df
0930c11
dd1cda5
287437b
9776256
f338d85
8e12aec
920661f
a9f1343
feb12fe
6b65ca8
1be350f
985b557
c09ef15
3c9d735
da3e599
f20c85e
ff5ec86
e7609c8
c970c05
b9164e8
4d381b1
0dfe4cf
d157c73
869ebd9
f1da8b8
40a00ae
6b34be1
6e4a792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/" | ||
| `; |
| 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(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| "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 = ''; | ||
| 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 += `${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'); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right. Only the line(s) that is being redefined should be removed.
registrydefines the default registry for unscoped dependencies, whereas if scope is provided this function is (re)defining a scoped registry.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @hross