Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This action for [Changesets](https://github.com/changesets/changesets) creates a pull request with all of the package versions updated and changelogs updated and when there are new changesets on [your configured `baseBranch`](https://github.com/changesets/changesets/blob/main/docs/config-file-options.md#basebranch-git-branch-name), the PR will be updated. When you're ready, you can merge the pull request and you can either publish the packages to npm manually or setup the action to do it for you.

There are also sub-actions hosted in this repository. Check out their respective READMEs for more details:

- [pr-status](./pr-status/README.md): Generate changeset status in PRs.

## Usage

### Inputs
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@
"@actions/core": "^3.0.1",
"@actions/exec": "^3.0.0",
"@actions/github": "^9.1.1",
"@changesets/get-release-plan": "^4.0.16",
"@changesets/ghcommit": "^2.0.1",
"@changesets/git": "^3.0.4",
"@changesets/pre": "^2.0.2",
"@changesets/read": "^0.6.5",
"@manypkg/get-packages": "^1.1.3",
"@octokit/core": "^7.0.6",
"@octokit/plugin-throttling": "^11.0.3",
"@types/mdast": "^3.0.0",
"human-id": "^4.1.3",
"markdown-table": "^3.0.4",
"mdast-util-to-string": "^1.0.6",
"remark-parse": "^7.0.1",
"remark-stringify": "^7.0.3",
"semver": "^7.5.3",
"tinyexec": "^1.1.2",
"unified": "^8.3.2"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ dedupePeers: true
dedupePeerDependents: true
minimumReleaseAge: 10080
minimumReleaseAgeExclude:
- '@oxc-project/*'
- '@rolldown/*'
- "@oxc-project/*"
- "@rolldown/*"
- rolldown@1.0.1
- vite@8.0.13
- vitest@4.1.6

overrides:
lightningcss: '-' # we do not bundle any css
postcss: '-' # we do not bundle any css
lightningcss: "-" # we do not bundle any css
postcss: "-" # we do not bundle any css

shellEmulator: true
trustPolicy: no-downgrade
61 changes: 61 additions & 0 deletions pr-status/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# changesets/action/pr-status

This action generates the changeset status in PRs, e.g. whether it has changeset files and which packages will be released if the PR is merged.

It requires the repo to be checked out, and automatically fetches the PR head ref into a temporary detached worktree in order to infer the changed files and packages. It also requires the [`pull_request_target`](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target) event to be triggered in order to have permissions to comment on the PR and to work in PRs from forks.

You can also use the [`pull_request`](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request) event if you prefer to lock permissions down and not run for PRs from forks. Make sure to add an if check to prevent the action from failing in fork PRs:

```yaml
jobs:
pr-status:
if: github.event.pull_request.head.repo.full_name == github.repository
# ...
```

See the [action metadata](action.yml) for details on the inputs and outputs.

> [!WARNING]
> **Do not run untrusted code** when using the `pull_request_target` event. The example below only checks out code and does not run any code from the PR. Read more about the `pull_request_target` event in the [GitHub documentation](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target).

## Example setup

```yaml
# .github/workflows/comment-changeset-pr-status.yml
name: Comment changeset status in PRs

on:
pull_request_target:
types: [opened, synchronize, reopened]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
pr-status:
runs-on: ubuntu-slim
permissions:
contents: read # to check out files in the repo
outputs:
commentBody: ${{ steps.pr-status.outputs.commentBody }}
steps:
- name: Check out repo
uses: actions/checkout@v6

- name: Generate status
id: pr-status
uses: changesets/action/pr-status@v1

comment:
needs: pr-status
runs-on: ubuntu-slim
permissions:
pull-requests: write # to create and update comments on PRs
steps:
- name: Comment on PR
uses: mshick/add-pr-comment@v3
with:
message-id: changeset-pr-status
message: ${{ needs.pr-status.outputs.commentBody }}
```
12 changes: 12 additions & 0 deletions pr-status/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Changesets - PR Status
description: Generate the changeset status in PRs
runs:
using: node24
main: ../dist/pr-status.js
inputs: {}
outputs:
commentBody:
description: The generated comment body to present the changeset status in PRs.
branding:
icon: package
color: blue
15 changes: 9 additions & 6 deletions rolldown.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { defineConfig } from 'rolldown'
import { defineConfig } from "rolldown";

export default defineConfig({
input: 'src/index.ts',
input: {
index: "src/index.ts",
["pr-status"]: "src/pr-status/index.ts",
},
output: {
dir: 'dist',
format: 'esm',
dir: "dist",
format: "esm",
cleanDir: true,
minify: true,
comments: false,
},
platform: 'node',
})
platform: "node",
});
21 changes: 21 additions & 0 deletions src/pr-status/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { getCommentMessage } from "./message.ts";

