-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
161 lines (139 loc) · 4.07 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
import { cp, mkdir } from 'node:fs/promises';
import { resolve } from 'node:path';
import { cwd, exit } from 'node:process';
import {
error,
getBooleanInput,
getInput,
getMultilineInput,
group,
setFailed,
} from '@actions/core';
import { exec } from '@actions/exec';
import { context } from '@actions/github';
import { Octokit } from '@octokit/rest';
import fetch from 'node-fetch';
const getInputRequired = (name: string) =>
getInput(name, {
required: true,
});
(async () => {
const version = getInputRequired('version');
const preRelease = getBooleanInput('pre-release');
const disableSourceTag = getBooleanInput('disable-source-tag');
const releaseBranch = getInputRequired('release-branch');
const buildCommand = getMultilineInput('build-command', { required: true });
const directory = getInputRequired('directory');
const token = getInputRequired('token');
const versionInfo = /^v(?<major>\d+).(?<minor>\d+).(?<patch>\d+).*$/.exec(
version,
)?.groups as {
major: string;
minor: string;
patch: string;
} | null;
if (versionInfo === null) {
setFailed('Unsupported version format. Use `vx.y.z` style.');
exit(1);
}
const octokit = new Octokit({
baseUrl: context.apiUrl,
auth: token,
request: fetch,
});
const repository = (await octokit.repos.get({ ...context.repo })).data;
const runInDist = { cwd: resolve(cwd(), directory) };
await group('Initialising Git', async () => {
await exec('git', [
'config',
'--global',
'user.name',
'github-actions[bot]',
]);
await exec('git', [
'config',
'--global',
'user.email',
'github-actions[bot]@users.noreply.github.com',
]);
const gitUrl = new URL(repository.clone_url);
gitUrl.username = 'oauth2';
gitUrl.password = token;
await exec('git', [
'config',
'--global',
`url.${gitUrl.toString()}.insteadOf`,
repository.clone_url,
]);
});
await group('Creating source tag', async () => {
if (disableSourceTag) {
console.log('Disabled, skipping.');
} else {
await exec('git', ['tag', `${version}-src`]);
await exec('git', ['push', '--tags']);
}
});
await group('Initialising release branch', async () => {
await mkdir(directory, {
recursive: true,
});
await exec('git', ['init', '-b', releaseBranch], runInDist);
await exec(
'git',
['remote', 'add', 'origin', repository.clone_url],
runInDist,
);
await exec(
'git',
['pull', '--set-upstream', '--rebase', 'origin', releaseBranch],
{ ...runInDist, ignoreReturnCode: true },
);
});
await group('Running build commands', async () => {
for (const command of buildCommand) {
await exec('sh', ['-c', command]);
}
await cp('action.yml', resolve(directory, 'action.yml'));
});
await group('Committing changes', async () => {
await exec('git', ['add', '.'], runInDist);
await exec('git', ['commit', '-m', `feat: Release ${version}`], runInDist);
await exec('git', ['tag', '-f', `v${versionInfo.major}`], runInDist);
await exec('git', ['push', '-u', 'origin', releaseBranch], runInDist);
await exec('git', ['push', '--tags', '-f'], runInDist);
});
await group('Creating a release', async () => {
let latestRelease = null;
try {
latestRelease = await octokit.repos.getLatestRelease({
...context.repo,
});
} catch {} // do nothing
let body = null;
if (!disableSourceTag) {
const releaseNotes = await octokit.repos.generateReleaseNotes({
...context.repo,
tag_name: `${version}-src`,
target_commitish: context.sha,
previous_tag_name: latestRelease
? `${latestRelease.data.tag_name}-src`
: undefined,
});
body = releaseNotes.data.body;
}
await octokit.repos.createRelease({
...context.repo,
name: version,
tag_name: version,
target_commitish: releaseBranch,
prerelease: preRelease,
body,
});
});
})()
.then()
.catch((e) => {
error(e);
exit(1);
});