/
update-and-open-pr.js
76 lines (64 loc) · 1.92 KB
/
update-and-open-pr.js
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
const execa = require(`execa`)
const { Octokit } = require(`@octokit/rest`)
const { getAllPackageNames, updateChangelog } = require(`./generate`)
if (!process.env.GITHUB_ACCESS_TOKEN) {
throw new Error(`GITHUB_ACCESS_TOKEN env var not set`)
}
async function run() {
// TODO: save current branch/commit/hash (and restore on complete)
const base = `vladar/generate-changelogs`
const branch = `changelog-update-${Date.now()}`
const args = [`checkout`, `-b`, branch, `origin/${base}`, `--no-track`]
await execa(`git`, args)
const updatedPackages = []
for (const pkg of getAllPackageNames()) {
try {
const updated = await updateChangelog(pkg)
if (updated) {
updatedPackages.push(pkg)
}
} catch (e) {
console.error(`${pkg}: ${e.stack}`)
}
}
if (!updatedPackages.length) {
console.log(`Nothing to do`)
return
}
const commitMessage = `DO NOT MERGE: testing`
const updatedChangelogs = updatedPackages.map(
pkg => `packages/${pkg}/CHANGELOG.md`
)
await execa(`git`, [`add`, ...updatedChangelogs])
await execa(`git`, [`commit`, `-m`, commitMessage])
await execa(`git`, [`push`, `-u`, `origin`, branch])
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_ACCESS_TOKEN}`,
})
try {
const owner = `gatsbyjs`
const repo = `gatsby`
// Note: PR may already exist for this branch.
// Then it will throw but we don't care too much
const pr = await octokit.pulls.create({
owner,
repo,
title: commitMessage,
head: branch,
base,
body: `Updated changelogs of the following packages:\n\n${updatedPackages
.map(p => `- ${p}`)
.join(`\n`)}`,
})
console.log(`\n---\n\nPR opened - ${pr.data.html_url}`)
await octokit.issues.addLabels({
owner,
repo,
issue_number: pr.data.number,
labels: [`type: maintenance`],
})
} catch (e) {
console.error(e)
}
}
run()