Skip to content
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

fix: #424 - Publish in custom artifactory #425

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-lies-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

fix: Publish in custom artifactory #424 with `auth` or `authToken`
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have mentioned that the previous version of the code goes in to the else branch and that creates a problem. I don't understand this. Even if it goes there... it will just append a different token to ur .npmrc file and that's about it. Your correct token should still be used when publishing to a custom artifactory

if (authLine) {
core.info(
"Found existing auth token for the npm registry in the user .npmrc file"
Expand Down
96 changes: 95 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getChangelogEntry, BumpLevels, sortTheThings } from "./utils";
import { getChangelogEntry, BumpLevels, sortTheThings, extractAuthTokenLine } from "./utils";

let changelog = `# @keystone-alpha/email

Expand Down Expand Up @@ -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();
});
});
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};