-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
276 lines (241 loc) · 8.14 KB
/
index.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import * as core from '@actions/core';
import * as github from '@actions/github';
import {exec, getExecOutput} from '@actions/exec';
import {existsSync} from 'node:fs';
import path from 'node:path';
import {getPackages} from '@manypkg/get-packages';
const silentOption = {silent: true};
try {
if (!process.env.GITHUB_TOKEN) {
throw new Error(
'Please provide the GITHUB_TOKEN to the snapit GitHub action',
);
}
const {payload} = github.context;
if (
!payload.comment ||
!payload.repository ||
!payload.repository.owner.login ||
!payload.issue ||
!payload.issue.title
) {
throw new Error('No comment, repository, or issue found in the payload');
}
const ownerRepo = {
owner: payload.repository.owner.login,
repo: payload.repository.name,
};
const buildScript = core.getInput('build_script');
const isGlobal = core.getInput('global_install') === 'true';
const githubCommentIncludedPackages = core.getInput(
'github_comment_included_packages',
);
const branch = core.getInput('branch');
const workingDirectory = core.getInput('working_directory');
const customMessagePrefix = core.getInput('custom_message_prefix');
const customMessageSuffix = core.getInput('custom_message_suffix');
const commentCommands = core.getInput('comment_command');
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
if (workingDirectory) {
process.chdir(workingDirectory);
}
const isYarn = existsSync('yarn.lock');
const isPnpm = existsSync('pnpm-lock.yaml');
const changesetBinary = path.join('node_modules/.bin/changeset');
const versionPrefix = 'snapshot';
if (commentCommands.split(',').indexOf(payload.comment.body) !== -1) {
await octokit.rest.reactions.createForIssueComment({
...ownerRepo,
comment_id: payload.comment.id,
content: 'eyes',
});
// Check if user has permissions to run /snapit
const actorPermission = (
await octokit.rest.repos.getCollaboratorPermissionLevel({
...ownerRepo,
username: payload.comment.user.login,
})
).data.permission;
if (!['write', 'admin'].includes(actorPermission)) {
const errorMessage =
'Only users with write permission to the repository can run /snapit';
await octokit.rest.issues.createComment({
...ownerRepo,
issue_number: payload.issue.number,
body: errorMessage,
});
throw new Error(errorMessage);
}
// Check if pull request is from a forked repo
const pullRequest = await octokit.rest.pulls.get({
...ownerRepo,
pull_number: payload.issue.number,
});
if (payload.repository.full_name !== pullRequest.data.head.repo.full_name) {
throw new Error(
'`/snapit` is not supported on pull requests from forked repositories.',
);
}
await exec(
'gh',
['pr', 'checkout', payload.issue.number.toString()],
silentOption,
);
const {stdout: currentBranch} = await getExecOutput(
'git',
['branch', '--show-current'],
silentOption,
);
// Because changeset entries are consumed and removed on the
// 'changeset-release/main' branch, we need to reset the files
// so the following 'changeset version --snapshot' command will
// regenerate the package version bumps with the snapshot releases
if (currentBranch.trim() === 'changeset-release/main') {
await exec(
'git',
['checkout', 'origin/main', '--', '.changeset'],
silentOption,
);
}
// Running install to get the changesets package from the project
if (isYarn) {
await exec('yarn', ['install', '--frozen-lockfile']);
} else if (isPnpm) {
await exec('pnpm', ['install', '--frozen-lockfile']);
} else {
await exec('npm', ['ci']);
}
await exec(changesetBinary, ['version', '--snapshot', versionPrefix]);
const {packages} = await getPackages(process.cwd());
interface Snapshot {
package: string;
version: string;
timestamp: string;
fullString: string;
}
const snapshots: Snapshot[] = [];
packages.forEach(({packageJson}) => {
const {name, version, private: isPrivate} = packageJson;
if (name && version && !isPrivate && version.includes(versionPrefix)) {
const timestamp = version.split('-').at(-1);
snapshots.push({
package: name,
version,
timestamp,
fullString: `${name}@${version}`,
});
}
});
if (!snapshots.length) {
throw new Error('Changeset publish did not create new tags.');
}
const snapshotTimestamp = snapshots[0].timestamp;
// Run after `changeset version` so build scripts can use updated versions
if (buildScript) {
const commands = buildScript.split('&&').map((cmd) => cmd.trim());
for (const cmd of commands) {
const [cmdName, ...cmdArgs] = cmd.split(/\s+/);
try {
await exec(cmdName, cmdArgs);
} catch (error) {
throw new Error(
`Failed to run ${cmdName} ${cmdArgs.join(' ')}: ${error.message}`,
);
}
}
}
if (branch) {
// We all think this is weird
// Context: https://github.com/orgs/community/discussions/26560
await exec('git', [
'config',
'--global',
'user.email',
'41898282+github-actions[bot]@users.noreply.github.com',
]);
await exec('git', [
'config',
'--global',
'user.name',
'github-actions[bot]',
]);
await exec('git', ['add', '.']);
await exec('git', [
'commit',
'-m',
`${payload.issue.title} ${versionPrefix}-${snapshotTimestamp}`,
]);
await exec('git', ['checkout', '-b', branch]);
await exec('git', ['push', '--force', 'origin', branch]);
} else {
if (!process.env.NPM_TOKEN) {
throw new Error(
'Please provide the NPM_TOKEN to the snapit GitHub action',
);
}
await exec(
'bash',
[
'-c',
`echo "//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}" > "$HOME/.npmrc"`,
],
silentOption,
);
await exec(changesetBinary, [
'publish',
'--no-git-tags',
'--snapshot',
'--tag',
versionPrefix,
]);
}
const filteredSnapshots = githubCommentIncludedPackages
? snapshots.filter((snapshot: Snapshot) =>
githubCommentIncludedPackages
.split(',')
.some((filter) => snapshot.package === filter),
)
: snapshots;
const multiple = filteredSnapshots.length > 1;
const introMessage = branch
? `Your snapshot${multiple ? 's are' : ' is'} being published.**\n\n`
: `Your snapshot${multiple ? 's have' : ' has'} been published to npm.**\n\n`;
const globalInstallMessage = isYarn
? 'yarn global add'
: isPnpm
? 'pnpm i -g'
: 'npm i -g';
const globalPackagesMessage =
'```bash\n' +
filteredSnapshots
.map((pkg) => `${globalInstallMessage} ${pkg.fullString}`)
.join('\n') +
'\n```';
const localDependenciesMessage =
'```json\n' +
filteredSnapshots
.map((tag) => `"${tag.package}": "${tag.version}"`)
.join(',\n') +
'\n```';
const defaultMessage = isGlobal
? `Test the snapshot by installing your package globally:`
: `Test the snapshot${multiple ? 's' : ''} by updating your \`package.json\` with the newly published version${multiple ? 's' : ''}:`;
const body =
`🫰✨ **Thanks @${payload.comment.user.login}! ${introMessage}` +
`${customMessagePrefix ? customMessagePrefix + ' ' : ''}${defaultMessage}\n` +
`${isGlobal ? `${globalPackagesMessage}` : `${localDependenciesMessage}`}` +
`${customMessageSuffix ? `\n\n${customMessageSuffix}` : ''}`;
await octokit.rest.issues.createComment({
...ownerRepo,
issue_number: payload.issue.number,
body,
});
await octokit.rest.reactions.createForIssueComment({
...ownerRepo,
comment_id: payload.comment.id,
content: 'rocket',
});
}
} catch (error) {
core.setFailed(error.message);
}