Skip to content

Commit

Permalink
Reduce isHeaderPngAccessible() timeout (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea authored Dec 26, 2024
1 parent ac9f827 commit c8a1753
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
21 changes: 18 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,21 +826,34 @@ class Utils {
static getUsageBadge() {
return `![](${process.env.JF_URL}/ui/api/v1/u?s=1&m=1&job_id=${process.env.GITHUB_JOB}&run_id=${process.env.GITHUB_RUN_ID}&git_repo=${process.env.GITHUB_REPOSITORY})`;
}
/**
* Checks if the header image is accessible via the internet.
* Saves the result in a static variable to avoid multiple checks.
* @private
*/
static isHeaderPngAccessible() {
return __awaiter(this, void 0, void 0, function* () {
if (this.isSummaryHeaderAccessible != undefined) {
return this.isSummaryHeaderAccessible;
}
const url = this.MARKDOWN_HEADER_PNG_URL;
const httpClient = new http_client_1.HttpClient();
try {
const response = yield httpClient.head(url);
return response.message.statusCode === 200;
// Set timeout to 5 seconds
const requestOptions = {
socketTimeout: 5000,
};
const response = yield httpClient.head(url, requestOptions);
this.isSummaryHeaderAccessible = response.message.statusCode === 200;
}
catch (error) {
core.warning('No internet access to the header image, using the text header instead.');
return false;
this.isSummaryHeaderAccessible = false;
}
finally {
httpClient.dispose();
}
return this.isSummaryHeaderAccessible;
});
}
static getTempDirectory() {
Expand Down Expand Up @@ -917,3 +930,5 @@ Utils.CUSTOM_SERVER_ID = 'custom-server-id';
// It cannot be linked to the repository, as GitHub serves the image from a CDN,
// which gets blocked by the browser, resulting in an empty image.
Utils.MARKDOWN_HEADER_PNG_URL = 'https://media.jfrog.com/wp-content/uploads/2024/09/02161430/jfrog-job-summary.svg';
// Flag to indicate if the summary header is accessible, can be undefined if not checked yet.
Utils.isSummaryHeaderAccessible = undefined;
22 changes: 18 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export class Utils {
// It cannot be linked to the repository, as GitHub serves the image from a CDN,
// which gets blocked by the browser, resulting in an empty image.
private static MARKDOWN_HEADER_PNG_URL: string = 'https://media.jfrog.com/wp-content/uploads/2024/09/02161430/jfrog-job-summary.svg';
private static isSummaryHeaderAccessible: boolean;
// Flag to indicate if the summary header is accessible, can be undefined if not checked yet.
private static isSummaryHeaderAccessible: boolean | undefined = undefined;

/**
* Retrieves server credentials for accessing JFrog's server
Expand Down Expand Up @@ -913,18 +914,31 @@ export class Utils {
return `![](${process.env.JF_URL}/ui/api/v1/u?s=1&m=1&job_id=${process.env.GITHUB_JOB}&run_id=${process.env.GITHUB_RUN_ID}&git_repo=${process.env.GITHUB_REPOSITORY})`;
}

/**
* Checks if the header image is accessible via the internet.
* Saves the result in a static variable to avoid multiple checks.
* @private
*/
private static async isHeaderPngAccessible(): Promise<boolean> {
if (this.isSummaryHeaderAccessible != undefined) {
return this.isSummaryHeaderAccessible;
}
const url: string = this.MARKDOWN_HEADER_PNG_URL;
const httpClient: HttpClient = new HttpClient();
try {
const response: HttpClientResponse = await httpClient.head(url);
return response.message.statusCode === 200;
// Set timeout to 5 seconds
const requestOptions: OutgoingHttpHeaders = {
socketTimeout: 5000,
};
const response: HttpClientResponse = await httpClient.head(url, requestOptions);
this.isSummaryHeaderAccessible = response.message.statusCode === 200;
} catch (error) {
core.warning('No internet access to the header image, using the text header instead.');
return false;
this.isSummaryHeaderAccessible = false;
} finally {
httpClient.dispose();
}
return this.isSummaryHeaderAccessible;
}

private static getTempDirectory(): string {
Expand Down

0 comments on commit c8a1753

Please sign in to comment.