forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
224 lines (207 loc) · 6.8 KB
/
Copy pathgithub.ts
File metadata and controls
224 lines (207 loc) · 6.8 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
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
import Octokit from '@octokit/rest'
import { readLine } from './util'
import { promisify } from 'util'
import * as semver from 'semver'
import { mkdtemp as original_mkdtemp } from 'fs'
import * as os from 'os'
import * as path from 'path'
import execa from 'execa'
const mkdtemp = promisify(original_mkdtemp)
const formatDate = (d: Date): string => `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`
export async function ensureTrackingIssue({
majorVersion,
minorVersion,
assignees,
releaseDateTime,
oneWorkingDayBeforeRelease,
fourWorkingDaysBeforeRelease,
fiveWorkingDaysBeforeRelease,
}: {
majorVersion: string
minorVersion: string
assignees: string[]
releaseDateTime: Date
oneWorkingDayBeforeRelease: Date
fourWorkingDaysBeforeRelease: Date
fiveWorkingDaysBeforeRelease: Date
}): Promise<{ url: string; created: boolean }> {
const octokit = await getAuthenticatedGitHubClient()
const releaseIssueTemplate = await getContent(octokit, {
owner: 'sourcegraph',
repo: 'about',
path: 'handbook/engineering/releases/release_issue_template.md',
})
const releaseIssueBody = releaseIssueTemplate
.replace(/\$MAJOR/g, majorVersion)
.replace(/\$MINOR/g, minorVersion)
.replace(/\$RELEASE_DATE/g, formatDate(releaseDateTime))
.replace(/\$FIVE_WORKING_DAYS_BEFORE_RELEASE/g, formatDate(fiveWorkingDaysBeforeRelease))
.replace(/\$FOUR_WORKING_DAYS_BEFORE_RELEASE/g, formatDate(fourWorkingDaysBeforeRelease))
.replace(/\$ONE_WORKING_DAY_BEFORE_RELEASE/g, formatDate(oneWorkingDayBeforeRelease))
const milestoneTitle = `${majorVersion}.${minorVersion}`
const milestones = await octokit.issues.listMilestonesForRepo({
owner: 'sourcegraph',
repo: 'sourcegraph',
per_page: 100,
direction: 'desc',
})
const milestone = milestones.data.filter(m => m.title === milestoneTitle)
if (milestone.length === 0) {
console.log(
`Milestone ${JSON.stringify(
milestoneTitle
)} is closed or not found—you'll need to manually create it and add this issue to it.`
)
}
return ensureIssue(octokit, {
title: trackingIssueTitle(majorVersion, minorVersion),
owner: 'sourcegraph',
repo: 'sourcegraph',
assignees,
body: releaseIssueBody,
milestone: milestone.length > 0 ? milestone[0].number : undefined,
})
}
export async function ensurePatchReleaseIssue({
version,
assignees,
}: {
version: semver.SemVer
assignees: string[]
}): Promise<{ url: string; created: boolean }> {
const octokit = await getAuthenticatedGitHubClient()
const issueTemplate = await getContent(octokit, {
owner: 'sourcegraph',
repo: 'about',
path: 'handbook/engineering/releases/patch_release_issue_template.md',
})
const issueBody = issueTemplate
.replace(/\$MAJOR/g, version.major.toString())
.replace(/\$MINOR/g, version.minor.toString())
.replace(/\$PATCH/g, version.patch.toString())
return ensureIssue(octokit, {
title: `${version.version} patch release`,
owner: 'sourcegraph',
repo: 'sourcegraph',
assignees,
body: issueBody,
})
}
async function getContent(
octokit: Octokit,
params: {
owner: string
repo: string
path: string
}
): Promise<string> {
const resp = await octokit.repos.getContents(params)
if (Array.isArray(resp.data)) {
throw new Error(`${params.path} is a directory`)
}
return Buffer.from(resp.data.content as string, 'base64').toString()
}
async function ensureIssue(
octokit: Octokit,
{
title,
owner,
repo,
assignees,
body,
milestone,
}: {
title: string
owner: string
repo: string
assignees: string[]
body: string
milestone?: number
}
): Promise<{ url: string; created: boolean }> {
const url = await getIssueByTitle(octokit, title)
if (url) {
return { url, created: false }
}
const createdIssue = await octokit.issues.create({
title,
owner,
repo,
assignees,
body,
milestone,
})
return { url: createdIssue.data.html_url, created: true }
}
export async function listIssues(
octokit: Octokit,
query: string
): Promise<Octokit.SearchIssuesAndPullRequestsResponseItemsItem[]> {
return (await octokit.search.issuesAndPullRequests({ per_page: 100, q: query })).data.items
}
export function trackingIssueTitle(major: string, minor: string): string {
return `${major}.${minor} release tracking issue`
}
export async function getAuthenticatedGitHubClient(): Promise<Octokit> {
const githubPAT = await readLine(
'Enter a GitHub personal access token with "repo" scope (https://github.com/settings/tokens/new): ',
'.secrets/github.txt'
)
return new Octokit({ auth: githubPAT })
}
export async function getIssueByTitle(octokit: Octokit, title: string): Promise<string | null> {
const resp = await octokit.search.issuesAndPullRequests({
per_page: 100,
q: `type:issue repo:sourcegraph/sourcegraph is:open ${JSON.stringify(title)}`,
})
const matchingIssues = resp.data.items.filter(issue => issue.title === title)
if (matchingIssues.length === 0) {
return null
}
if (matchingIssues.length > 1) {
throw new Error(`Multiple issues matched issue title ${JSON.stringify(title)}`)
}
return matchingIssues[0].html_url
}
export interface CreateBranchWithChangesOptions {
owner: string
repo: string
base: string
head: string
commitMessage: string
bashEditCommands: string[]
}
export async function createBranchWithChanges({
owner,
repo,
base: baseRev,
head: headBranch,
commitMessage,
bashEditCommands,
}: CreateBranchWithChangesOptions): Promise<void> {
const tmpdir = await mkdtemp(path.join(os.tmpdir(), `sg-release-${owner}-${repo}-`))
console.log(`Created temp directory ${tmpdir}`)
const bashScript = `set -ex
cd ${tmpdir};
git clone --depth 10 git@github.com:${owner}/${repo} || git clone --depth 10 https://github.com/${owner}/${repo};
cd ./${repo};
git checkout ${baseRev};
${bashEditCommands.join(';\n ')};
git add :/;
git commit -a -m ${JSON.stringify(commitMessage)};
git push origin HEAD:${headBranch};
`
await execa('bash', ['-c', bashScript], { stdio: 'inherit' })
}
export async function createPR(options: {
owner: string
repo: string
head: string
base: string
title: string
body?: string
}): Promise<string> {
const octokit = await getAuthenticatedGitHubClient()
const response = await octokit.pulls.create(options)
return response.data.html_url
}