1
1
const execa = require ( `execa` )
2
+ const { Octokit } = require ( `@octokit/rest` )
2
3
const { getAllPackageNames, updateChangelog } = require ( `./generate` )
3
4
4
5
if ( ! process . env . GITHUB_ACCESS_TOKEN ) {
5
6
throw new Error ( `GITHUB_ACCESS_TOKEN env var not set` )
6
7
}
7
8
8
9
async function run ( ) {
9
- await execa ( `git` , [ `checkout` , `master` ] )
10
- await execa ( `git` , [ `pull` , `--tags` ] )
10
+ // TODO: save current branch/commit/hash (and restore on complete)
11
+ const base = `vladar/generate-changelogs`
12
+ const branch = `changelog-update-${ Date . now ( ) } `
13
+
14
+ const args = [ `checkout` , `-b` , branch , `origin/${ base } ` , `--no-track` ]
15
+ await execa ( `git` , args )
11
16
12
17
const updatedPackages = [ ]
13
18
for ( const pkg of getAllPackageNames ( ) ) {
@@ -26,24 +31,31 @@ async function run() {
26
31
return
27
32
}
28
33
29
- // Commit to the same branch
30
- const branchName = `bot-changelog-update`
31
34
const commitMessage = `DO NOT MERGE: testing`
32
- try {
33
- await execa ( `git` , [ `checkout` , `-b` , branchName , `origin/${ branchName } ` ] )
34
- } catch {
35
- await execa ( `git` , [ `checkout` , branchName ] )
36
- }
35
+ const updatedChangelogs = updatedPackages . map (
36
+ pkg => `packages/${ pkg } /CHANGELOG.md`
37
+ )
38
+ await execa ( `git` , [ `add` , ...updatedChangelogs ] )
37
39
await execa ( `git` , [ `commit` , `-m` , commitMessage ] )
40
+ await execa ( `git` , [ `push` , `-u` , `origin` , branch ] )
41
+
42
+ const octokit = new Octokit ( {
43
+ auth : `token ${ process . env . GITHUB_ACCESS_TOKEN } ` ,
44
+ } )
38
45
39
46
try {
47
+ const owner = `gatsbyjs`
48
+ const repo = `gatsby`
49
+
50
+ // Note: PR may already exist for this branch.
51
+ // Then it will throw but we don't care too much
40
52
const pr = await octokit . pulls . create ( {
41
- owner : `gatsby` ,
42
- repo : `gatsbyjs` ,
53
+ owner,
54
+ repo,
43
55
title : commitMessage ,
44
- head : branchName ,
45
- base : `master` ,
46
- body : `Update changelogs of the following packages:\n\n${ updatedPackages
56
+ head : branch ,
57
+ base,
58
+ body : `Updated changelogs of the following packages:\n\n${ updatedPackages
47
59
. map ( p => `- ${ p } ` )
48
60
. join ( `\n` ) } `,
49
61
} )
@@ -54,9 +66,11 @@ async function run() {
54
66
owner,
55
67
repo,
56
68
issue_number : pr . data . number ,
57
- labels : [ `bot: merge on green ` ] ,
69
+ labels : [ `type: maintenance ` ] ,
58
70
} )
59
71
} catch ( e ) {
60
- console . log ( e )
72
+ console . error ( e )
61
73
}
62
74
}
75
+
76
+ run ( )
0 commit comments