1+ const diff = require ( 'diff' ) ;
2+ const fs = require ( 'fs' ) . promises ;
3+ const path = require ( 'path' ) ;
4+ const githubIssues = require ( './githubIssues.js' ) ;
5+
6+ let log = console ;
7+
8+ const qnetworkReplyHeaderUrl = 'https://code.qt.io/cgit/qt/qtbase.git/plain/src/network/access/qnetworkreply.h' ;
9+ const issueTitleBase = 'QNetworkReply Error Code Update' ;
10+
11+ const checkForUpdate = async ( { github, core, context, dryRun } ) => {
12+ try
13+ {
14+ log = core ;
15+
16+ const qnetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeList ( github ) ;
17+ const lastUpdatedDate = httpStatusCodes . lastUpdated ;
18+ const diffWithLastUsedVersion = await getDiffWithLastUsedVersion ( httpStatusCodes . httpStatusCodesList ) ;
19+ if ( ! diffWithLastUsedVersion ) {
20+ log . info ( 'HTTP status codes list is still up to date' ) ;
21+ return ;
22+ }
23+ log . warning ( 'HTTP status codes list is outdated!' ) ;
24+
25+ const existingGithubIssues = await githubIssues . searchForExistingGithubIssue ( { keywords : [ issueTitleBase , lastUpdatedDate ] , github, context } ) ;
26+
27+ if ( existingGithubIssues . total_count === 0 ) {
28+ createNewGithubIssue ( { lastUpdatedDate, diffWithLastUsedVersion, github, context, dryRun } ) ;
29+ }
30+ else if ( existingGithubIssues . total_count === 1 ) {
31+ log . info ( 'An issue already exists for this update.' ) ;
32+ }
33+ else {
34+ log . warning ( `Multiple issues exist for the HTTP status code update from ${ lastUpdatedDate } :\n${ JSON . stringify ( existingGithubIssues , undefined , 4 ) } ` ) ;
35+ }
36+ }
37+ catch ( error ) {
38+ core . setFailed ( `Could not check for HTTP status codes updates: ${ error } ` ) ;
39+ throw error ;
40+ }
41+ } ;
42+
43+ const fetchQNetworkReplyErrorCodeList = async ( github ) => {
44+
45+ const response = await github . repos . getContent ( {
46+ owner : 'qt' ,
47+ repo : 'qtbase' ,
48+ path : 'src/network/access/qnetworkreply.h' ,
49+ ref : 'dev'
50+ } ) ;
51+ const commitId = response . sha ;
52+
53+ const qnetworkReplyHeaderSource = decodeRepoContent ( response ) ;
54+ const qnetworkReplyErrorCodes = extractQNetworkReplyErrorCodes ( qnetworkReplyHeaderSource ) ;
55+
56+ return { commitId, qnetworkReplyErrorCodes } ;
57+ } ;
58+
59+ const decodeRepoContent = ( response ) => {
60+ try {
61+ return Buffer . from ( response . content , response . encoding ) . toString ( 'utf-8' ) ;
62+ }
63+ catch ( e ) {
64+ throw Error ( `Unsupported repository content encoding: ${ response . encoding } \nOriginal exception: ${ e } ` ) ;
65+ }
66+ } ;
67+
68+ const extractQNetworkReplyErrorCodes = ( qnetworkReplyHeaderSource ) => {
69+
70+ }
71+
72+ const getDiffWithLastUsedVersion = async ( httpStatusCodeList ) => {
73+ const pathToLastUsedVersion = path . resolve ( './.github/http-status-codes.txt' ) ;
74+ const lastUsedVersion = await fs . readFile ( pathToLastUsedVersion , { encoding : 'utf-8' } ) ;
75+ if ( lastUsedVersion === httpStatusCodeList ) {
76+ return null ;
77+ }
78+ const patch = diff . createPatch ( 'http-status-codes.txt' , lastUsedVersion , httpStatusCodeList ) ;
79+ return patch ;
80+ } ;
81+
82+ const createNewGithubIssue = async ( { diffWithLastUsedVersion, github, context, dryRun } ) => {
83+ const title = `${ issueTitleBase } ${ lastUpdatedDate } ` ;
84+ const body = 'The `QNetworkReply::NetworkError` codes list has been updated. \n' +
85+ 'See https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml' + '\n\n' +
86+ '## Diff ##' + '\n' +
87+ '```diff' + '\n' +
88+ diffWithLastUsedVersion + '\n' +
89+ '```' ;
90+
91+ if ( dryRun ) {
92+ log . info ( `Would create issue:\n${ JSON . stringify ( { title, body } , null , 4 ) } ` ) ;
93+ }
94+ else {
95+ const newIssue = await githubIssues . createNewGithubIssue ( { title, body, github, context } ) ;
96+ log . info ( `Created issue #${ newIssue . number } : ${ newIssue . html_url } ` ) ;
97+ }
98+ } ;
99+
100+ const main = async ( ) => {
101+ try
102+ {
103+ const qnetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeList ( ) ;
104+ const patch = await getDiffWithLastUsedVersion ( qnetworkReplyErrorCodes ) ;
105+ if ( patch ) {
106+ log . log ( patch ) ;
107+ }
108+ }
109+ catch ( reason ) {
110+ log . error ( reason ) ;
111+ process . exitCode = - 1 ;
112+ } ;
113+ } ;
114+
115+ module . exports = checkForUpdate ;
116+
117+ if ( require . main === module ) {
118+ main ( ) ;
119+ }
0 commit comments