From 38c86467c6b882339ae5466d3e07686daa34055b Mon Sep 17 00:00:00 2001 From: Tyler Watson Date: Wed, 26 Apr 2023 22:09:30 +1000 Subject: [PATCH] feature: support Azure DevOps Artifacts --- src/authutil.ts | 24 +++++++++++++++++++----- src/main.ts | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/authutil.ts b/src/authutil.ts index 2ce94939..04349be0 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -4,7 +4,11 @@ import * as path from 'path'; import * as core from '@actions/core'; import * as github from '@actions/github'; -export function configAuthentication(registryUrl: string, alwaysAuth: string) { +export function configAuthentication( + registryUrl: string, + alwaysAuth: string, + username?: string +) { const npmrc: string = path.resolve( process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc' @@ -13,13 +17,14 @@ export function configAuthentication(registryUrl: string, alwaysAuth: string) { registryUrl += '/'; } - writeRegistryToFile(registryUrl, npmrc, alwaysAuth); + writeRegistryToFile(registryUrl, npmrc, alwaysAuth, username); } function writeRegistryToFile( registryUrl: string, fileLocation: string, - alwaysAuth: string + alwaysAuth: string, + username?: string ) { let scope: string = core.getInput('scope'); if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) { @@ -44,8 +49,17 @@ function writeRegistryToFile( }); } // Remove http: or https: from front of registry. - const authString: string = - registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}'; + const registryPrefix = registryUrl.replace(/(^\w+:|^)/, ''); + + if (username) { + newContents += registryPrefix + `:_username=${username}${os.EOL}`; + newContents += registryPrefix + `:_email=dummy value`; + } + + const authString: string = username + ? registryPrefix + ':_password=${NODE_AUTH_TOKEN}' + : registryPrefix + ':_authToken=${NODE_AUTH_TOKEN}'; + const registryString = `${scope}registry=${registryUrl}`; const alwaysAuthString = `always-auth=${alwaysAuth}`; newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`; diff --git a/src/main.ts b/src/main.ts index 90cd1d9d..73027197 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,8 +55,9 @@ export async function run() { const registryUrl: string = core.getInput('registry-url'); const alwaysAuth: string = core.getInput('always-auth'); + const username: string | undefined = core.getInput('username'); if (registryUrl) { - auth.configAuthentication(registryUrl, alwaysAuth); + auth.configAuthentication(registryUrl, alwaysAuth, username); } if (cache && isCacheFeatureAvailable()) {