|
| 1 | +import * as github from "@actions/github"; |
| 2 | +import getReleasePlan from "@changesets/get-release-plan"; |
| 3 | +import type { |
| 4 | + ComprehensiveRelease, |
| 5 | + ReleasePlan, |
| 6 | + VersionType, |
| 7 | +} from "@changesets/types"; |
| 8 | +import { markdownTable } from "markdown-table"; |
| 9 | +import { |
| 10 | + getNewChangesetTemplateContent, |
| 11 | + getNewChangesetUrl, |
| 12 | +} from "./template.ts"; |
| 13 | +import { getPullRequestWorktree } from "./worktree.ts"; |
| 14 | + |
| 15 | +type PullRequestContext = NonNullable< |
| 16 | + typeof github.context.payload.pull_request |
| 17 | +>; |
| 18 | + |
| 19 | +export async function getCommentMessage(context: PullRequestContext) { |
| 20 | + await using worktree = await getPullRequestWorktree(context); |
| 21 | + |
| 22 | + const releasePlan = await getReleasePlan(worktree.cwd, worktree.baseRef); |
| 23 | + const templateContent = await getNewChangesetTemplateContent( |
| 24 | + worktree.cwd, |
| 25 | + worktree.baseRef, |
| 26 | + context.title, |
| 27 | + ); |
| 28 | + |
| 29 | + const newChangesetUrl = getNewChangesetUrl( |
| 30 | + context.head.repo.html_url, |
| 31 | + context.head.ref, |
| 32 | + templateContent, |
| 33 | + ); |
| 34 | + |
| 35 | + if (releasePlan.changesets.length > 0) { |
| 36 | + return getApproveMessage(context.head.sha, newChangesetUrl, releasePlan); |
| 37 | + } else { |
| 38 | + return getAbsentMessage(context.head.sha, newChangesetUrl, releasePlan); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function getApproveMessage( |
| 43 | + commitSha: string, |
| 44 | + newChangesetUrl: string, |
| 45 | + releasePlan: ReleasePlan, |
| 46 | +) { |
| 47 | + return `\ |
| 48 | +### 🦋 Changeset detected |
| 49 | +
|
| 50 | +Latest commit: ${commitSha} |
| 51 | +
|
| 52 | +**The changes in this PR will be included in the next version bump.** |
| 53 | +
|
| 54 | +${getReleasePlanMessage(releasePlan)} |
| 55 | +
|
| 56 | +Not sure what this means? [Click here to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md). |
| 57 | +
|
| 58 | +[Click here if you're a maintainer who wants to add another changeset to this PR](${newChangesetUrl})`; |
| 59 | +} |
| 60 | + |
| 61 | +function getAbsentMessage( |
| 62 | + commitSha: string, |
| 63 | + newChangesetUrl: string, |
| 64 | + releasePlan: ReleasePlan, |
| 65 | +) { |
| 66 | + return `\ |
| 67 | +### ⚠️ No Changeset found |
| 68 | +
|
| 69 | +Latest commit: ${commitSha} |
| 70 | +
|
| 71 | +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.** |
| 72 | +
|
| 73 | +${getReleasePlanMessage(releasePlan)} |
| 74 | +
|
| 75 | +[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md). |
| 76 | +
|
| 77 | +[Click here if you're a maintainer who wants to add a changeset to this PR](${newChangesetUrl})`; |
| 78 | +} |
| 79 | + |
| 80 | +function getReleasePlanMessage(releasePlan: ReleasePlan) { |
| 81 | + const publishableReleases = releasePlan.releases.filter( |
| 82 | + (r) => r.type !== "none", |
| 83 | + ) as (ComprehensiveRelease & { type: Exclude<VersionType, "none"> })[]; |
| 84 | + |
| 85 | + const table = markdownTable([ |
| 86 | + ["Name", "Type"], |
| 87 | + ...publishableReleases.map((release) => { |
| 88 | + return [ |
| 89 | + release.name, |
| 90 | + { |
| 91 | + major: "Major", |
| 92 | + minor: "Minor", |
| 93 | + patch: "Patch", |
| 94 | + }[release.type], |
| 95 | + ]; |
| 96 | + }), |
| 97 | + ]); |
| 98 | + |
| 99 | + let summary = "This PR includes "; |
| 100 | + if (releasePlan.changesets.length === 0) { |
| 101 | + summary += "no changesets"; |
| 102 | + } else { |
| 103 | + summary += `changesets to release ${publishableReleases.length} package`; |
| 104 | + if (publishableReleases.length !== 1) { |
| 105 | + summary += "s"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return `\ |
| 110 | +<details> |
| 111 | +<summary>${summary}</summary> |
| 112 | +
|
| 113 | +${ |
| 114 | + publishableReleases.length > 0 |
| 115 | + ? table |
| 116 | + : "When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types" |
| 117 | +} |
| 118 | +
|
| 119 | +</details>`; |
| 120 | +} |
0 commit comments