/
create-draft.ts
214 lines (205 loc) · 6.15 KB
/
create-draft.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
import { graphql } from "@octokit/graphql";
import { Octokit } from "@octokit/rest";
import { generateMetaComment } from "./meta-parser";
import { template } from "./template";
export type Issue = {
id: string;
title: string;
body: string;
url: string;
number: number;
}
export const fetchIssues = async (
options: { owner: string; repo: string; labels: string[]; GITHUB_TOKEN: string; },
): Promise<Issue[]> => {
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${options.GITHUB_TOKEN}`
}
});
const QUERY = `query ($owner: String!, $repo: String!, $labels: [String]) {
repository(owner: $owner, name: $repo) {
issues(first: 20, filterBy: {labels: $labels}) {
nodes {
id
title
body
url
number
}
}
}
}
`;
const { repository } = await graphqlWithAuth<{
repository: {
issues: {
nodes: Issue[]
}
};
}>(QUERY, { owner: options.owner, repo: options.repo, labels: options.labels });
return repository.issues.nodes;
};
export const createNextTagVersion = async (
options: { owner: string; repo: string; GITHUB_TOKEN: string; },
): Promise<{ tagName: string; type: "new" | "update", releaseId?: number; }> => {
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${options.GITHUB_TOKEN}`
}
});
const QUERY = `query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
releases(last: 5, orderBy: { field: CREATED_AT, direction: DESC }) {
nodes {
databaseId,
tagName
isDraft
}
totalCount
}
latestRelease {
isDraft
tagName
}
}
}
`;
const { repository } = await graphqlWithAuth<{
repository: {
releases: {
nodes: {
databaseId: number;
tagName: string;
isDraft: boolean;
}[]
totalCount: number;
},
latestRelease: {
databaseId: number,
isDraft: boolean,
tagName: string;
} | null
};
}>(QUERY, { owner: options.owner, repo: options.repo });
const initialVersion = 1;
const currentVersion = repository.latestRelease ? parseInt(repository.latestRelease.tagName, 10) : initialVersion;
console.log("repository releases", JSON.stringify(repository, null, 4));
// first release, but has draft
const latestDraft = (() => {
const release = repository.releases.nodes[0];
if (!release) {
return;
}
if (release.isDraft) {
return release
}
return;
})();
if (latestDraft) {
return {
type: "update",
tagName: latestDraft.tagName,
releaseId: latestDraft.databaseId
}
}
// first release
if (repository.latestRelease === null) {
return {
tagName: `${currentVersion}`,
type: "new"
}
}
// 1, 2 ...
if (currentVersion === repository.releases.totalCount) {
return {
tagName: `${currentVersion + 1}`,
type: "new"
}
}
throw new Error("Unknown response:" + JSON.stringify(repository, null, 4));
};
async function createDraftRelease({
type,
releaseId,
owner,
repo,
releaseName,
tagName,
releaseBody,
commitSha,
GITHUB_TOKEN
}:
{ type: "new" | "update", releaseId?: number; owner: string; repo: string; releaseName: string; releaseBody: string; tagName: string, commitSha: string; GITHUB_TOKEN: string }) {
const octokit = new Octokit({
auth: GITHUB_TOKEN
});
// Create a release
// API Documentation: https://developer.github.com/v3/repos/releases/#create-a-release
// Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-create-release
if (type === "new") {
await octokit.repos.createRelease({
owner,
repo,
tag_name: tagName,
name: releaseName,
body: releaseBody,
draft: true,
prerelease: false,
target_commitish: commitSha
});
} else if (type === "update" && releaseId) {
await octokit.repos.updateRelease({
release_id: releaseId,
owner,
repo,
tag_name: tagName,
name: releaseName,
body: releaseBody,
draft: true,
prerelease: false,
target_commitish: commitSha
});
}
}
async function main() {
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY!
const DRAFT_LABEL = process.env.DRAFT_LABEL!
const GITHUB_TOKEN = process.env.GITHUB_TOKEN!;
const GIT_COMMIT_SHA = process.env.GIT_COMMIT_SHA!;
const [owner, repo] = GITHUB_REPOSITORY.split("/");
console.log([owner, repo]);
const issues = await fetchIssues({
GITHUB_TOKEN,
labels: [DRAFT_LABEL],
owner,
repo
});
console.log("issues", issues);
const next = await createNextTagVersion({
GITHUB_TOKEN,
owner,
repo,
});
console.log("next version", next);
const body = template(issues);
const metaComment = generateMetaComment(issues);
const title = "Draft";
await createDraftRelease({
owner,
repo,
GITHUB_TOKEN,
commitSha: GIT_COMMIT_SHA,
type: next.type,
tagName: next.tagName,
releaseId: next.releaseId,
releaseName: title,
releaseBody: body + "\n\n" + metaComment
})
}
if (require.main) {
main().catch(error => {
console.error(error);
process.exit(1);
})
}