Skip to content

Commit

Permalink
#32943: Added logging the retry count if an error occured.
Browse files Browse the repository at this point in the history
  • Loading branch information
janssen-tiobe committed Oct 9, 2023
1 parent 6697bd0 commit df212ad
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 81 deletions.
96 changes: 55 additions & 41 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,19 @@ 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')
viewerUrl: (0, core_1.getInput)('viewerUrl'),
retries: 10
};
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: 10 } }, (__nccwpck_require__(6298).retry));
exports.httpClient = new http_client_1.HttpClient('tics-github-action', undefined, { allowRetries: true, maxRetries: 10, ignoreSslError: ignoreSslError });
exports.octokit = (0, github_1.getOctokit)(exports.ticsConfig.githubToken, { request: { retries: exports.ticsConfig.retries, retryAfter: 5 } }, (__nccwpck_require__(6298).retry));
exports.httpClient = new http_client_1.HttpClient('tics-github-action', undefined, {
allowRetries: true,
maxRetries: exports.ticsConfig.retries,
ignoreSslError: ignoreSslError
});
exports.baseUrl = (0, api_helper_1.getTicsWebBaseUrlFromUrl)(exports.ticsConfig.ticsConfiguration);
exports.viewerUrl = exports.ticsConfig.viewerUrl ? exports.ticsConfig.viewerUrl.replace(/\/+$/, '') : exports.baseUrl;

Expand All @@ -94,6 +99,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.deletePreviousReviewComments = exports.postAnnotations = exports.getPostedReviewComments = void 0;
const logger_1 = __nccwpck_require__(6440);
const configuration_1 = __nccwpck_require__(5527);
const error_1 = __nccwpck_require__(5922);
/**
* Gets a list of all reviews posted on the pull request.
* @returns List of reviews posted on the pull request.
Expand All @@ -110,9 +116,7 @@ async function getPostedReviewComments() {
response = await configuration_1.octokit.paginate(configuration_1.octokit.rest.pulls.listReviewComments, params);
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Could not retrieve the review comments: ${message}`);
}
return response;
Expand Down Expand Up @@ -156,9 +160,7 @@ function deletePreviousReviewComments(postedReviewComments) {
await configuration_1.octokit.rest.pulls.deleteReviewComment(params);
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Could not delete review comment: ${message}`);
}
}
Expand All @@ -183,6 +185,7 @@ const artifact_1 = __nccwpck_require__(2605);
const logger_1 = __nccwpck_require__(6440);
const fs_1 = __nccwpck_require__(7147);
const canonical_path_1 = __nccwpck_require__(5806);
const error_1 = __nccwpck_require__(5922);
async function uploadArtifact() {
const artifactClient = (0, artifact_1.create)();
try {
Expand All @@ -195,9 +198,7 @@ async function uploadArtifact() {
}
}
catch (error) {
let message = 'reason unknown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.debug('Failed to upload artifact: ' + message);
}
}
Expand Down Expand Up @@ -249,6 +250,7 @@ const configuration_1 = __nccwpck_require__(5527);
const summary_1 = __nccwpck_require__(1502);
const markdown_1 = __nccwpck_require__(5300);
const enums_1 = __nccwpck_require__(1655);
const error_1 = __nccwpck_require__(5922);
/**
* Gets a list of all comments on the pull request.
* @returns List of comments on the pull request.
Expand All @@ -265,9 +267,7 @@ async function getPostedComments() {
response = await configuration_1.octokit.paginate(configuration_1.octokit.rest.issues.listComments, params);
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Could not retrieve the comments: ${message}`);
}
return response;
Expand Down Expand Up @@ -308,9 +308,7 @@ async function postComment(body) {
logger_1.logger.info('Posted comment for this pull request.');
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Posting the comment failed: ${message}`);
}
}
Expand All @@ -328,9 +326,7 @@ function deletePreviousComments(comments) {
await configuration_1.octokit.rest.issues.deleteComment(params);
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Removing a comment failed: ${message}`);
}
}
Expand Down Expand Up @@ -363,6 +359,7 @@ exports.getChangedFilesOfCommit = void 0;
const canonical_path_1 = __nccwpck_require__(5806);
const logger_1 = __nccwpck_require__(6440);
const configuration_1 = __nccwpck_require__(5527);
const error_1 = __nccwpck_require__(5922);
/**
* Sends a request to retrieve the changed files for a given pull request to the GitHub API.
* @returns List of changed files within the GitHub Pull request.
Expand Down Expand Up @@ -400,9 +397,7 @@ async function getChangedFilesOfCommit() {
logger_1.logger.info('Retrieved changed files from commit.');
}
catch (error) {
let message = 'error unknown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.exit(`Could not retrieve the changed files: ${message}`);
}
return response;
Expand All @@ -423,6 +418,7 @@ const fs_1 = __nccwpck_require__(7147);
const canonical_path_1 = __nccwpck_require__(5806);
const logger_1 = __nccwpck_require__(6440);
const configuration_1 = __nccwpck_require__(5527);
const error_1 = __nccwpck_require__(5922);
/**
* Sends a request to retrieve the changed files for a given pull request to the GitHub API.
* @returns List of changed files within the GitHub Pull request.
Expand Down Expand Up @@ -458,9 +454,7 @@ async function getChangedFilesOfPullRequest() {
logger_1.logger.info('Retrieved changed files from pull request.');
}
catch (error) {
let message = 'error unknown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.exit(`Could not retrieve the changed files: ${message}`);
}
return response;
Expand Down Expand Up @@ -498,6 +492,7 @@ const logger_1 = __nccwpck_require__(6440);
const configuration_1 = __nccwpck_require__(5527);
const enums_1 = __nccwpck_require__(1655);
const markdown_1 = __nccwpck_require__(5300);
const error_1 = __nccwpck_require__(5922);
/**
* Create review on the pull request from the analysis given.
* @param body Body containing the summary of the review
Expand All @@ -517,9 +512,7 @@ async function postReview(body, event) {
logger_1.logger.info('Posted review for this pull request.');
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Posting the review failed: ${message}`);
}
}
Expand All @@ -543,9 +536,7 @@ async function postNothingAnalyzedReview(message) {
logger_1.logger.info('Posted review for this pull request.');
}
catch (error) {
let message = 'reason unkown';
if (error instanceof Error)
message = error.message;
const message = (0, error_1.handleOctokitError)(error);
logger_1.logger.error(`Posting the review failed: ${message}`);
}
}
Expand Down Expand Up @@ -576,6 +567,30 @@ var Events;
})(Events || (exports.Events = Events = {}));


/***/ }),

