1+ import * as github from '@actions/github' ;
12import * as logfmt from 'logfmt' ;
2- import matchAll from 'string.prototype.matchall'
3+ const matchAll = require ( 'string.prototype.matchall' )
34
45export const findCodeBlocks = ( source : string ) : ( [ string , number ] ) [ ] => {
56 const pat = / ( ^ ` ` ` \r ? \n ) ( .+ ?) ^ ` ` ` / gms;
@@ -44,7 +45,6 @@ export const patchCodeBlocks = (source: string): [patched: string, patchCount: n
4445 logFmtMatches ++
4546 return formatLogItem ( time , level , msg , fields )
4647 } else {
47- console . log ( `!LF: ${ line } ` )
4848 // Did not include the usual log fields, probably not in logfmt, just skip it
4949 return line
5050 }
@@ -59,4 +59,50 @@ export const patchCodeBlocks = (source: string): [patched: string, patchCount: n
5959
6060 // Copy all unmatched lines after the last match
6161 return [ patched + source . substring ( sourcePos ) , logFmtMatches ]
62+ }
63+
64+ export const patchIssue = async ( authToken : string , owner : string , repo : string , number : number ) => {
65+
66+ console . log ( `Retrieving details for issue #${ number } in ${ owner } /${ repo } ...` )
67+
68+ const octokit = github . getOctokit ( authToken ) ;
69+
70+ const response = await octokit . request ( "GET /repos/{owner}/{repo}/issues/{issue_number}" , {
71+ owner,
72+ repo,
73+ issue_number : number ,
74+ } ) ;
75+
76+ if ( response . status != 200 ) {
77+ throw new Error ( `Failed to fetch issue data. Server responded with ${ response . status } ` )
78+ }
79+
80+ const issue = response . data ;
81+
82+ console . log ( `Issue title: ${ issue . title } ` )
83+ console . log ( `Patching issue body...` )
84+
85+ const [ patchedBody , patchCount ] = patchCodeBlocks ( issue . body ) ;
86+
87+ if ( patchCount < 1 ) {
88+ console . log ( 'No lines where patched. Skipping update.' )
89+ // No need to update the issue body, since we found no logfmt lines
90+ return
91+ }
92+
93+ console . log ( `Patch count: ${ patchCount } ` )
94+ console . log ( `Saving issue body...` )
95+
96+ const saveResponse = await octokit . request ( 'PATCH /repos/{owner}/{repo}/issues/{issue_number}' , {
97+ owner,
98+ repo,
99+ issue_number : number ,
100+ body : patchedBody ,
101+ } )
102+
103+ if ( saveResponse . status != 200 ) {
104+ throw new Error ( `Failed to save issue data. Server responded with ${ response . status } ` )
105+ }
106+
107+ console . log ( 'Done!' )
62108}
0 commit comments