-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
96 lines (83 loc) · 2.49 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
import { error as logError, getInput, info as logInfo, setFailed } from '@actions/core';
import { context, getOctokit } from "@actions/github";
import { GitHub } from '@actions/github/lib/utils';
import { ReleaseReleasedEvent } from '@octokit/webhooks-types';
import { ShortcutClient } from "@useshortcut/client";
import { default as replaceAsync } from 'string-replace-async';
import { ReleaseNotesGenerator } from "./release-notes-generator";
// Shortcut's types depend on the DOM :(
declare global {
type File = any;
}
class ReleaseNotesAction {
private releaseNotesRegex = /\[rn > ([-._\w]*?)\]/;
constructor(
private githubApi: InstanceType<typeof GitHub>,
private shortcutApi: ShortcutClient<any>,
private repositoryOwner: string,
private repository: string,
private templates: { releasenotes: string, noStories: string },
) {
}
public async run() {
const payload = context.payload as ReleaseReleasedEvent;
if (!payload || !payload.release || typeof payload.release.body !== "string") {
throw new Error("No release body available. Unable to generate release notes");
}
const head = payload.release.tag_name;
const replacedBody = await replaceAsync(payload.release.body, this.releaseNotesRegex,
async (substring, base) => {
try {
const generator = new ReleaseNotesGenerator(
this.githubApi,
this.shortcutApi,
this.repositoryOwner,
this.repository,
head,
base,
this.templates,
);
return await generator.generate();
} catch (e) {
logError(e);
if (e.response && e.response.url) {
logError(e.response?.url);
}
setFailed(e);
return substring;
}
},
);
if(replacedBody === payload.release.body) {
logInfo('Nothing to replace');
return;
}
logInfo('Replaced body:' + replacedBody);
try {
await this.githubApi.rest.repos.updateRelease({
owner: this.repositoryOwner,
repo: this.repository,
release_id: payload.release.id,
body: replacedBody,
});
} catch (e) {
logError(e);
throw new Error('Failed to update release');
}
logInfo("Update release notes to release");
}
}
const action = new ReleaseNotesAction(
getOctokit(getInput('github-token')),
new ShortcutClient(getInput('clubhouse-token')),
getInput("repository-owner"),
getInput("repository-name"),
{
releasenotes: getInput("releasenotes-template"),
noStories: getInput("no-stories-template"),
},
);
action.run().catch((error) => {
logError('Action failed.');
setFailed(error.message);
});