Skip to content

Commit d977e24

Browse files
committed
chore: Add scripts to update code lists
1 parent 7fd648b commit d977e24

8 files changed

+148
-77
lines changed
File renamed without changes.

.github/checkForHttpStatusCodesUpdate.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const fetch = require( 'node-fetch' );
21
const diff = require( 'diff' );
32
const fs = require( 'fs' ).promises;
4-
const path = require( 'path' );
5-
const githubIssues = require( './githubIssues.js' );
3+
const githubIssues = require( './githubIssues' );
4+
const {
5+
fetchHttpStatusCodesList,
6+
httpStatusCodesFileName,
7+
httpStatusCodesFilePath
8+
} = require( './httpStatusCodes' );
69

710
let log = console;
811

9-
const httpStatusCodesUrl = 'https://www.iana.org/assignments/http-status-codes/http-status-codes.txt';
1012
const issueTitleBase = 'IANA HTTP Status Code Update';
1113

1214
const checkForUpdate = async ( { github, core, context, dryRun } ) => {
@@ -41,28 +43,12 @@ const checkForUpdate = async ( { github, core, context, dryRun } ) => {
4143
}
4244
};
4345

44-
const fetchHttpStatusCodesList = async () => {
45-
const response = await fetch( httpStatusCodesUrl );
46-
if ( !response.ok ) {
47-
throw Error( `Error fetching HTTP status codes list: ${response.status} ${response.statusText}` );
48-
}
49-
const httpStatusCodesList = await response.text();
50-
51-
const match = /Last Updated\s+(\d{4}-\d{2}-\d{2})/.exec( httpStatusCodesList );
52-
if ( !match ) {
53-
throw Error( 'Could not find "Last Updated" date in HTTP status list' );
54-
}
55-
const lastUpdated = match[ 1 ];
56-
return { lastUpdated, httpStatusCodesList };
57-
};
58-
5946
const getDiffWithLastUsedVersion = async ( httpStatusCodeList ) => {
60-
const pathToLastUsedVersion = path.resolve( './.github/http-status-codes.txt' );
61-
const lastUsedVersion = await fs.readFile( pathToLastUsedVersion, { encoding: 'utf-8' } );
47+
const lastUsedVersion = await fs.readFile( httpStatusCodesFilePath, { encoding: 'utf-8' } );
6248
if ( lastUsedVersion === httpStatusCodeList ) {
6349
return null;
6450
}
65-
const patch = diff.createPatch( 'http-status-codes.txt', lastUsedVersion, httpStatusCodeList );
51+
const patch = diff.createPatch( httpStatusCodesFileName, lastUsedVersion, httpStatusCodeList );
6652
return patch;
6753
};
6854

