Skip to content

Commit c8a1753

Browse files
authored
Reduce isHeaderPngAccessible() timeout (#235)
1 parent ac9f827 commit c8a1753

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/utils.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -826,21 +826,34 @@ class Utils {
826826
static getUsageBadge() {
827827
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})`;
828828
}
829+
/**
830+
* Checks if the header image is accessible via the internet.
831+
* Saves the result in a static variable to avoid multiple checks.
832+
* @private
833+
*/
829834
static isHeaderPngAccessible() {
830835
return __awaiter(this, void 0, void 0, function* () {
836+
if (this.isSummaryHeaderAccessible != undefined) {
837+
return this.isSummaryHeaderAccessible;
838+
}
831839
const url = this.MARKDOWN_HEADER_PNG_URL;
832840
const httpClient = new http_client_1.HttpClient();
833841
try {
834-
const response = yield httpClient.head(url);
835-
return response.message.statusCode === 200;
842+
// Set timeout to 5 seconds
843+
const requestOptions = {
844+
socketTimeout: 5000,
845+
};
846+
const response = yield httpClient.head(url, requestOptions);
847+
this.isSummaryHeaderAccessible = response.message.statusCode === 200;
836848
}
837849
catch (error) {
838850
core.warning('No internet access to the header image, using the text header instead.');
839-
return false;
851+
this.isSummaryHeaderAccessible = false;
840852
}
841853
finally {
842854
httpClient.dispose();
843855
}
856+
return this.isSummaryHeaderAccessible;
844857
});
845858
}
846859
static getTempDirectory() {
@@ -917,3 +930,5 @@ Utils.CUSTOM_SERVER_ID = 'custom-server-id';
917930
// It cannot be linked to the repository, as GitHub serves the image from a CDN,
918931
// which gets blocked by the browser, resulting in an empty image.
919932
Utils.MARKDOWN_HEADER_PNG_URL = 'https://media.jfrog.com/wp-content/uploads/2024/09/02161430/jfrog-job-summary.svg';
933+
// Flag to indicate if the summary header is accessible, can be undefined if not checked yet.
934+
Utils.isSummaryHeaderAccessible = undefined;

src/utils.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export class Utils {
8181
// It cannot be linked to the repository, as GitHub serves the image from a CDN,
8282
// which gets blocked by the browser, resulting in an empty image.
8383
private static MARKDOWN_HEADER_PNG_URL: string = 'https://media.jfrog.com/wp-content/uploads/2024/09/02161430/jfrog-job-summary.svg';
84-
private static isSummaryHeaderAccessible: boolean;
84+
// Flag to indicate if the summary header is accessible, can be undefined if not checked yet.
85+
private static isSummaryHeaderAccessible: boolean | undefined = undefined;
8586

8687
/**
8788
* Retrieves server credentials for accessing JFrog's server
@@ -913,18 +914,31 @@ export class Utils {
913914
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})`;
914915
}
915916

917+
/**
918+
* Checks if the header image is accessible via the internet.
919+
* Saves the result in a static variable to avoid multiple checks.
920+
* @private
921+
*/
916922
private static async isHeaderPngAccessible(): Promise<boolean> {
923+
if (this.isSummaryHeaderAccessible != undefined) {
924+
return this.isSummaryHeaderAccessible;
925+
}
917926
const url: string = this.MARKDOWN_HEADER_PNG_URL;
918927
const httpClient: HttpClient = new HttpClient();
919928
try {
920-
const response: HttpClientResponse = await httpClient.head(url);
921-
return response.message.statusCode === 200;
929+
// Set timeout to 5 seconds
930+
const requestOptions: OutgoingHttpHeaders = {
931+
socketTimeout: 5000,
932+
};
933+
const response: HttpClientResponse = await httpClient.head(url, requestOptions);
934+
this.isSummaryHeaderAccessible = response.message.statusCode === 200;
922935
} catch (error) {
923936
core.warning('No internet access to the header image, using the text header instead.');
924-
return false;
937+
this.isSummaryHeaderAccessible = false;
925938
} finally {
926939
httpClient.dispose();
927940
}
941+
return this.isSummaryHeaderAccessible;
928942
}
929943

930944
private static getTempDirectory(): string {

0 commit comments

Comments
 (0)