diff --git a/github-action/entrypoint.sh b/github-action/entrypoint.sh index aba399cb..2cec2697 100755 --- a/github-action/entrypoint.sh +++ b/github-action/entrypoint.sh @@ -86,8 +86,10 @@ echo -e "${BLUE}Parameters:${NC}" "$PARAMETERS" echo "::endgroup::" handle_file_error () { - echo -e "${RED}Validation error: File not found:${NC}" "$1" - echo -e "skipping...\n" + local filepath="$1" + echo -e "${RED}Validation error: File not found:${NC}" "$filepath" >&2 + echo -e "skipping...\n" >&2 + return 0 } handle_validate () { @@ -102,6 +104,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 +119,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 +150,7 @@ handle_generate () { exit 1 fi echo "::endgroup::" + return 0 } handle_convert () { @@ -172,6 +177,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 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(); }, });