/***/ 5922:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.handleOctokitError = void 0;
function handleOctokitError(error) {
let message = 'reason unkown';
if (error instanceof Error) {
message = '';
const retryCount = error.request?.request?.retryCount;
if (retryCount) {
message = `Retried ${retryCount} time(s), but got: `;
}
message += error.message;
}
return message;
}
exports.handleOctokitError = handleOctokitError;


/***/ }),

/***/ 6440:
Expand Down Expand Up @@ -1210,6 +1225,7 @@ 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}.`;
switch (response.message.statusCode) {
case 200:
const text = await response.readBody();
Expand All @@ -1221,24 +1237,22 @@ async function httpRequest(url) {
}
break;
case 302:
logger_1.logger.exit(`HTTP request failed with status ${response.message.statusCode}. Please check if the given ticsConfiguration is correct (possibly http instead of https).`);
logger_1.logger.exit(`${errorMessage} Please check if the given ticsConfiguration is correct (possibly http instead of https).`);
break;
case 400:
const body = await response.readBody();
console.log(body);
logger_1.logger.exit(`HTTP request failed with status ${response.message.statusCode}. ${JSON.parse(body).alertMessages[0].header}`);
logger_1.logger.exit(`${errorMessage} ${JSON.parse(await response.readBody()).alertMessages[0].header}`);
break;
case 401:
logger_1.logger.exit(`HTTP request failed with status ${response.message.statusCode}. Please provide a valid TICSAUTHTOKEN in your configuration. Check ${configuration_1.viewerUrl}/Administration.html#page=authToken`);
logger_1.logger.exit(`${errorMessage} Please provide a valid TICSAUTHTOKEN in your configuration. Check ${configuration_1.viewerUrl}/Administration.html#page=authToken`);
break;
case 403:
logger_1.logger.exit(`HTTP request failed with status ${response.message.statusCode}. Forbidden call: ${url}`);
logger_1.logger.exit(`${errorMessage} Forbidden call: ${url}`);
break;
case 404:
logger_1.logger.exit(`HTTP request failed with status ${response.message.statusCode}. Please check if the given ticsConfiguration is correct.`);
logger_1.logger.exit(`${errorMessage} Please check if the given ticsConfiguration is correct.`);
break;
default:
logger_1.logger.exit(`HTTP request failed with status ${response.message.statusCode}. Please check if your configuration is correct.`);
logger_1.logger.exit(`${errorMessage} ${response.message.statusMessage}`);
break;
}
return;
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@actions/github": "^5.1.1",
"@actions/http-client": "^2.1.1",
"@octokit/plugin-retry": "^6.0.1",
"@octokit/request-error": "^5.0.1",
"canonical-path": "^1.0.0",
"compare-versions": "^6.1.0",
"semver": "^7.5.4",
Expand Down
15 changes: 12 additions & 3 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export const ticsConfig = {
tmpDir: getInput('tmpDir'),
trustStrategy: getInput('trustStrategy'),
secretsFilter: getSecretsFilter(getInput('secretsFilter')),
viewerUrl: getInput('viewerUrl')
viewerUrl: getInput('viewerUrl'),
retries: 10
};

