From a5005d88fea9f74f18a9e0d00271285e4a58f484 Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Mon, 20 Jun 2022 09:43:40 +0900 Subject: [PATCH] feat: add helpful error message when unable to enable automerge (#345) (#346) * chore(deps): update @octokit/core * feat: add helpful error message when unable to enable automerge * chore: build Co-authored-by: William Boman --- dist/index.js | 72 ++++++++++++++++++++++++++++++++------------ package-lock.json | 46 ++++++++++++++-------------- package.json | 2 +- src/github-helper.ts | 29 ++++++++++++++---- 4 files changed, 100 insertions(+), 49 deletions(-) diff --git a/dist/index.js b/dist/index.js index bf870f0..334ad4d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6,6 +6,25 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -17,7 +36,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.GithubHelper = void 0; +const core = __importStar(__nccwpck_require__(2186)); const core_1 = __nccwpck_require__(6762); +const graphql_1 = __nccwpck_require__(8467); const https_proxy_agent_1 = __nccwpck_require__(7219); const Octokit = core_1.Octokit.plugin(autoProxyAgent); // Octokit plugin to support the https_proxy environment variable @@ -56,6 +77,7 @@ class GithubHelper { }); } enablePullRequestAutomerge(pullRequestId, mergeMethod) { + var _a; return __awaiter(this, void 0, void 0, function* () { const params = { pullRequestId: pullRequestId, @@ -76,8 +98,18 @@ class GithubHelper { } } }`; - const response = yield this.octokit.graphql(query, params); - return response.enablePullRequestAutoMerge.pullRequest.autoMergeRequest; + try { + const response = yield this.octokit.graphql(query, params); + return response.enablePullRequestAutoMerge.pullRequest.autoMergeRequest; + } + catch (error) { + if (error instanceof graphql_1.GraphqlResponseError) { + if ((_a = error.errors) === null || _a === void 0 ? void 0 : _a.some(e => e.message.toLowerCase().includes('pull request is in clean status'))) { + core.error('Unable to enable automerge. Make sure you have enabled branch protection with at least one status check marked as required. See https://github.com/peter-evans/enable-pull-request-automerge#conditions for more information.'); + } + } + throw error; + } }); } } @@ -1540,7 +1572,7 @@ function _objectWithoutProperties(source, excluded) { return target; } -const VERSION = "3.5.1"; +const VERSION = "3.6.0"; const _excluded = ["authStrategy"]; class Octokit { @@ -2083,18 +2115,22 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); var request = __nccwpck_require__(6234); var universalUserAgent = __nccwpck_require__(5030); -const VERSION = "4.6.4"; +const VERSION = "4.8.0"; -class GraphqlError extends Error { - constructor(request, response) { - const message = response.data.errors[0].message; - super(message); - Object.assign(this, response.data); - Object.assign(this, { - headers: response.headers - }); - this.name = "GraphqlError"; - this.request = request; // Maintains proper stack trace (only available on V8) +function _buildMessageForResponseErrors(data) { + return `Request failed due to following response errors:\n` + data.errors.map(e => ` - ${e.message}`).join("\n"); +} + +class GraphqlResponseError extends Error { + constructor(request, headers, response) { + super(_buildMessageForResponseErrors(response)); + this.request = request; + this.headers = headers; + this.response = response; + this.name = "GraphqlResponseError"; // Expose the errors and response data in their shorthand properties. + + this.errors = response.errors; + this.data = response.data; // Maintains proper stack trace (only available on V8) /* istanbul ignore next */ @@ -2152,10 +2188,7 @@ function graphql(request, query, options) { headers[key] = response.headers[key]; } - throw new GraphqlError(requestOptions, { - headers, - data: response.data - }); + throw new GraphqlResponseError(requestOptions, headers, response.data); } return response.data.data; @@ -2189,6 +2222,7 @@ function withCustomRequest(customRequest) { }); } +exports.GraphqlResponseError = GraphqlResponseError; exports.graphql = graphql$1; exports.withCustomRequest = withCustomRequest; //# sourceMappingURL=index.js.map @@ -2294,7 +2328,7 @@ var isPlainObject = __nccwpck_require__(3287); var nodeFetch = _interopDefault(__nccwpck_require__(467)); var requestError = __nccwpck_require__(537); -const VERSION = "5.6.0"; +const VERSION = "5.6.3"; function getBufferResponse(response) { return response.arrayBuffer(); diff --git a/package-lock.json b/package-lock.json index 68a066a..b626999 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@actions/core": "^1.6.0", - "@octokit/core": "^3.5.1", + "@octokit/core": "^3.6.0", "https-proxy-agent": "^5.0.0" }, "devDependencies": { @@ -1123,13 +1123,13 @@ } }, "node_modules/@octokit/core": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/@octokit/core/-/core-3.5.1.tgz", - "integrity": "sha512-omncwpLVxMP+GLpLPgeGJBF6IWJFjXDS5flY5VbppePYX9XehevbDykRH9PdCdvqt9TS5AOTiDide7h0qrkHjw==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@octokit/core/-/core-3.6.0.tgz", + "integrity": "sha512-7RKRKuA4xTjMhY+eG3jthb3hlZCsOwg3rztWh75Xc+ShDWOfDDATWbeZpAHBNRpm4Tv9WgBMOy1zEJYXG6NJ7Q==", "dependencies": { "@octokit/auth-token": "^2.4.4", "@octokit/graphql": "^4.5.8", - "@octokit/request": "^5.6.0", + "@octokit/request": "^5.6.3", "@octokit/request-error": "^2.0.5", "@octokit/types": "^6.0.3", "before-after-hook": "^2.2.0", @@ -1147,9 +1147,9 @@ } }, "node_modules/@octokit/graphql": { - "version": "4.6.4", - "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-4.6.4.tgz", - "integrity": "sha512-SWTdXsVheRmlotWNjKzPOb6Js6tjSqA2a8z9+glDJng0Aqjzti8MEWOtuT8ZSu6wHnci7LZNuarE87+WJBG4vg==", + "version": "4.8.0", + "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-4.8.0.tgz", + "integrity": "sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==", "dependencies": { "@octokit/request": "^5.6.0", "@octokit/types": "^6.0.3", @@ -1162,15 +1162,15 @@ "integrity": "sha512-XBP03pG4XuTU+VgeJM1ozRdmZJerMG4tk6wA+raFKycC4qV9jtD2UQroAg9bAcmI3Q0zWvifeDGtPqsFjMzkLg==" }, "node_modules/@octokit/request": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/@octokit/request/-/request-5.6.0.tgz", - "integrity": "sha512-4cPp/N+NqmaGQwbh3vUsYqokQIzt7VjsgTYVXiwpUP2pxd5YiZB2XuTedbb0SPtv9XS7nzAKjAuQxmY8/aZkiA==", + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-5.6.3.tgz", + "integrity": "sha512-bFJl0I1KVc9jYTe9tdGGpAMPy32dLBXXo1dS/YwSCTL/2nd9XeHsY616RE3HPXDVk+a+dBuzyz5YdlXwcDTr2A==", "dependencies": { "@octokit/endpoint": "^6.0.1", "@octokit/request-error": "^2.1.0", "@octokit/types": "^6.16.1", "is-plain-object": "^5.0.0", - "node-fetch": "^2.6.1", + "node-fetch": "^2.6.7", "universal-user-agent": "^6.0.0" } }, @@ -7068,13 +7068,13 @@ } }, "@octokit/core": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/@octokit/core/-/core-3.5.1.tgz", - "integrity": "sha512-omncwpLVxMP+GLpLPgeGJBF6IWJFjXDS5flY5VbppePYX9XehevbDykRH9PdCdvqt9TS5AOTiDide7h0qrkHjw==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@octokit/core/-/core-3.6.0.tgz", + "integrity": "sha512-7RKRKuA4xTjMhY+eG3jthb3hlZCsOwg3rztWh75Xc+ShDWOfDDATWbeZpAHBNRpm4Tv9WgBMOy1zEJYXG6NJ7Q==", "requires": { "@octokit/auth-token": "^2.4.4", "@octokit/graphql": "^4.5.8", - "@octokit/request": "^5.6.0", + "@octokit/request": "^5.6.3", "@octokit/request-error": "^2.0.5", "@octokit/types": "^6.0.3", "before-after-hook": "^2.2.0", @@ -7092,9 +7092,9 @@ } }, "@octokit/graphql": { - "version": "4.6.4", - "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-4.6.4.tgz", - "integrity": "sha512-SWTdXsVheRmlotWNjKzPOb6Js6tjSqA2a8z9+glDJng0Aqjzti8MEWOtuT8ZSu6wHnci7LZNuarE87+WJBG4vg==", + "version": "4.8.0", + "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-4.8.0.tgz", + "integrity": "sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==", "requires": { "@octokit/request": "^5.6.0", "@octokit/types": "^6.0.3", @@ -7107,15 +7107,15 @@ "integrity": "sha512-XBP03pG4XuTU+VgeJM1ozRdmZJerMG4tk6wA+raFKycC4qV9jtD2UQroAg9bAcmI3Q0zWvifeDGtPqsFjMzkLg==" }, "@octokit/request": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/@octokit/request/-/request-5.6.0.tgz", - "integrity": "sha512-4cPp/N+NqmaGQwbh3vUsYqokQIzt7VjsgTYVXiwpUP2pxd5YiZB2XuTedbb0SPtv9XS7nzAKjAuQxmY8/aZkiA==", + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-5.6.3.tgz", + "integrity": "sha512-bFJl0I1KVc9jYTe9tdGGpAMPy32dLBXXo1dS/YwSCTL/2nd9XeHsY616RE3HPXDVk+a+dBuzyz5YdlXwcDTr2A==", "requires": { "@octokit/endpoint": "^6.0.1", "@octokit/request-error": "^2.1.0", "@octokit/types": "^6.16.1", "is-plain-object": "^5.0.0", - "node-fetch": "^2.6.1", + "node-fetch": "^2.6.7", "universal-user-agent": "^6.0.0" } }, diff --git a/package.json b/package.json index 7e225ff..e4ab818 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "homepage": "https://github.com/peter-evans/enable-pull-request-automerge", "dependencies": { "@actions/core": "^1.6.0", - "@octokit/core": "^3.5.1", + "@octokit/core": "^3.6.0", "https-proxy-agent": "^5.0.0" }, "devDependencies": { diff --git a/src/github-helper.ts b/src/github-helper.ts index 097b9e6..6f39c08 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -1,5 +1,7 @@ +import * as core from '@actions/core' import {Octokit as Core} from '@octokit/core' import * as OctokitTypes from '@octokit/types' +import {GraphqlResponseError} from '@octokit/graphql' import {HttpsProxyAgent} from 'https-proxy-agent' const Octokit = Core.plugin(autoProxyAgent) @@ -73,12 +75,27 @@ export class GithubHelper { } } }` - const response = - await this.octokit.graphql( - query, - params - ) - return response.enablePullRequestAutoMerge.pullRequest.autoMergeRequest + try { + const response = + await this.octokit.graphql( + query, + params + ) + return response.enablePullRequestAutoMerge.pullRequest.autoMergeRequest + } catch (error) { + if (error instanceof GraphqlResponseError) { + if ( + error.errors?.some(e => + e.message.toLowerCase().includes('pull request is in clean status') + ) + ) { + core.error( + 'Unable to enable automerge. Make sure you have enabled branch protection with at least one status check marked as required. See https://github.com/peter-evans/enable-pull-request-automerge#conditions for more information.' + ) + } + } + throw error + } } }