-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmessage.ts
More file actions
124 lines (99 loc) · 3.32 KB
/
Copy pathmessage.ts
File metadata and controls
124 lines (99 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 { commentMarker } from "./constants.ts";
import {
getNewChangesetTemplateContent,
getNewChangesetUrl,
} from "./template.ts";
type PullRequestContext = NonNullable<
typeof github.context.payload.pull_request
>;
export async function getCommentMessage(context: PullRequestContext) {
const cwd = process.cwd();
const releasePlan = await getReleasePlan(cwd, context.base.ref);
const templateContent = await getNewChangesetTemplateContent(
cwd,
context.base.ref,
context.title,
);
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 `\
${commentMarker}
### 🦋 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 `\
${commentMarker}
### ⚠️ 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>`;
}