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/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..aac6bba7 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,97 @@ 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 with _authToken", + 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: "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 + `, + expected: "//registry.npmjs.org/:_authToken=efgh5678", + }, + { + 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 + `, + expected: + "//mydomain-111122223333.d.codeartifact.us-east-1.amazonaws.com/npm/repository-name/:_authToken=ijkl9012", + }, + { + 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 + `, + 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 }) => { + 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(); + }); +}); \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index e92a1953..2f86b492 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 + * This regex dynamically adapts to any registry by looking for the :_auth or :_authToken= pattern. + */ + const line = npmrcContent.split("\n").find((line) => { + return /^\s*\/\/.*\/:(_auth|_authToken)=/i.test(line); // Match both _auth and _authToken + }); + return line ? line.trim() : undefined; +}; \ No newline at end of file