@@ -3,6 +3,11 @@ import * as github from '@actions/github'
33
44const GITHUB_TOKEN = getEnvVar ( 'GITHUB_TOKEN' )
55
6+ interface Changelog {
7+ draft : string [ ]
8+ releases : { [ tag : string ] : string [ ] }
9+ }
10+
611process . on ( 'unhandledRejection' , ( reason : any , _ : Promise < any > ) =>
712 handleError ( reason )
813)
@@ -14,7 +19,9 @@ async function main() {
1419 github . context . payload . pull_request &&
1520 github . context . payload . pull_request . title
1621 core . info ( 'title: ' + title )
17- await getChangelog ( )
22+ const changelog = await getChangelog ( )
23+ const updatedChangelog = { draft : [ title , ...changelog . draft ] , ...changelog }
24+ await commitChangelog ( updatedChangelog )
1825}
1926
2027async function getChangelog ( ) {
@@ -25,7 +32,30 @@ async function getChangelog() {
2532 const response = await octokit . repos . getContents ( { owner, repo, path } )
2633 const base64Content = ( response . data as any ) . content
2734 const content = Buffer . from ( base64Content , 'base64' ) . toString ( 'utf8' )
28- core . info ( 'content: ' + content )
35+ return JSON . parse ( content )
36+ }
37+
38+ async function commitChangelog ( changelog : Changelog ) {
39+ const octokit = new github . GitHub ( GITHUB_TOKEN )
40+ const owner = github . context . repo . owner
41+ const repo = github . context . repo . repo
42+ const path = 'changelog.json'
43+ const message = 'update changelog'
44+ const json = JSON . stringify ( changelog , null , 2 )
45+ const content = Buffer . from ( json ) . toString ( 'base64' )
46+ const response = await octokit . repos . createOrUpdateFile ( {
47+ owner,
48+ repo,
49+ path,
50+ message,
51+ content,
52+ } )
53+
54+ if ( response . status != 200 ) {
55+ throw new Error (
56+ 'Failed to commit changelog udpates. ' + JSON . stringify ( response . data )
57+ )
58+ }
2959}
3060
3161function handleError ( err : Error ) {
0 commit comments