Skip to content

feat: add support for npm Trusted Publishers with NPM_ID_TOKEN - #227

Open
meesvandongen wants to merge 2 commits into
un-ts:mainfrom
meesvandongen:main
Open

feat: add support for npm Trusted Publishers with NPM_ID_TOKEN#227
meesvandongen wants to merge 2 commits into
un-ts:mainfrom
meesvandongen:main

Conversation

@meesvandongen

@meesvandongen meesvandongen commented Oct 5, 2025

Copy link
Copy Markdown
Contributor

Important

Add support for npm Trusted Publishers using NPM_ID_TOKEN, modifying .npmrc creation and updating documentation.

  • New Features:
    • Support for npm Trusted Publishers using NPM_ID_TOKEN in src/main.ts.
    • Skips .npmrc creation if NPM_ID_TOKEN is detected.
    • Retains .npmrc creation with NPM_TOKEN if no .npmrc exists.
  • Error Handling:
    • Improved error messages in src/main.ts for missing tokens.
  • Documentation:
    • Updated README.md to include setup for Trusted Publishers and classic token flows.
    • Clarified .npmrc creation behavior in README.md.

This description was created by Ellipsis for 1e3d823. You can customize this summary. It will automatically update as commits are pushed.


Summary by CodeRabbit

  • New Features

    • Added support for npm Trusted Publishers using NPM_ID_TOKEN, skipping .npmrc creation when present.
    • Kept the classic automation token flow using NPM_TOKEN, including automatic .npmrc creation when required.
    • Improved authentication handling and error messaging when required tokens are missing.
  • Documentation

    • Updated the setup guide to explain both publishing methods side-by-side.
    • Added/adjusted CI configuration examples and clarified .npmrc behavior for each mode.

@changeset-bot

changeset-bot Bot commented Oct 5, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dfb9b67

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
changesets-gitlab Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Oct 5, 2025

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

README documents Trusted Publishers authentication with NPM_ID_TOKEN and classic token authentication with NPM_TOKEN. The publish flow skips .npmrc creation for NPM_ID_TOKEN, creates it for NPM_TOKEN, and fails when neither is available.

Changes

npm authentication

Layer / File(s) Summary
Authentication contract and publish branching
src/types.ts, src/main.ts
Env accepts NPM_ID_TOKEN; publishing selects Trusted Publishers, classic token handling, or an explicit missing-token failure.
Authentication documentation and release metadata
README.md, .changeset/yummy-hairs-enjoy.md
README describes both authentication methods and conditional .npmrc behavior; a minor changeset records Trusted Publishers support.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CI as CI Runner
  participant Action as Publish Action
  participant FS as File System
  participant NPM as npm Registry

  CI->>Action: Invoke publish
  Action->>FS: Check for .npmrc
  alt NPM_ID_TOKEN present
    Action->>NPM: Publish without creating .npmrc
  else NPM_TOKEN present
    Action->>FS: Create .npmrc with NPM_TOKEN
    Action->>NPM: Publish
  else Neither token present
    Action-->>CI: Fail with missing-token error
  end
Loading

Suggested reviewers: jounqin

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: npm Trusted Publishers support via NPM_ID_TOKEN.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Fix failing CI checks
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codesandbox-ci

codesandbox-ci Bot commented Oct 5, 2025

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2025

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/changesets-gitlab@227

commit: dfb9b67

@ellipsis-dev ellipsis-dev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to d8321bc in 1 minute and 1 seconds. Click for details.
  • Reviewed 84 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 4 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. README.md:92
  • Draft comment:
    Clear documentation for npm Trusted Publishers added. Consider clarifying what happens if both NPM_ID_TOKEN and NPM_TOKEN are set (i.e. that NPM_ID_TOKEN takes precedence).
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
2. src/main.ts:25
  • Draft comment:
    NPM_ID_TOKEN is correctly destructured from env. Add a brief inline comment noting that if both tokens are provided, NPM_ID_TOKEN is prioritized.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
3. src/main.ts:72
  • Draft comment:
    The conditional branch for .npmrc creation handles Trusted Publishers mode well. Consider logging a warning if both NPM_ID_TOKEN and NPM_TOKEN are set, to make the precedence explicit for users.
  • Reason this comment was not posted:
    Confidence changes required: 50% <= threshold 50% None
4. src/types.ts:31
  • Draft comment:
    Addition of the NPM_ID_TOKEN field in the Env type is clear and properly documented.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_o3nypJU0XSTncZRE

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main.ts (1)

69-88: Consider extracting .npmrc handling to reduce cognitive complexity.

The cognitive complexity is slightly above the threshold (16 vs 15). While the code is readable, extracting the .npmrc handling logic into a separate function could improve maintainability.

Example refactor:

async function ensureNpmrc(
  npmrcPath: string,
  NPM_ID_TOKEN: string | undefined,
  NPM_TOKEN: string | undefined
): Promise<void> {
  if (fs.existsSync(npmrcPath)) {
    console.log('Found existing .npmrc file')
  } else if (NPM_ID_TOKEN) {
    console.log(
      'Detected `NPM_ID_TOKEN`; skipping `.npmrc` creation (Trusted Publishers mode).',
    )
  } else if (NPM_TOKEN) {
    console.log('No .npmrc file found, creating one with `NPM_TOKEN`')
    await fs.promises.writeFile(
      npmrcPath,
      `//registry.npmjs.org/:_authToken=${NPM_TOKEN}`,
    )
  } else {
    setFailed(
      'No `.npmrc` found and neither `NPM_TOKEN` nor `NPM_ID_TOKEN` provided, unable to publish packages',
    )
    throw new Error('No npm authentication available')
  }
}

Then in the main function:

