diff --git a/dist/index.js b/dist/index.js index 37a04a94..fa293ab2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7,7 +7,7 @@ "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.viewerUrl = exports.baseUrl = exports.httpClient = exports.octokit = exports.ticsConfig = exports.githubConfig = void 0; +exports.viewerUrl = exports.baseUrl = exports.httpClient = exports.octokit = exports.retryConfig = exports.ticsConfig = exports.githubConfig = void 0; const core_1 = __nccwpck_require__(2186); const github_1 = __nccwpck_require__(5438); const http_client_1 = __nccwpck_require__(6255); @@ -71,17 +71,20 @@ exports.ticsConfig = { tmpDir: (0, core_1.getInput)('tmpDir'), trustStrategy: (0, core_1.getInput)('trustStrategy'), secretsFilter: getSecretsFilter((0, core_1.getInput)('secretsFilter')), - viewerUrl: (0, core_1.getInput)('viewerUrl'), - retries: 10 + viewerUrl: (0, core_1.getInput)('viewerUrl') +}; +exports.retryConfig = { + maxRetries: 10, + retryCodes: [http_client_1.HttpCodes.BadGateway, http_client_1.HttpCodes.ServiceUnavailable, http_client_1.HttpCodes.GatewayTimeout] }; const ignoreSslError = exports.ticsConfig.hostnameVerification === '0' || exports.ticsConfig.hostnameVerification === 'false' || exports.ticsConfig.trustStrategy === 'self-signed' || exports.ticsConfig.trustStrategy === 'all'; -exports.octokit = (0, github_1.getOctokit)(exports.ticsConfig.githubToken, { request: { retries: exports.ticsConfig.retries, retryAfter: 5 } }, (__nccwpck_require__(6298).retry)); +exports.octokit = (0, github_1.getOctokit)(exports.ticsConfig.githubToken, { request: { retries: exports.retryConfig.maxRetries, retryAfter: 5 } }, (__nccwpck_require__(6298).retry)); exports.httpClient = new http_client_1.HttpClient('tics-github-action', undefined, { allowRetries: true, - maxRetries: exports.ticsConfig.retries, + maxRetries: exports.retryConfig.maxRetries, ignoreSslError: ignoreSslError }); exports.baseUrl = (0, api_helper_1.getTicsWebBaseUrlFromUrl)(exports.ticsConfig.ticsConfiguration); @@ -1225,7 +1228,11 @@ async function httpRequest(url) { headers.Authorization = `Basic ${configuration_1.ticsConfig.ticsAuthToken}`; } const response = await configuration_1.httpClient.get(url, headers); - const errorMessage = `Retried ${configuration_1.ticsConfig.retries} time(s), but got: HTTP request failed with status ${response.message.statusCode}.`; + let errorMessage = ''; + if (response.message.statusCode && configuration_1.retryConfig.retryCodes.includes(response.message.statusCode)) { + errorMessage += `Retried ${configuration_1.retryConfig.maxRetries} time(s), but got: `; + } + errorMessage += `HTTP request failed with status ${response.message.statusCode}.`; switch (response.message.statusCode) { case 200: const text = await response.readBody(); diff --git a/src/configuration.ts b/src/configuration.ts index 98188445..2b977c36 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -1,6 +1,6 @@ import { getBooleanInput, getInput, isDebug } from '@actions/core'; import { context, getOctokit } from '@actions/github'; -import { HttpClient } from '@actions/http-client'; +import { HttpClient, HttpCodes } from '@actions/http-client'; import { getTicsWebBaseUrlFromUrl } from './tics/api_helper'; import { EOL } from 'os'; @@ -64,8 +64,12 @@ export const ticsConfig = { tmpDir: getInput('tmpDir'), trustStrategy: getInput('trustStrategy'), secretsFilter: getSecretsFilter(getInput('secretsFilter')), - viewerUrl: getInput('viewerUrl'), - retries: 10 + viewerUrl: getInput('viewerUrl') +}; + +export const retryConfig = { + maxRetries: 10, + retryCodes: [HttpCodes.BadGateway, HttpCodes.ServiceUnavailable, HttpCodes.GatewayTimeout] }; const ignoreSslError: boolean = @@ -76,12 +80,12 @@ const ignoreSslError: boolean = export const octokit = getOctokit( ticsConfig.githubToken, - { request: { retries: ticsConfig.retries, retryAfter: 5 } }, + { request: { retries: retryConfig.maxRetries, retryAfter: 5 } }, require('@octokit/plugin-retry').retry ); export const httpClient = new HttpClient('tics-github-action', undefined, { allowRetries: true, - maxRetries: ticsConfig.retries, + maxRetries: retryConfig.maxRetries, ignoreSslError: ignoreSslError }); export const baseUrl = getTicsWebBaseUrlFromUrl(ticsConfig.ticsConfiguration); diff --git a/src/tics/api_helper.ts b/src/tics/api_helper.ts index 95244f7b..16be0db9 100644 --- a/src/tics/api_helper.ts +++ b/src/tics/api_helper.ts @@ -1,6 +1,6 @@ import { OutgoingHttpHeaders } from 'http'; import { logger } from '../helper/logger'; -import { githubConfig, httpClient, ticsConfig, viewerUrl } from '../configuration'; +import { githubConfig, httpClient, retryConfig, ticsConfig, viewerUrl } from '../configuration'; import { Analysis, HttpResponse } from '../helper/interfaces'; import { HttpClientResponse } from '@actions/http-client'; @@ -19,7 +19,11 @@ export async function httpRequest(url: string): Promise { const response: HttpClientResponse = await httpClient.get(url, headers); - const errorMessage = `Retried ${ticsConfig.retries} time(s), but got: HTTP request failed with status ${response.message.statusCode}.`; + let errorMessage = ''; + if (response.message.statusCode && retryConfig.retryCodes.includes(response.message.statusCode)) { + errorMessage += `Retried ${retryConfig.maxRetries} time(s), but got: `; + } + errorMessage += `HTTP request failed with status ${response.message.statusCode}.`; switch (response.message.statusCode) { case 200: diff --git a/test/.setup/mock.ts b/test/.setup/mock.ts index 2bdae607..e898545f 100644 --- a/test/.setup/mock.ts +++ b/test/.setup/mock.ts @@ -23,6 +23,10 @@ jest.mock('../../src/configuration', () => { id: '123-1', commitSha: 'asdfghjk' }, + retryConfig: { + maxRetries: 10, + retryCodes: [502, 503, 504] + }, octokit: { paginate: jest.fn(), rest: { diff --git a/test/tics/api_helper.test.ts b/test/tics/api_helper.test.ts index bff225ba..eea2746d 100644 --- a/test/tics/api_helper.test.ts +++ b/test/tics/api_helper.test.ts @@ -32,7 +32,7 @@ describe('httpRequest', () => { const response = await httpRequest('url'); const calledWith = - 'Retried undefined time(s), but got: HTTP request failed with status 302. Please check if the given ticsConfiguration is correct (possibly http instead of https).'; + 'HTTP request failed with status 302. Please check if the given ticsConfiguration is correct (possibly http instead of https).'; expect(response).toEqual(undefined); expect(exit).toHaveBeenCalledTimes(1); @@ -46,7 +46,7 @@ describe('httpRequest', () => { const exit = jest.spyOn(logger, 'exit'); const response = await httpRequest('url'); - const calledWith = 'Retried undefined time(s), but got: HTTP request failed with status 400. header'; + const calledWith = 'HTTP request failed with status 400. header'; expect(response).toEqual(undefined); expect(exit).toHaveBeenCalledTimes(1); @@ -59,7 +59,7 @@ describe('httpRequest', () => { const response = await httpRequest('url'); const calledWith = - 'Retried undefined time(s), but got: HTTP request failed with status 401. Please provide a valid TICSAUTHTOKEN in your configuration. Check /Administration.html#page=authToken'; + 'HTTP request failed with status 401. Please provide a valid TICSAUTHTOKEN in your configuration. Check /Administration.html#page=authToken'; expect(response).toEqual(undefined); expect(exit).toHaveBeenCalledTimes(1); @@ -71,7 +71,7 @@ describe('httpRequest', () => { const exit = jest.spyOn(logger, 'exit'); const response = await httpRequest('url'); - const calledWith = 'Retried undefined time(s), but got: HTTP request failed with status 403. Forbidden call: url'; + const calledWith = 'HTTP request failed with status 403. Forbidden call: url'; expect(response).toEqual(undefined); expect(exit).toHaveBeenCalledTimes(1); @@ -83,22 +83,21 @@ describe('httpRequest', () => { const exit = jest.spyOn(logger, 'exit'); const response = await httpRequest('url'); - const calledWith = - 'Retried undefined time(s), but got: HTTP request failed with status 404. Please check if the given ticsConfiguration is correct.'; + const calledWith = 'HTTP request failed with status 404. Please check if the given ticsConfiguration is correct.'; expect(response).toEqual(undefined); expect(exit).toHaveBeenCalledTimes(1); expect(exit).toHaveBeenCalledWith(calledWith); }); - test('Should return undefined response and call exit on status null', async () => { + test('Should return undefined response and call exit on status 502', async () => { (httpClient.get as any).mockImplementationOnce( - (): Promise => Promise.resolve({ message: { statusCode: null, statusMessage: 'Just because' } }) + (): Promise => Promise.resolve({ message: { statusCode: 502, statusMessage: 'Just because' } }) ); const exit = jest.spyOn(logger, 'exit'); const response = await httpRequest('url'); - const calledWith = 'Retried undefined time(s), but got: HTTP request failed with status null. Just because'; + const calledWith = 'Retried 10 time(s), but got: HTTP request failed with status 502. Just because'; expect(response).toEqual(undefined); expect(exit).toHaveBeenCalledTimes(1);