Skip to content

Commit 9a335c9

Browse files
Jochen Ulrichj-ulrich
authored andcommitted
Refactors check script
1 parent b1bec00 commit 9a335c9

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

.github/checkForHttpStatusCodesUpdate.js

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const fetch = require( 'node-fetch' );
22
const diff = require( 'diff' );
33
const fs = require( 'fs' ).promises;
44
const path = require( 'path' );
5+
const githubIssues = require( './githubIssues.js' );
56

67
let log = console;
78

89
const httpStatusCodesUrl = 'https://www.iana.org/assignments/http-status-codes/http-status-codes.txt';
10+
const issueTitleBase = 'IANA HTTP Status Code Update';
911

1012
const 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-
9159
const 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+
10187
const main = async () => {
10288
try
10389
{

.github/dummy.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

.github/githubIssues.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
const searchForExistingGithubIssue = async ( { keywords, github, context } ) => {
3+
const query = keywords.concat( [ 'in:title', `repo:${context.payload.repository.full_name}`, 'type:issue' ] ).join( '+' );
4+
const searchResponse = await github.search.issuesAndPullRequests( {
5+
q: query,
6+
} );
7+
return searchResponse.data;
8+
};
9+
10+
const createNewGithubIssue = async ( { title, body, github, context } ) => {
11+
12+
const newIssue = {
13+
owner: context.repo.owner,
14+
repo: context.repo.repo,
15+
title,
16+
body
17+
};
18+
19+
const issueResponse = await github.issues.create( newIssue );
20+
return issueResponse.data;
21+
};
22+
23+
module.exports = { searchForExistingGithubIssue, createNewGithubIssue };

0 commit comments

Comments
 (0)