const ignoreSslError: boolean =
Expand All @@ -73,7 +74,15 @@ const ignoreSslError: boolean =
ticsConfig.trustStrategy === 'self-signed' ||
ticsConfig.trustStrategy === 'all';

export const octokit = getOctokit(ticsConfig.githubToken, { request: { retries: 10 } }, require('@octokit/plugin-retry').retry);
export const httpClient = new HttpClient('tics-github-action', undefined, { allowRetries: true, maxRetries: 10, ignoreSslError: ignoreSslError });
export const octokit = getOctokit(
ticsConfig.githubToken,
{ request: { retries: ticsConfig.retries, retryAfter: 5 } },
require('@octokit/plugin-retry').retry
);
export const httpClient = new HttpClient('tics-github-action', undefined, {
allowRetries: true,
maxRetries: ticsConfig.retries,
ignoreSslError: ignoreSslError
});
export const baseUrl = getTicsWebBaseUrlFromUrl(ticsConfig.ticsConfiguration);
export const viewerUrl = ticsConfig.viewerUrl ? ticsConfig.viewerUrl.replace(/\/+$/, '') : baseUrl;
7 changes: 3 additions & 4 deletions src/github/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { logger } from '../helper/logger';
import { githubConfig, octokit } from '../configuration';
import { ReviewComment } from './interfaces';
import { AnalysisResults, TicsReviewComment } from '../helper/interfaces';
import { handleOctokitError } from '../helper/error';

/**
* Gets a list of all reviews posted on the pull request.
Expand All @@ -18,8 +19,7 @@ export async function getPostedReviewComments(): Promise<ReviewComment[]> {
};
response = await octokit.paginate(octokit.rest.pulls.listReviewComments, params);
} catch (error: unknown) {
let message = 'reason unkown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.error(`Could not retrieve the review comments: ${message}`);
}
return response;
Expand Down Expand Up @@ -65,8 +65,7 @@ export function deletePreviousReviewComments(postedReviewComments: ReviewComment
};
await octokit.rest.pulls.deleteReviewComment(params);
} catch (error: unknown) {
let message = 'reason unkown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.error(`Could not delete review comment: ${message}`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/github/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { create } from '@actions/artifact';
import { logger } from '../helper/logger';
import { readdirSync } from 'fs';
import { join } from 'canonical-path';
import { handleOctokitError } from '../helper/error';

export async function uploadArtifact(): Promise<void> {
const artifactClient = create();
Expand All @@ -18,8 +19,7 @@ export async function uploadArtifact(): Promise<void> {
logger.debug(`Failed to upload file(s): ${response.failedItems.join(', ')}`);
}
} catch (error: unknown) {
let message = 'reason unknown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.debug('Failed to upload artifact: ' + message);
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/github/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createErrorSummary } from '../helper/summary';
import { generateStatusMarkdown } from '../helper/markdown';
import { Status } from '../helper/enums';
import { Comment } from './interfaces';
import { handleOctokitError } from '../helper/error';

/**
* Gets a list of all comments on the pull request.
Expand All @@ -21,8 +22,7 @@ export async function getPostedComments(): Promise<Comment[]> {
};
response = await octokit.paginate(octokit.rest.issues.listComments, params);
} catch (error: unknown) {
let message = 'reason unkown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.error(`Could not retrieve the comments: ${message}`);
}
return response;
Expand Down Expand Up @@ -65,8 +65,7 @@ export async function postComment(body: string): Promise<void> {
await octokit.rest.issues.createComment(params);
logger.info('Posted comment for this pull request.');
} catch (error: unknown) {
let message = 'reason unkown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.error(`Posting the comment failed: ${message}`);
}
}
Expand All @@ -83,8 +82,7 @@ export function deletePreviousComments(comments: Comment[]): void {
};
await octokit.rest.issues.deleteComment(params);
} catch (error: unknown) {
let message = 'reason unkown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.error(`Removing a comment failed: ${message}`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/github/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { normalize } from 'canonical-path';
import { logger } from '../helper/logger';
import { githubConfig, octokit, ticsConfig } from '../configuration';
import { ChangedFile } from './interfaces';
import { handleOctokitError } from '../helper/error';

/**
* Sends a request to retrieve the changed files for a given pull request to the GitHub API.
Expand Down Expand Up @@ -39,8 +40,7 @@ export async function getChangedFilesOfCommit(): Promise<ChangedFile[]> {
});
logger.info('Retrieved changed files from commit.');
} catch (error: unknown) {
let message = 'error unknown';
if (error instanceof Error) message = error.message;
const message = handleOctokitError(error);
logger.exit(`Could not retrieve the changed files: ${message}`);
}
return response;
Expand Down
Loading

0 comments on commit df212ad

Please sign in to comment.