Skip to content

Commit

Permalink
#32943: Most codes do not trigger retry, fixed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
janssen-tiobe committed Oct 9, 2023
1 parent df212ad commit 723c0ba
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
19 changes: 13 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 =
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/tics/api_helper.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -19,7 +19,11 @@ export async function httpRequest<T>(url: string): Promise<T | undefined> {

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:
Expand Down
4 changes: 4 additions & 0 deletions test/.setup/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
17 changes: 8 additions & 9 deletions test/tics/api_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('httpRequest', () => {

const response = await httpRequest<any>('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);
Expand All @@ -46,7 +46,7 @@ describe('httpRequest', () => {
const exit = jest.spyOn(logger, 'exit');

const response = await httpRequest<any>('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);
Expand All @@ -59,7 +59,7 @@ describe('httpRequest', () => {

const response = await httpRequest<any>('url');
const calledWith =
'Retried undefined time(s), but got: HTTP request failed with status 401. Please provide a valid TICSAUTHTOKEN in your configuration. Check <url>/Administration.html#page=authToken';
'HTTP request failed with status 401. Please provide a valid TICSAUTHTOKEN in your configuration. Check <url>/Administration.html#page=authToken';

expect(response).toEqual(undefined);
expect(exit).toHaveBeenCalledTimes(1);
Expand All @@ -71,7 +71,7 @@ describe('httpRequest', () => {
const exit = jest.spyOn(logger, 'exit');

const response = await httpRequest<any>('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);
Expand All @@ -83,22 +83,21 @@ describe('httpRequest', () => {
const exit = jest.spyOn(logger, 'exit');

const response = await httpRequest<any>('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<any> => Promise.resolve({ message: { statusCode: null, statusMessage: 'Just because' } })
(): Promise<any> => Promise.resolve({ message: { statusCode: 502, statusMessage: 'Just because' } })
);
const exit = jest.spyOn(logger, 'exit');

const response = await httpRequest<any>('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);
Expand Down

0 comments on commit 723c0ba

Please sign in to comment.