diff --git a/package-lock.json b/package-lock.json index 1b642f4b..27f68291 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kie/build-chain-action", - "version": "3.0.13", + "version": "3.0.14", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@kie/build-chain-action", - "version": "3.0.13", + "version": "3.0.14", "license": "ISC", "dependencies": { "@actions/artifact": "^1.1.0", @@ -14,6 +14,7 @@ "@actions/exec": "^1.1.1", "@actions/glob": "^0.3.0", "@kie/build-chain-configuration-reader": "^3.0.10", + "@octokit/request-error": "^3.0.3", "@octokit/rest": "^18.12.0", "@octokit/types": "^6.39.0", "commander": "^9.3.0", @@ -1475,20 +1476,6 @@ "node": ">= 14" } }, - "node_modules/@octokit/core/node_modules/@octokit/request-error": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-3.0.2.tgz", - "integrity": "sha512-WMNOFYrSaX8zXWoJg9u/pKgWPo94JXilMLb2VManNOby9EZxrQaBe/QSC4a1TzpAlpxofg2X/jMnCyZgL6y7eg==", - "peer": true, - "dependencies": { - "@octokit/types": "^8.0.0", - "deprecation": "^2.0.0", - "once": "^1.4.0" - }, - "engines": { - "node": ">= 14" - } - }, "node_modules/@octokit/core/node_modules/@octokit/types": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/@octokit/types/-/types-8.0.0.tgz", @@ -1570,6 +1557,32 @@ } }, "node_modules/@octokit/request-error": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-3.0.3.tgz", + "integrity": "sha512-crqw3V5Iy2uOU5Np+8M/YexTlT8zxCfI+qu+LxUB7SZpje4Qmx3mub5DfEKSO8Ylyk0aogi6TYdf6kxzh2BguQ==", + "dependencies": { + "@octokit/types": "^9.0.0", + "deprecation": "^2.0.0", + "once": "^1.4.0" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/@octokit/request-error/node_modules/@octokit/openapi-types": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-16.0.0.tgz", + "integrity": "sha512-JbFWOqTJVLHZSUUoF4FzAZKYtqdxWu9Z5m2QQnOyEa04fOFljvyh7D3GYKbfuaSWisqehImiVIMG4eyJeP5VEA==" + }, + "node_modules/@octokit/request-error/node_modules/@octokit/types": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-9.0.0.tgz", + "integrity": "sha512-LUewfj94xCMH2rbD5YJ+6AQ4AVjFYTgpp6rboWM5T7N3IsIF65SBEOVcYMGAEzO/kKNiNaW4LoWtoThOhH06gw==", + "dependencies": { + "@octokit/openapi-types": "^16.0.0" + } + }, + "node_modules/@octokit/request/node_modules/@octokit/request-error": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", "integrity": "sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==", @@ -1616,6 +1629,16 @@ "@octokit/core": ">=3" } }, + "node_modules/@octokit/rest/node_modules/@octokit/request-error": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", + "integrity": "sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==", + "dependencies": { + "@octokit/types": "^6.0.3", + "deprecation": "^2.0.0", + "once": "^1.4.0" + } + }, "node_modules/@octokit/types": { "version": "6.39.0", "resolved": "https://registry.npmjs.org/@octokit/types/-/types-6.39.0.tgz", @@ -8721,17 +8744,6 @@ "universal-user-agent": "^6.0.0" } }, - "@octokit/request-error": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-3.0.2.tgz", - "integrity": "sha512-WMNOFYrSaX8zXWoJg9u/pKgWPo94JXilMLb2VManNOby9EZxrQaBe/QSC4a1TzpAlpxofg2X/jMnCyZgL6y7eg==", - "peer": true, - "requires": { - "@octokit/types": "^8.0.0", - "deprecation": "^2.0.0", - "once": "^1.4.0" - } - }, "@octokit/types": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/@octokit/types/-/types-8.0.0.tgz", @@ -8809,16 +8821,43 @@ "is-plain-object": "^5.0.0", "node-fetch": "^2.6.7", "universal-user-agent": "^6.0.0" + }, + "dependencies": { + "@octokit/request-error": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", + "integrity": "sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==", + "requires": { + "@octokit/types": "^6.0.3", + "deprecation": "^2.0.0", + "once": "^1.4.0" + } + } } }, "@octokit/request-error": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", - "integrity": "sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-3.0.3.tgz", + "integrity": "sha512-crqw3V5Iy2uOU5Np+8M/YexTlT8zxCfI+qu+LxUB7SZpje4Qmx3mub5DfEKSO8Ylyk0aogi6TYdf6kxzh2BguQ==", "requires": { - "@octokit/types": "^6.0.3", + "@octokit/types": "^9.0.0", "deprecation": "^2.0.0", "once": "^1.4.0" + }, + "dependencies": { + "@octokit/openapi-types": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-16.0.0.tgz", + "integrity": "sha512-JbFWOqTJVLHZSUUoF4FzAZKYtqdxWu9Z5m2QQnOyEa04fOFljvyh7D3GYKbfuaSWisqehImiVIMG4eyJeP5VEA==" + }, + "@octokit/types": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-9.0.0.tgz", + "integrity": "sha512-LUewfj94xCMH2rbD5YJ+6AQ4AVjFYTgpp6rboWM5T7N3IsIF65SBEOVcYMGAEzO/kKNiNaW4LoWtoThOhH06gw==", + "requires": { + "@octokit/openapi-types": "^16.0.0" + } + } } }, "@octokit/rest": { @@ -8854,6 +8893,16 @@ "@octokit/types": "^6.39.0", "deprecation": "^2.3.1" } + }, + "@octokit/request-error": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", + "integrity": "sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==", + "requires": { + "@octokit/types": "^6.0.3", + "deprecation": "^2.0.0", + "once": "^1.4.0" + } } } }, diff --git a/package.json b/package.json index 9203192a..0d7ca8f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kie/build-chain-action", - "version": "3.0.13", + "version": "3.0.14", "description": "Library to execute commands based on github projects dependencies.", "main": "dist/index.js", "author": "", @@ -57,6 +57,7 @@ "@actions/core": "^1.8.2", "@actions/exec": "^1.1.1", "@actions/glob": "^0.3.0", + "@octokit/request-error": "^3.0.3", "@kie/build-chain-configuration-reader": "^3.0.10", "@octokit/rest": "^18.12.0", "@octokit/types": "^6.39.0", diff --git a/src/service/git/github-api.ts b/src/service/git/github-api.ts index 1e4b751e..96177722 100644 --- a/src/service/git/github-api.ts +++ b/src/service/git/github-api.ts @@ -5,6 +5,7 @@ import { LoggerService } from "@bc/service/logger/logger-service"; import { logAndThrow } from "@bc/utils/log"; import { Octokit } from "@octokit/rest"; import { Endpoints } from "@octokit/types"; +import { RequestError } from "@octokit/request-error"; import Container, { Service } from "typedi"; @Service() @@ -29,7 +30,12 @@ export class GithubAPIService { await this.octokit.repos.getBranch({ owner, repo, branch }); return true; } catch (err) { - this.logger.warn(`project github.com/${owner}/${repo}:${branch} does not exist. It's not necessarily an error.`); + this.logger.warn( + this.getErrorMessage( + err, + `project github.com/${owner}/${repo}:${branch} does not exist. It's not necessarily an error.` + ) + ); return false; } } @@ -64,7 +70,8 @@ export class GithubAPIService { if (head) { msg += `&head=${head}`; } - this.logger.error(msg); + + this.logger.error(this.getErrorMessage(err, msg)); throw err; } } @@ -106,8 +113,32 @@ export class GithubAPIService { throw new NotFoundError(); } } catch (err) { - this.logger.error(`Error getting fork name for ${targetOwner}/${repo} where owner is ${sourceOwner}`); + this.logger.error( + this.getErrorMessage( + err, + `Error getting fork name for ${targetOwner}/${repo} where owner is ${sourceOwner}` + ) + ); throw err; } } + + private getErrorMessage(err: unknown, msg: string): string { + let reason; + if (err instanceof RequestError) { + switch (err.status) { + case 401: + reason = "Failed to authenticate with provided token, please use -token argument to provide a new one. You can also check your GITHUB_TOKEN environment variable and check whether the provided token is still valid."; + break; + case 404: + reason = "Failed to fetch GitHub URL, please check if the URL used in -url argument is valid and if the token you are using have permissions to access it."; + break; + case 403: + reason = "Failed to fetch resource. Either your github token does not have access to the requested resource or you have reached your github api rate limit."; + break; + default: // let reason be undefined for all other codes + } + } + return reason ? `${msg} Reason: ${reason}` : msg; + } }