11const execa = require ( `execa` )
2+ const { Octokit } = require ( `@octokit/rest` )
23const { getAllPackageNames, updateChangelog } = require ( `./generate` )
34
45if ( ! process . env . GITHUB_ACCESS_TOKEN ) {
56 throw new Error ( `GITHUB_ACCESS_TOKEN env var not set` )
67}
78
89async 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 )
1116
1217 const updatedPackages = [ ]
1318 for ( const pkg of getAllPackageNames ( ) ) {
@@ -26,24 +31,31 @@ async function run() {
2631 return
2732 }
2833
29- // Commit to the same branch
30- const branchName = `bot-changelog-update`
3134 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 ] )
3739 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+ } )
3845
3946 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
4052 const pr = await octokit . pulls . create ( {
41- owner : `gatsby` ,
42- repo : `gatsbyjs` ,
53+ owner,
54+ repo,
4355 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
4759 . map ( p => `- ${ p } ` )
4860 . join ( `\n` ) } `,
4961 } )
@@ -54,9 +66,11 @@ async function run() {
5466 owner,
5567 repo,
5668 issue_number : pr . data . number ,
57- labels : [ `bot: merge on green ` ] ,
69+ labels : [ `type: maintenance ` ] ,
5870 } )
5971 } catch ( e ) {
60- console . log ( e )
72+ console . error ( e )
6173 }
6274}
75+
76+ run ( )
0 commit comments