From 3507478d02c2ab1b5a3fc48a8b58ae2adfcfa2a1 Mon Sep 17 00:00:00 2001 From: Darshan Thakare <143271270+DarshanCode2005@users.noreply.github.com> Date: Wed, 7 Jan 2026 20:46:18 +0530 Subject: [PATCH 1/4] refactor: reduce cognitive complexity in HTTP auth resolver --- src/domains/services/validation.service.ts | 92 ++++++++++++++-------- 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/src/domains/services/validation.service.ts b/src/domains/services/validation.service.ts index 993bb1ad..11ac45fc 100644 --- a/src/domains/services/validation.service.ts +++ b/src/domains/services/validation.service.ts @@ -81,6 +81,50 @@ const convertGitHubWebUrl = (url: string): string => { return url; }; +/** + * Helper function to fetch with error handling + */ +const fetchWithErrorHandling = async ( + url: string, + headers: Record, + errorMessage: string, +): Promise => { + const res = await fetch(url, { headers }); + if (!res.ok) { + throw new Error(`${errorMessage}: ${url} - ${res.statusText}`); + } + return res; +}; + +/** + * Helper function to fetch content from GitHub API + */ +const fetchGitHubApiContent = async ( + url: string, + headers: Record, +): Promise => { + headers['Accept'] = 'application/vnd.github.v3+json'; + const res = await fetchWithErrorHandling( + url, + headers, + 'Failed to fetch GitHub API URL', + ); + const fileInfo = (await res.json()) as GitHubFileInfo; + + if (!fileInfo.download_url) { + throw new Error( + `No download URL found in GitHub API response for: ${url}`, + ); + } + + const contentRes = await fetchWithErrorHandling( + fileInfo.download_url, + headers, + 'Failed to fetch content from download URL', + ); + return await contentRes.text(); +}; + /** * Custom resolver for private repositories */ @@ -108,43 +152,23 @@ const createHttpWithAuthResolver = () => ({ } if (url.includes('api.github.com')) { - headers['Accept'] = 'application/vnd.github.v3+json'; - const res = await fetch(url, { headers }); - if (!res.ok) { - throw new Error( - `Failed to fetch GitHub API URL: ${url} - ${res.statusText}` - ); - } - const fileInfo = (await res.json()) as GitHubFileInfo; - - if (fileInfo.download_url) { - const contentRes = await fetch(fileInfo.download_url, { headers }); - if (!contentRes.ok) { - throw new Error( - `Failed to fetch content from download URL: ${fileInfo.download_url} - ${contentRes.statusText}` - ); - } - return await contentRes.text(); - } - throw new Error( - `No download URL found in GitHub API response for: ${url}` - ); - } else if (url.includes('raw.githubusercontent.com')) { + return await fetchGitHubApiContent(url, headers); + } + if (url.includes('raw.githubusercontent.com')) { headers['Accept'] = 'application/vnd.github.v3.raw'; - const res = await fetch(url, { headers }); - if (!res.ok) { - throw new Error( - `Failed to fetch GitHub URL: ${url} - ${res.statusText}` - ); - } - return await res.text(); - } else { - const res = await fetch(url, { headers }); - if (!res.ok) { - throw new Error(`Failed to fetch URL: ${url} - ${res.statusText}`); - } + const res = await fetchWithErrorHandling( + url, + headers, + 'Failed to fetch GitHub URL', + ); return await res.text(); } + const res = await fetchWithErrorHandling( + url, + headers, + 'Failed to fetch URL', + ); + return await res.text(); }, }); From 75904858ba63787132097c6cd9abdf3e1ec36b6e Mon Sep 17 00:00:00 2001 From: Darshan Thakare <143271270+DarshanCode2005@users.noreply.github.com> Date: Thu, 8 Jan 2026 18:58:54 +0530 Subject: [PATCH 2/4] fix: add explicit return statements to all bash functions --- github-action/entrypoint.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/github-action/entrypoint.sh b/github-action/entrypoint.sh index aba399cb..7e7f4bf2 100755 --- a/github-action/entrypoint.sh +++ b/github-action/entrypoint.sh @@ -88,6 +88,7 @@ echo "::endgroup::" handle_file_error () { echo -e "${RED}Validation error: File not found:${NC}" "$1" echo -e "skipping...\n" + return 0 } handle_validate () { @@ -102,6 +103,7 @@ handle_validate () { echo -e "${BLUE}Executing command:${NC}" "asyncapi validate $FILEPATH $PARAMETERS" eval "asyncapi validate $FILEPATH $PARAMETERS" echo "::endgroup::" + return 0 } handle_optimize () { @@ -116,6 +118,7 @@ handle_optimize () { echo -e "${BLUE}Executing command:${NC}" "asyncapi optimize $FILEPATH $PARAMETERS" eval "asyncapi optimize $FILEPATH $PARAMETERS" echo "::endgroup::" + return 0 } handle_generate () { @@ -146,6 +149,7 @@ handle_generate () { exit 1 fi echo "::endgroup::" + return 0 } handle_convert () { @@ -172,6 +176,7 @@ handle_convert () { echo -e "${BLUE}Executing command:${NC}" "asyncapi convert $FILEPATH -o $OUTPUT $PARAMETERS" eval "asyncapi convert $FILEPATH -o $OUTPUT $PARAMETERS" fi + return 0 } if [[ $COMMAND == "validate" ]]; then From 11076e1448340c1fb65932d3720dd7f583a0fea4 Mon Sep 17 00:00:00 2001 From: Darshan Thakare <143271270+DarshanCode2005@users.noreply.github.com> Date: Thu, 8 Jan 2026 19:02:11 +0530 Subject: [PATCH 3/4] fix: replace positional parameter with local variable --- github-action/entrypoint.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github-action/entrypoint.sh b/github-action/entrypoint.sh index 7e7f4bf2..dcf13ff2 100755 --- a/github-action/entrypoint.sh +++ b/github-action/entrypoint.sh @@ -86,7 +86,8 @@ echo -e "${BLUE}Parameters:${NC}" "$PARAMETERS" echo "::endgroup::" handle_file_error () { - echo -e "${RED}Validation error: File not found:${NC}" "$1" + local filepath="$1" + echo -e "${RED}Validation error: File not found:${NC}" "$filepath" echo -e "skipping...\n" return 0 } From 7ea09e3b93aa8dda672622bd36e1142cc1d46446 Mon Sep 17 00:00:00 2001 From: Darshan Thakare <143271270+DarshanCode2005@users.noreply.github.com> Date: Thu, 8 Jan 2026 19:05:16 +0530 Subject: [PATCH 4/4] fix: redirect error messages to stderr --- github-action/entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github-action/entrypoint.sh b/github-action/entrypoint.sh index dcf13ff2..2cec2697 100755 --- a/github-action/entrypoint.sh +++ b/github-action/entrypoint.sh @@ -87,8 +87,8 @@ echo "::endgroup::" handle_file_error () { local filepath="$1" - echo -e "${RED}Validation error: File not found:${NC}" "$filepath" - echo -e "skipping...\n" + echo -e "${RED}Validation error: File not found:${NC}" "$filepath" >&2 + echo -e "skipping...\n" >&2 return 0 }