@@ -2,10 +2,12 @@ const fetch = require( 'node-fetch' );
22const diff = require ( 'diff' ) ;
33const fs = require ( 'fs' ) . promises ;
44const path = require ( 'path' ) ;
5+ const githubIssues = require ( './githubIssues.js' ) ;
56
67let log = console ;
78
89const httpStatusCodesUrl = 'https://www.iana.org/assignments/http-status-codes/http-status-codes.txt' ;
10+ const issueTitleBase = 'IANA HTTP Status Code Update' ;
911
1012const checkForUpdate = async ( { github, core, context, dryRun } ) => {
1113 try
@@ -21,11 +23,10 @@ const checkForUpdate = async ( { github, core, context, dryRun } ) => {
2123 }
2224 log . warning ( 'HTTP status codes list is outdated!' ) ;
2325
24- const existingGithubIssues = await searchForExistingGithubIssue ( { lastUpdatedDate, github, context } ) ;
26+ const existingGithubIssues = await githubIssues . searchForExistingGithubIssue ( { keywords : [ issueTitleBase , lastUpdatedDate ] , github, context } ) ;
2527
2628 if ( existingGithubIssues . total_count === 0 ) {
27- const newIssue = await createNewGithubIssue ( { httpStatusCodes, diffWithLastUsedVersion, github, context, dryRun } ) ;
28- log . info ( `Created issue #${ newIssue . number } : ${ newIssue . html_url } ` ) ;
29+ createNewGithubIssue ( { lastUpdatedDate, diffWithLastUsedVersion, github, context, dryRun } ) ;
2930 }
3031 else if ( existingGithubIssues . total_count === 1 ) {
3132 log . info ( 'An issue already exists for this update.' ) ;
@@ -55,39 +56,6 @@ const fetchHttpStatusCodesList = async () => {
5556 return { lastUpdated, httpStatusCodesList } ;
5657} ;
5758
58- const issueTitleBase = 'IANA HTTP Status Code Update' ;
59-
60- const searchForExistingGithubIssue = async ( { lastUpdatedDate, github, context } ) => {
61- const query = [ issueTitleBase , lastUpdatedDate , 'in:title' , `repo:${ context . payload . repository . full_name } ` , 'type:issue' ] . join ( '+' ) ;
62- const searchResponse = await github . search . issuesAndPullRequests ( {
63- q : query ,
64- } ) ;
65- return searchResponse . data ;
66- } ;
67-
68- const createNewGithubIssue = async ( { httpStatusCodes, diffWithLastUsedVersion, github, context, dryRun } ) => {
69-
70- const newIssue = {
71- owner : context . repo . owner ,
72- repo : context . repo . repo ,
73- title : `${ issueTitleBase } ${ httpStatusCodes . lastUpdated } ` ,
74- body : `The HTTP status codes list has been updated on ${ httpStatusCodes . lastUpdated } . ` + '\n' +
75- 'See https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml' + '\n\n' +
76- '## Diff ##' + '\n' +
77- '```diff' + '\n' +
78- diffWithLastUsedVersion + '\n' +
79- '```'
80- } ;
81-
82- if ( dryRun ) {
83- log . info ( `Would create issue:\n${ JSON . stringify ( newIssue , null , 4 ) } ` ) ;
84- return { number : 0 , html_url : context . payload . repository . issues_url } ;
85- }
86-
87- const issueResponse = await github . issues . create ( newIssue ) ;
88- return issueResponse . data ;
89- } ;
90-
9159const getDiffWithLastUsedVersion = async ( httpStatusCodeList ) => {
9260 const pathToLastUsedVersion = path . resolve ( './.github/http-status-codes.txt' ) ;
9361 const lastUsedVersion = await fs . readFile ( pathToLastUsedVersion , { encoding : 'utf-8' } ) ;
@@ -98,6 +66,24 @@ const getDiffWithLastUsedVersion = async ( httpStatusCodeList ) => {
9866 return patch ;
9967} ;
10068
69+ const createNewGithubIssue = async ( { lastUpdatedDate, diffWithLastUsedVersion, github, context, dryRun } ) => {
70+ const title = `${ issueTitleBase } ${ lastUpdatedDate } ` ;
71+ const body = `The HTTP status codes list has been updated on ${ lastUpdatedDate } . ` + '\n' +
72+ 'See https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml' + '\n\n' +
73+ '## Diff ##' + '\n' +
74+ '```diff' + '\n' +
75+ diffWithLastUsedVersion + '\n' +
76+ '```' ;
77+
78+ if ( dryRun ) {
79+ log . info ( `Would create issue:\n${ JSON . stringify ( { title, body } , null , 4 ) } ` ) ;
80+ }
81+ else {
82+ const newIssue = await githubIssues . createNewGithubIssue ( { title, body, github, context } ) ;
83+ log . info ( `Created issue #${ newIssue . number } : ${ newIssue . html_url } ` ) ;
84+ }
85+ } ;
86+
10187const main = async ( ) => {
10288 try
10389 {
0 commit comments