(async () => {
const context = github.context.payload.pull_request;
if (!context) {
core.error(
"This action should only be run on `pull_request_target` or `pull_request` events",
);
return;
}

core.info("Creating comment message...");
const commentBody = await getCommentMessage(context);
core.setOutput("commentBody", commentBody);
core.info("Done!");
})().catch((err) => {
core.error(err);
core.setFailed(err.message);
});
128 changes: 128 additions & 0 deletions src/pr-status/message.ts
Comment thread
bluwy marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as github from "@actions/github";
import getReleasePlan from "@changesets/get-release-plan";
import type {
ComprehensiveRelease,
ReleasePlan,
VersionType,
} from "@changesets/types";
import { markdownTable } from "markdown-table";
import {
getNewChangesetTemplateContent,
getNewChangesetUrl,
} from "./template.ts";
import { withPullRequestWorktree } from "./worktree.ts";

type PullRequestContext = NonNullable<
typeof github.context.payload.pull_request
>;

export async function getCommentMessage(context: PullRequestContext) {
const { releasePlan, templateContent } = await withPullRequestWorktree(
context,
async ({ cwd, baseRef }) => {
const releasePlan = await getReleasePlan(cwd, baseRef);
const templateContent = await getNewChangesetTemplateContent(
cwd,
baseRef,
context.title,
);

return {
releasePlan,
templateContent,
};
},
);

const newChangesetUrl = getNewChangesetUrl(
context.head.repo.html_url,
context.head.ref,
templateContent,
);

if (releasePlan.changesets.length > 0) {
return getApproveMessage(context.head.sha, newChangesetUrl, releasePlan);
} else {
return getAbsentMessage(context.head.sha, newChangesetUrl, releasePlan);
}
}

function getApproveMessage(
commitSha: string,
newChangesetUrl: string,
releasePlan: ReleasePlan,
) {
return `\
### 🦋 Changeset detected

Latest commit: ${commitSha}

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

${getReleasePlanMessage(releasePlan)}

Not sure what this means? [Click here to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](${newChangesetUrl})`;
}

function getAbsentMessage(
commitSha: string,
newChangesetUrl: string,
releasePlan: ReleasePlan,
) {
return `\
### ⚠️ No Changeset found

Latest commit: ${commitSha}

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

${getReleasePlanMessage(releasePlan)}

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](${newChangesetUrl})`;
}

function getReleasePlanMessage(releasePlan: ReleasePlan) {
const publishableReleases = releasePlan.releases.filter(
(r) => r.type !== "none",
) as (ComprehensiveRelease & { type: Exclude<VersionType, "none"> })[];

const table = markdownTable([
["Name", "Type"],
...publishableReleases.map((release) => {
return [
release.name,
{
major: "Major",
minor: "Minor",
patch: "Patch",
}[release.type],
];
}),
]);

let summary = "This PR includes ";
if (releasePlan.changesets.length === 0) {
summary += "no changesets";
} else {
summary += `changesets to release ${publishableReleases.length} package`;
if (publishableReleases.length !== 1) {
summary += "s";
}
}

return `\
<details>
<summary>${summary}</summary>

${
publishableReleases.length > 0
? table
: "When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types"
}

</details>`;
}
30 changes: 30 additions & 0 deletions src/pr-status/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getChangedPackagesSinceRef } from "@changesets/git";
import { humanId } from "human-id";

export function getNewChangesetUrl(
headRepoUrl: string,
headRef: string,
templateContent: string,
) {
const fileName = humanId({ separator: "-", capitalize: false });
return `${headRepoUrl}/new/${headRef}?filename=.changeset/${fileName}.md&value=${encodeURIComponent(templateContent)}`;
}

export async function getNewChangesetTemplateContent(
cwd: string,
baseRef: string,
prTitle: string,
) {
const changedPackages = await getChangedPackagesSinceRef({
cwd,
ref: baseRef,
});

return `\
---
${changedPackages.map((p) => `"${p.packageJson.name}": patch`).join("\n")}
---

${prTitle}
`;
}
Loading