From 8669687d7d94a625a5a2f1f5796624ff35b77787 Mon Sep 17 00:00:00 2001 From: Hari Haran Date: Tue, 19 Nov 2024 07:40:29 +0000 Subject: [PATCH 1/2] fix: #424 - Publish in custom artifactory --- src/index.ts | 6 ++--- src/utils.test.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++++- src/utils.ts | 17 +++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 194204dc..d4fd1a51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import fs from "fs-extra"; import * as gitUtils from "./gitUtils"; import { runPublish, runVersion } from "./run"; import readChangesetState from "./readChangesetState"; +import { extractAuthTokenLine } from "./utils"; const getOptionalInput = (name: string) => core.getInput(name) || undefined; @@ -59,10 +60,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; if (fs.existsSync(userNpmrcPath)) { core.info("Found existing user .npmrc file"); const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8"); - const authLine = userNpmrcContent.split("\n").find((line) => { - // check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 - return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line); - }); + const authLine = extractAuthTokenLine(userNpmrcContent); if (authLine) { core.info( "Found existing auth token for the npm registry in the user .npmrc file" diff --git a/src/utils.test.ts b/src/utils.test.ts index 1633b891..6cb0bf78 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,4 +1,4 @@ -import { getChangelogEntry, BumpLevels, sortTheThings } from "./utils"; +import { getChangelogEntry, BumpLevels, sortTheThings, extractAuthTokenLine } from "./utils"; let changelog = `# @keystone-alpha/email @@ -99,3 +99,62 @@ test("it sorts the things right", () => { ]; expect(things.sort(sortTheThings)).toMatchSnapshot(); }); + +/** + * Test the extractAuthTokenLine function for various registries. + */ +describe("extractAuthTokenLine", () => { + it("should correctly find the auth token line for multiple registries", () => { + const testCases = [ + { + name: "Custom private registry", + npmrc: ` + registry=https://custom.private-registry.com/api/npm/npm/ + //custom.private-registry.com/api/npm/npm/:_authToken=abcd1234 + always-auth=true + `, + expected: "//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234", + }, + { + name: "NPM default registry", + npmrc: ` + registry=https://registry.npmjs.org/ + //registry.npmjs.org/:_authToken=efgh5678 + `, + expected: "//registry.npmjs.org/:_authToken=efgh5678", + }, + { + name: "AWS CodeArtifact registry", + npmrc: ` + registry=https://mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/ + //mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012 + `, + expected: + "//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012", + }, + { + name: "Azure DevOps registry", + npmrc: ` + registry=https://pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/ + //pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456 + `, + expected: + "//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456", + }, + ]; + + testCases.forEach(({ name, npmrc, expected }) => { + const result = extractAuthTokenLine(npmrc); + expect(result).toBe(expected); + }); + }); + + it("should return undefined if no auth token line is present", () => { + const npmrcContent = ` + registry=https://custom.private-registry.com/api/npm/npm/ + always-auth=true + `; + const result = extractAuthTokenLine(npmrcContent); + expect(result).toBeUndefined(); + }); +}); diff --git a/src/utils.ts b/src/utils.ts index e92a1953..12db03f2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -98,3 +98,20 @@ export function sortTheThings( } return -1; } + +/** + * Extracts the line containing the auth token from .npmrc content. + * + * @param {string} npmrcContent - The content of the .npmrc file as a string. + * @returns {string | undefined} - The line containing the auth token or undefined if not found. + */ +export function extractAuthTokenLine(npmrcContent:string) { + /** + * @see https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 + * Also dynamically adapt to any registry by looking for :_authToken= pattern + */ + const line = npmrcContent.split("\n").find((line) => { + return /^\s*\/\/.*\/:[_-]authToken=/i.test(line); + }); + return line ? line.trim() : undefined; +}; \ No newline at end of file From c2796d4e4ccb62b0b9bf0e77cced8afccdbe7f9a Mon Sep 17 00:00:00 2001 From: Hari Haran Date: Thu, 21 Nov 2024 10:20:58 +0000 Subject: [PATCH 2/2] fix: Publish in custom artifactory #424 with `auth` or `authToken` --- .changeset/sharp-lies-shop.md | 5 ++++ src/utils.test.ts | 45 +++++++++++++++++++++++++++++++---- src/utils.ts | 10 ++++---- 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 .changeset/sharp-lies-shop.md diff --git a/.changeset/sharp-lies-shop.md b/.changeset/sharp-lies-shop.md new file mode 100644 index 00000000..1c716fb3 --- /dev/null +++ b/.changeset/sharp-lies-shop.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": patch +--- + +fix: Publish in custom artifactory #424 with `auth` or `authToken` diff --git a/src/utils.test.ts b/src/utils.test.ts index 6cb0bf78..aac6bba7 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -107,7 +107,7 @@ describe("extractAuthTokenLine", () => { it("should correctly find the auth token line for multiple registries", () => { const testCases = [ { - name: "Custom private registry", + name: "Custom private registry with _authToken", npmrc: ` registry=https://custom.private-registry.com/api/npm/npm/ //custom.private-registry.com/api/npm/npm/:_authToken=abcd1234 @@ -116,7 +116,16 @@ describe("extractAuthTokenLine", () => { expected: "//custom.private-registry.com/api/npm/npm/:_authToken=abcd1234", }, { - name: "NPM default registry", + name: "Custom private registry with _auth", + npmrc: ` + registry=https://custom.private-registry.com/api/npm/npm/ + //custom.private-registry.com/api/npm/npm/:_auth=abcd1234 + always-auth=true + `, + expected: "//custom.private-registry.com/api/npm/npm/:_auth=abcd1234", + }, + { + name: "NPM default registry with _authToken", npmrc: ` registry=https://registry.npmjs.org/ //registry.npmjs.org/:_authToken=efgh5678 @@ -124,7 +133,15 @@ describe("extractAuthTokenLine", () => { expected: "//registry.npmjs.org/:_authToken=efgh5678", }, { - name: "AWS CodeArtifact registry", + name: "NPM default registry with _auth", + npmrc: ` + registry=https://registry.npmjs.org/ + //registry.npmjs.org/:_auth=efgh5678 + `, + expected: "//registry.npmjs.org/:_auth=efgh5678", + }, + { + name: "AWS CodeArtifact registry with _authToken", npmrc: ` registry=https://mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/ //mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012 @@ -133,7 +150,16 @@ describe("extractAuthTokenLine", () => { "//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012", }, { - name: "Azure DevOps registry", + name: "AWS CodeArtifact registry with _auth", + npmrc: ` + registry=https://mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/ + //mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_auth=ijkl9012 + `, + expected: + "//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_auth=ijkl9012", + }, + { + name: "Azure DevOps registry with _authToken", npmrc: ` registry=https://pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/ //pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456 @@ -141,6 +167,15 @@ describe("extractAuthTokenLine", () => { expected: "//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_authToken=mnop3456", }, + { + name: "Azure DevOps registry with _auth", + npmrc: ` + registry=https://pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/ + //pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_auth=mnop3456 + `, + expected: + "//pkgs.dev.azure.com/myorg/_packaging/myfeed/npm/registry/:_auth=mnop3456", + }, ]; testCases.forEach(({ name, npmrc, expected }) => { @@ -157,4 +192,4 @@ describe("extractAuthTokenLine", () => { const result = extractAuthTokenLine(npmrcContent); expect(result).toBeUndefined(); }); -}); +}); \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 12db03f2..2f86b492 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -105,13 +105,13 @@ export function sortTheThings( * @param {string} npmrcContent - The content of the .npmrc file as a string. * @returns {string | undefined} - The line containing the auth token or undefined if not found. */ -export function extractAuthTokenLine(npmrcContent:string) { +export function extractAuthTokenLine(npmrcContent: string) { /** * @see https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 - * Also dynamically adapt to any registry by looking for :_authToken= pattern + * This regex dynamically adapts to any registry by looking for the :_auth or :_authToken= pattern. */ - const line = npmrcContent.split("\n").find((line) => { - return /^\s*\/\/.*\/:[_-]authToken=/i.test(line); + const line = npmrcContent.split("\n").find((line) => { + return /^\s*\/\/.*\/:(_auth|_authToken)=/i.test(line); // Match both _auth and _authToken }); - return line ? line.trim() : undefined; + return line ? line.trim() : undefined; }; \ No newline at end of file