try {
  await ensureNpmrc(npmrcPath, NPM_ID_TOKEN, NPM_TOKEN)
} catch {
  return
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eccf474 and d8321bc.

📒 Files selected for processing (3)
  • README.md (2 hunks)
  • src/main.ts (2 hunks)
  • src/types.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/main.ts (1)
src/env.ts (2)
  • env (10-33)
  • GITLAB_TOKEN (24-32)
🪛 GitHub Actions: CI
src/main.ts

[error] 24-29: SonarJS cognitive complexity limit exceeded. Refactor this function to reduce cognitive complexity from 16 to ≤15.

README.md

[warning] 1-1: Unexpected GFM autolink literal, expected regular autolink; add '<' before and '>' after

🪛 GitHub Check: Lint and Test with Node.js 18
README.md

[warning] 94-94:
{"reason":"Unexpected GFM autolink literal, expected regular autolink, add < before and > after","source":"remark-lint","ruleId":"no-literal-urls","severity":1}

🪛 GitHub Check: Lint and Test with Node.js 20
README.md

[warning] 94-94:
{"reason":"Unexpected GFM autolink literal, expected regular autolink, add < before and > after","source":"remark-lint","ruleId":"no-literal-urls","severity":1}

🪛 GitHub Check: Lint and Test with Node.js 22
README.md

[warning] 94-94:
{"reason":"Unexpected GFM autolink literal, expected regular autolink, add < before and > after","source":"remark-lint","ruleId":"no-literal-urls","severity":1}

🪛 markdownlint-cli2 (0.18.1)
README.md

94-94: Bare URL used

(MD034, no-bare-urls)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (8)
src/types.ts (1)

30-32: LGTM! Clear type definition with helpful comments.

The addition of NPM_ID_TOKEN is well-documented and aligns with the implementation in main.ts where this token is used to skip .npmrc creation in Trusted Publishers mode.

src/main.ts (4)

25-25: LGTM!

Correct destructuring of the new NPM_ID_TOKEN environment variable.


72-76: LGTM! Correct Trusted Publishers detection.

The logic correctly detects NPM_ID_TOKEN and skips .npmrc creation, allowing npm to handle the OIDC token exchange internally.


78-78: LGTM! Improved log message clarity.

Explicitly mentioning NPM_TOKEN helps users understand which authentication method is being used.


85-85: LGTM! Clear error message.

The error message now correctly indicates both authentication options, helping users understand what's missing.

README.md (3)

92-93: LGTM! Clear documentation of authentication methods.

The documentation accurately describes both authentication approaches and provides helpful guidance for users.

Also applies to: 95-96


122-122: LGTM! Clear clarification of .npmrc behavior.

The note accurately explains when the .npmrc file is created in classic token mode.


129-129: LGTM! Helpful note about Trusted Publishers.

The note clearly explains that no .npmrc is required when using Trusted Publishers with NPM_ID_TOKEN.

Comment thread README.md Outdated
@meesvandongen
meesvandongen marked this pull request as draft October 5, 2025 19:16
@sonarqubecloud

sonarqubecloud Bot commented Oct 5, 2025

Copy link
Copy Markdown

@meesvandongen
meesvandongen marked this pull request as ready for review October 5, 2025 19:24

@ellipsis-dev ellipsis-dev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 1e3d823 in 38 seconds. Click for details.
  • Reviewed 95 lines of code in 4 files
  • Skipped 0 files when reviewing.
  • Skipped posting 4 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. .changeset/yummy-hairs-enjoy.md:2
  • Draft comment:
    Changeset file for a minor update looks good. Ensure that the minor version bump is intended.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. README.md:92
  • Draft comment:
    The README update clearly explains both authentication methods. Consider verifying the formatting of the provided cat << EOF code block to avoid unintended whitespace in the generated .npmrc.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
3. src/main.ts:72
  • Draft comment:
    The token handling logic now prioritizes NPM_ID_TOKEN over NPM_TOKEN by skipping .npmrc creation when NPM_ID_TOKEN is set. Consider adding a code comment to document that if both tokens are present, Trusted Publishers mode takes precedence.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
4. src/types.ts:29
  • Draft comment:
    The addition of NPM_ID_TOKEN in the Env type is clear and aligns with the updated authentication methods.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_9lLxGRUIOmOg3VuU

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

meesvandongen and others added 2 commits July 30, 2026 02:48
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/main.ts (1)

72-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression coverage for the authentication branch matrix.

Please cover existing .npmrc, NPM_ID_TOKEN precedence, NPM_TOKEN creation, missing-token failure, and both tokens being present.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main.ts` around lines 72 - 85, Add regression tests for the
authentication branching in the main publishing flow: cover an existing .npmrc,
NPM_ID_TOKEN taking precedence, NPM_TOKEN creating .npmrc, failure when neither
token exists, and the behavior when both tokens are present. Assert the relevant
filesystem writes, logs, and setFailed outcome for each branch.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/main.ts`:
- Around line 72-85: Add regression tests for the authentication branching in
the main publishing flow: cover an existing .npmrc, NPM_ID_TOKEN taking
precedence, NPM_TOKEN creating .npmrc, failure when neither token exists, and
the behavior when both tokens are present. Assert the relevant filesystem
writes, logs, and setFailed outcome for each branch.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 83f80878-afa9-43ef-8f5c-1f74b949a7a6

📥 Commits

Reviewing files that changed from the base of the PR and between 1e3d823 and dfb9b67.

📒 Files selected for processing (4)
  • .changeset/yummy-hairs-enjoy.md
  • README.md
  • src/main.ts
  • src/types.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • .changeset/yummy-hairs-enjoy.md
  • src/types.ts
  • README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant