1+ const fetch = require ( 'node-fetch' ) ;
12const diff = require ( 'diff' ) ;
23const fs = require ( 'fs' ) . promises ;
34const path = require ( 'path' ) ;
45const githubIssues = require ( './githubIssues.js' ) ;
56
67let log = console ;
78
8- const qnetworkReplyHeaderUrl = 'https://code.qt.io/cgit/qt/qtbase.git/plain/src/network/access/qnetworkreply.h' ;
9+ const qNetworkReplyHeaderUrl = 'https://code.qt.io/cgit/qt/qtbase.git/plain/src/network/access/qnetworkreply.h' ;
910const issueTitleBase = 'QNetworkReply Error Code Update' ;
1011
1112const checkForUpdate = async ( { github, core, context, dryRun } ) => {
1213 try
1314 {
1415 log = core ;
1516
16- const qnetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeList ( github ) ;
17- const lastUpdatedDate = httpStatusCodes . lastUpdated ;
18- const diffWithLastUsedVersion = await getDiffWithLastUsedVersion ( httpStatusCodes . httpStatusCodesList ) ;
17+ const { commitId, qNetworkReplyErrorCodes } = await fetchQNetworkReplyErrorCodeListFromGitHub ( github ) ;
18+ const diffWithLastUsedVersion = await getDiffWithLastUsedVersion ( qNetworkReplyErrorCodes ) ;
1919 if ( ! diffWithLastUsedVersion ) {
20- log . info ( 'HTTP status codes list is still up to date' ) ;
20+ log . info ( 'QNetworkReply error codes list is still up to date' ) ;
2121 return ;
2222 }
23- log . warning ( 'HTTP status codes list is outdated!' ) ;
23+ log . warning ( 'QNetworkReply error codes list is outdated!' ) ;
2424
25- const existingGithubIssues = await githubIssues . searchForExistingGithubIssue ( { keywords : [ issueTitleBase , lastUpdatedDate ] , github, context } ) ;
25+ const existingGithubIssues = await githubIssues . searchForExistingGithubIssue ( { keywords : [ issueTitleBase , shortenCommitId ( commitId ) ] , github, context } ) ;
2626
2727 if ( existingGithubIssues . total_count === 0 ) {
28- createNewGithubIssue ( { lastUpdatedDate , diffWithLastUsedVersion, github, context, dryRun } ) ;
28+ createNewGithubIssue ( { commitId , diffWithLastUsedVersion, github, context, dryRun } ) ;
2929 }
3030 else if ( existingGithubIssues . total_count === 1 ) {
3131 log . info ( 'An issue already exists for this update.' ) ;
3232 }
3333 else {
34- log . warning ( `Multiple issues exist for the HTTP status code update from ${ lastUpdatedDate } :\n${ JSON . stringify ( existingGithubIssues , undefined , 4 ) } ` ) ;
34+ log . warning ( `Multiple issues exist for the QNetworkReply error codes update with id ${ commitId } :\n${ JSON . stringify ( existingGithubIssues , undefined , 4 ) } ` ) ;
3535 }
3636 }
3737 catch ( error ) {
@@ -40,7 +40,11 @@ const checkForUpdate = async ( { github, core, context, dryRun } ) => {
4040 }
4141} ;
4242
43- const fetchQNetworkReplyErrorCodeList = async ( github ) => {
43+ const shortenCommitId = commitId => {
44+ return commitId . substring ( 0 , 6 )
45+ } ;
46+
47+ const fetchQNetworkReplyErrorCodeListFromGitHub = async ( github ) => {
4448
4549 const response = await github . repos . getContent ( {
4650 owner : 'qt' ,
@@ -50,12 +54,22 @@ const fetchQNetworkReplyErrorCodeList = async ( github ) => {
5054 } ) ;
5155 const commitId = response . sha ;
5256
53- const qnetworkReplyHeaderSource = decodeRepoContent ( response ) ;
54- const qnetworkReplyErrorCodes = extractQNetworkReplyErrorCodes ( qnetworkReplyHeaderSource ) ;
57+ const qNetworkReplyHeaderSource = decodeRepoContent ( response ) ;
58+ const qNetworkReplyErrorCodes = extractQNetworkReplyErrorCodes ( qNetworkReplyHeaderSource ) ;
5559
56- return { commitId, qnetworkReplyErrorCodes } ;
60+ return { commitId, qNetworkReplyErrorCodes } ;
5761} ;
5862
63+ const fetchQNetworkReplyErrorCodeListFromQt = async ( ) => {
64+ const response = await fetch ( qNetworkReplyHeaderUrl ) ;
65+ if ( ! response . ok ) {
66+ throw Error ( `Error fetching QNetworkReply header: ${ response . status } ${ response . statusText } ` ) ;
67+ }
68+ const qNetworkReplyHeaderSource = await response . text ( ) ;
69+
70+ return extractQNetworkReplyErrorCodes ( qNetworkReplyHeaderSource ) ;
71+ }
72+
5973const decodeRepoContent = ( response ) => {
6074 try {
6175 return Buffer . from ( response . content , response . encoding ) . toString ( 'utf-8' ) ;
@@ -66,23 +80,29 @@ const decodeRepoContent = ( response ) => {
6680} ;
6781
6882const extractQNetworkReplyErrorCodes = ( qnetworkReplyHeaderSource ) => {
69-
83+ const enumMatch = / e n u m N e t w o r k E r r o r { ( .* ?) } ; / s. exec ( qnetworkReplyHeaderSource ) ;
84+ if ( ! enumMatch ) {
85+ throw Error ( 'Could not extract NetworkError codes from QNetworkReply header' ) ;
86+ }
87+ const enums = enumMatch [ 1 ] . trim ( ) . replace ( / \/ \/ .* $ | [ \t ] + | \n \n + | , / mg, '' ) ;
88+ return enums ;
7089}
7190
72- const getDiffWithLastUsedVersion = async ( httpStatusCodeList ) => {
73- const pathToLastUsedVersion = path . resolve ( './.github/http-status-codes .txt' ) ;
91+ const getDiffWithLastUsedVersion = async ( qNetworkReplyErrorCodes ) => {
92+ const pathToLastUsedVersion = path . resolve ( './.github/QNetworkReplyErroCodes .txt' ) ;
7493 const lastUsedVersion = await fs . readFile ( pathToLastUsedVersion , { encoding : 'utf-8' } ) ;
75- if ( lastUsedVersion === httpStatusCodeList ) {
94+ if ( lastUsedVersion === qNetworkReplyErrorCodes ) {
7695 return null ;
7796 }
78- const patch = diff . createPatch ( 'http-status-codes .txt' , lastUsedVersion , httpStatusCodeList ) ;
97+ const patch = diff . createPatch ( 'QNetworkReplyErroCodes .txt' , lastUsedVersion , qNetworkReplyErrorCodes ) ;
7998 return patch ;
8099} ;
81100
82- const createNewGithubIssue = async ( { diffWithLastUsedVersion, github, context, dryRun } ) => {
83- const title = `${ issueTitleBase } ${ lastUpdatedDate } ` ;
101+ const createNewGithubIssue = async ( { commitId, diffWithLastUsedVersion, github, context, dryRun } ) => {
102+ const commitIdShort = shortenCommitId ( commitId ) ;
103+ const title = `${ issueTitleBase } ${ commitIdShort } ` ;
84104 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' +
105+ ` See [qnetworkreply.h@ ${ commitIdShort } ]( https://code.qt.io/cgit/qt/qtbase.git/commit/src/network/access/qnetworkreply.h?id= ${ commitId } )` + '\n\n' +
86106 '## Diff ##' + '\n' +
87107 '```diff' + '\n' +
88108 diffWithLastUsedVersion + '\n' +
@@ -100,11 +120,14 @@ const createNewGithubIssue = async ( { diffWithLastUsedVersion, github, context,
100120const main = async ( ) => {
101121 try
102122 {
103- const qnetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeList ( ) ;
123+ const qnetworkReplyErrorCodes = await fetchQNetworkReplyErrorCodeListFromQt ( ) ;
104124 const patch = await getDiffWithLastUsedVersion ( qnetworkReplyErrorCodes ) ;
105125 if ( patch ) {
106126 log . log ( patch ) ;
107127 }
128+ else {
129+ log . log ( "Last used version is still up to date!" ) ;
130+ }
108131 }
109132 catch ( reason ) {
110133 log . error ( reason ) ;
0 commit comments