@@ -74,7 +60,7 @@ const createNewGithubIssue = async ( { lastUpdatedDate, diffWithLastUsedVersion,
7460
'```diff' + '\n' +
7561
diffWithLastUsedVersion + '\n' +
7662
'```';
77-
63+
7864
if ( dryRun ) {
7965
log.info( `Would create issue:\n${ JSON.stringify( { title, body }, null, 4 ) }` );
8066
}
@@ -104,4 +90,4 @@ module.exports = checkForUpdate;
10490

10591
if ( require.main === module ) {
10692
main();
107-
}
93+
}
Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
const fetch = require( 'node-fetch' );
21
const diff = require( 'diff' );
32
const fs = require( 'fs' ).promises;
4-
const path = require( 'path' );
5-
const githubIssues = require( './githubIssues.js' );
3+
const githubIssues = require( './githubIssues' );
4+
const {
5+
fetchQNetworkReplyErrorCodeListFromGitHub,
6+
fetchQNetworkReplyErrorCodeListFromQt,
7+
qNetworkReplyErrorCodesFilePath,
8+
qNetworkReplyErrorCodesFileName
9+
} = require('./qNetworkReplyErrorCodes');
610

711
let log = console;
812

9-
const qNetworkReplyHeaderUrl = 'https://code.qt.io/cgit/qt/qtbase.git/plain/src/network/access/qnetworkreply.h';
1013
const issueTitleBase = 'QNetworkReply Error Code Update';
1114

1215
const checkForUpdate = async ( { github, core, context, dryRun } ) => {
@@ -45,57 +48,14 @@ const shortenId = id => {
4548
return id.substring( 0, 8 )
4649
};
4750

48-
const fetchQNetworkReplyErrorCodeListFromGitHub = async ( github ) => {
49-
const getContent = github.repos.getContent || github.repos.getContents;
50-
const response = await getContent( {
51-
owner: 'qt',
52-
repo: 'qtbase',
53-
path: 'src/network/access/qnetworkreply.h',
54-
ref: 'dev'
55-
} );
56-
const blobId = response.data.sha;
57-
58-
const qNetworkReplyHeaderSource = decodeRepoContent( response.data );
59-
const qNetworkReplyErrorCodes = extractQNetworkReplyErrorCodes( qNetworkReplyHeaderSource );
60-
61-
return { blobId, qNetworkReplyErrorCodes };
62-
};
63-
64-
const fetchQNetworkReplyErrorCodeListFromQt = async () => {
65-
const response = await fetch( qNetworkReplyHeaderUrl );
66-
if ( !response.ok ) {
67-
throw Error( `Error fetching QNetworkReply header: ${response.status} ${response.statusText}` );
68-
}
69-
const qNetworkReplyHeaderSource = await response.text();
70-
71-
return extractQNetworkReplyErrorCodes( qNetworkReplyHeaderSource );
72-
}
73-
74-
const decodeRepoContent = ( response ) => {
75-
try {
76-
return Buffer.from( response.content, response.encoding ).toString( 'utf-8' );
77-
}
78-
catch( e ) {
79-
throw Error( `Unsupported repository content encoding: ${response.encoding}\nOriginal exception: ${e}` );
80-
}
81-
};
82-
83-
const extractQNetworkReplyErrorCodes = ( qnetworkReplyHeaderSource ) => {
84-
const enumMatch = /enum NetworkError {(.*?)};/s.exec( qnetworkReplyHeaderSource );
85-
if ( !enumMatch ) {
86-
throw Error( 'Could not extract NetworkError codes from QNetworkReply header' );
87-
}
88-
const enums = enumMatch[ 1 ].trim().replace( /\/\/.*$|[ \t]+|\n\n+|,/mg, '' );
89-
return enums;
90-
}
9151

9252
const getDiffWithLastUsedVersion = async ( qNetworkReplyErrorCodes ) => {
93-
const pathToLastUsedVersion = path.resolve( './.github/QNetworkReplyErroCodes.txt' );
53+
const pathToLastUsedVersion = qNetworkReplyErrorCodesFilePath;
9454
const lastUsedVersion = await fs.readFile( pathToLastUsedVersion, { encoding: 'utf-8' } );
9555
if ( lastUsedVersion === qNetworkReplyErrorCodes ) {
9656
return null;
9757
}
98-
const patch = diff.createPatch( 'QNetworkReplyErroCodes.txt', lastUsedVersion, qNetworkReplyErrorCodes );
58+
const patch = diff.createPatch( qNetworkReplyErrorCodesFileName, lastUsedVersion, qNetworkReplyErrorCodes );
9959
return patch;
10060
};
10161

@@ -108,7 +68,7 @@ const createNewGithubIssue = async ( { blobId, diffWithLastUsedVersion, github,
10868
'```diff' + '\n' +
10969
diffWithLastUsedVersion + '\n' +
11070
'```';
111-
71+
11272
if ( dryRun ) {
11373
log.info( `Would create issue:\n${ JSON.stringify( { title, body }, null, 4 ) }` );
11474
}
@@ -121,8 +81,8 @@ const createNewGithubIssue = async ( { blobId, diffWithLastUsedVersion, github,
12181
const main = async () => {
12282
try
12383
{
124-
const qnetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeListFromQt();
125-
const patch = await getDiffWithLastUsedVersion( qnetworkReplyErrorCodes );
84+
const qNetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeListFromQt();
85+
const patch = await getDiffWithLastUsedVersion( qNetworkReplyErrorCodes );
12686
if ( patch ) {
12787
log.log( patch );
12888
}
@@ -140,4 +100,4 @@ module.exports = checkForUpdate;
140100

141101
if ( require.main === module ) {
142102
main();
143-
}
103+
}

.github/httpStatusCodes.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
const fetch = require( 'node-fetch' );
3+
const path = require( 'path' );
4+
5+
const httpStatusCodesUrl = 'https://www.iana.org/assignments/http-status-codes/http-status-codes.txt';
6+
const httpStatusCodesFileName = 'http-status-codes.txt';
7+
const httpStatusCodesFilePath = path.join( __dirname, httpStatusCodesFileName );
8+
9+
const fetchHttpStatusCodesList = async () => {
10+
const response = await fetch( httpStatusCodesUrl );
11+
if ( !response.ok ) {
12+
throw Error( `Error fetching HTTP status codes list: ${response.status} ${response.statusText}` );
13+
}
14+
const httpStatusCodesList = await response.text();
15+
16+
const match = /Last Updated\s+(\d{4}-\d{2}-\d{2})/.exec( httpStatusCodesList );
17+
if ( !match ) {
18+
throw Error( 'Could not find "Last Updated" date in HTTP status list' );
19+
}
20+
const lastUpdated = match[ 1 ];
21+
return { lastUpdated, httpStatusCodesList };
22+
};
23+
24+
module.exports = { fetchHttpStatusCodesList, httpStatusCodesFileName, httpStatusCodesFilePath };

.github/qNetworkReplyErrorCodes.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const fetch = require( 'node-fetch' );
2+
const path = require( 'path' );
3+
4+
const qNetworkReplyHeaderUrl = 'https://code.qt.io/cgit/qt/qtbase.git/plain/src/network/access/qnetworkreply.h';
5+
const qNetworkReplyErrorCodesFileName = 'QNetworkReplyErrorCodes.txt';
6+
const qNetworkReplyErrorCodesFilePath = path.join( __dirname, qNetworkReplyErrorCodesFileName );
7+
8+
const fetchQNetworkReplyErrorCodeListFromGitHub = async ( github ) => {
9+
const getContent = github.repos.getContent || github.repos.getContents;
10+
const response = await getContent( {
11+
owner: 'qt',
12+
repo: 'qtbase',
13+
path: 'src/network/access/qnetworkreply.h',
14+
ref: 'dev'
15+
} );
16+
const blobId = response.data.sha;
17+
18+
const qNetworkReplyHeaderSource = decodeRepoContent( response.data );
19+
const qNetworkReplyErrorCodes = extractQNetworkReplyErrorCodes( qNetworkReplyHeaderSource );
20+
21+
return { blobId, qNetworkReplyErrorCodes };
22+
};
23+
24+
const decodeRepoContent = ( response ) => {
25+
try {
26+
return Buffer.from( response.content, response.encoding ).toString( 'utf-8' );
27+
}
28+
catch( e ) {
29+
throw Error( `Unsupported repository content encoding: ${response.encoding}\nOriginal exception: ${e}` );
30+
}
31+
};
32+
33+
const extractQNetworkReplyErrorCodes = ( qnetworkReplyHeaderSource ) => {
34+
const enumMatch = /enum NetworkError {(.*?)};/s.exec( qnetworkReplyHeaderSource );
35+
if ( !enumMatch ) {
36+
throw Error( 'Could not extract NetworkError codes from QNetworkReply header' );
37+
}
38+
const enums = enumMatch[ 1 ].trim().replace( /\/\/.*$|[ \t]+|\n\n+|,/mg, '' );
39+
return enums;
40+
}
41+
42+
const fetchQNetworkReplyErrorCodeListFromQt = async () => {
43+
const response = await fetch( qNetworkReplyHeaderUrl );
44+
if ( !response.ok ) {
45+
throw Error( `Error fetching QNetworkReply header: ${response.status} ${response.statusText}` );
46+
}
47+
const qNetworkReplyHeaderSource = await response.text();
48+
49+
return extractQNetworkReplyErrorCodes( qNetworkReplyHeaderSource );
50+
}
51+
52+
module.exports = {
53+
fetchQNetworkReplyErrorCodeListFromGitHub,
54+
fetchQNetworkReplyErrorCodeListFromQt,
55+
qNetworkReplyErrorCodesFileName,
56+
qNetworkReplyErrorCodesFilePath
57+
}

.github/updateHttpStatusCodes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require( 'fs' );
2+
const { fetchHttpStatusCodesList, httpStatusCodesFilePath } = require('./httpStatusCodes');
3+
4+
let log = console;
5+
6+
const main = async () => {
7+
try
8+
{
9+
log.info( 'Updating HTTP Status Codes in file', httpStatusCodesFilePath, '...' );
10+
const httpStatusCodes = await fetchHttpStatusCodesList();
11+
fs.writeFileSync( httpStatusCodesFilePath, httpStatusCodes.httpStatusCodesList, 'utf-8' );
12+
}
13+
catch( reason ) {
14+
log.error( reason );
15+
process.exitCode = -1;
16+
};
17+
};
18+
19+
main();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require( 'fs' );
2+
const { fetchQNetworkReplyErrorCodeListFromQt, qNetworkReplyErrorCodesFilePath } = require('./qNetworkReplyErrorCodes');
3+
4+
let log = console;
5+
6+
const main = async () => {
7+
try
8+
{
9+
log.info( 'Updating QNetworkReply Error Codes in file', qNetworkReplyErrorCodesFilePath, '...' );
10+
const qNetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeListFromQt();
11+
fs.writeFileSync( qNetworkReplyErrorCodesFilePath, qNetworkReplyErrorCodes, 'utf-8' );
12+
}
13+
catch( reason ) {
14+
log.error( reason );
15+
process.exitCode = -1;
16+
};
17+
};
18+
19+
main();

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
"name": "http-status-codes-cpp",
33
"version": "1.5.0",
44
"description": "HTTP Status Codes and Reason Phrases for C, C++ and Qt",
5+
"scripts": {
6+
"check-http-status-codes-update": "node .github/checkForHttpStatusCodesUpdate.js",
7+
"update-http-status-codes": "node .github/updateHttpStatusCodes.js",
8+
"check-qnetworkreply-error-codes-update": "node .github/checkQNetworkReplyErrorCodeUpdate.js",
9+
"update-qnetworkreply-error-codes": "node .github/updateQNetworkReplyErrorCodes.js"
10+
},
511
"directories": {
612
"test": "tests"
713
},

0 commit comments

Comments
 (0)