@@ -63624,10 +63624,12 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
63624
63624
const core = __importStar(__nccwpck_require__(2186));
63625
63625
const github = __importStar(__nccwpck_require__(5438));
63626
63626
const lib_1 = __nccwpck_require__(6791);
63627
- const track_1 = __nccwpck_require__(1263);
63627
+ const api_1 = __nccwpck_require__(9095);
63628
+ const github_1 = __nccwpck_require__(8216);
63629
+ const track_1 = __nccwpck_require__(4154);
63628
63630
const jobID = (0, lib_1.optional)('codeball-job-id');
63629
63631
function run() {
63630
- var _a, _b, _c, _d, _e, _f, _g;
63632
+ var _a, _b, _c, _d, _e, _f, _g, _h ;
63631
63633
return __awaiter(this, void 0, void 0, function* () {
63632
63634
const pullRequestURL = (_b = (_a = github.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.html_url;
63633
63635
if (!pullRequestURL)
@@ -63649,27 +63651,54 @@ function run() {
63649
63651
const octokit = new lib_1.Octokit({ auth: githubToken });
63650
63652
const dashboardLink = `[dashboard](https://codeball.ai/${process.env.GITHUB_REPOSITORY})`;
63651
63653
const reviewMessage = `${message} ${dashboardLink}`;
63652
- yield octokit.pulls.createReview({
63654
+ const pr = yield octokit.pulls
63655
+ .get({
63656
+ owner: repoOwner,
63657
+ repo: repoName,
63658
+ pull_number: pullRequestNumber
63659
+ })
63660
+ .then(r => r.data);
63661
+ const isPrivate = pr.base.repo.private;
63662
+ const isFromFork = (_h = pr.head.repo) === null || _h === void 0 ? void 0 : _h.fork;
63663
+ const isToFork = pr.base.repo.fork;
63664
+ yield octokit.pulls
63665
+ .createReview({
63653
63666
owner: repoOwner,
63654
63667
repo: repoName,
63655
63668
pull_number: pullRequestNumber,
63656
63669
commit_id: commitId,
63657
63670
body: reviewMessage,
63658
63671
event: 'APPROVE'
63659
- });
63672
+ })
63673
+ .catch((error) => __awaiter(this, void 0, void 0, function* () {
63674
+ if (error instanceof Error &&
63675
+ error.message === 'Resource not accessible by integration') {
63676
+ // If the token is not allowed to create reviews (for example it's a pull request from a public fork),
63677
+ // we can try to approve the pull request from the backend with the app token.
63678
+ return (0, github_1.approve)({
63679
+ link: pullRequestURL,
63680
+ message: reviewMessage
63681
+ }).catch(error => {
63682
+ if (error.name === api_1.ForbiddenError.name) {
63683
+ throw new Error(!isPrivate && isFromFork && !isToFork
63684
+ ? 'Codeball Approver failed to access GitHub. Install https://github.com/apps/codeball-ai-writer to the base repository to give Codeball permission to approve Pull Requests.'
63685
+ : 'Codeball Approver failed to access GitHub. Check the "GITHUB_TOKEN Permissions" of this job and make sure that the job has WRITE permissions to Pull Requests.');
63686
+ }
63687
+ throw error;
63688
+ });
63689
+ }
63690
+ else {
63691
+ throw error;
63692
+ }
63693
+ }));
63660
63694
});
63661
63695
}
63662
63696
run()
63663
63697
.then(() => __awaiter(void 0, void 0, void 0, function* () { return yield (0, track_1.track)({ jobID, actionName: 'approver' }); }))
63664
63698
.catch((error) => __awaiter(void 0, void 0, void 0, function* () {
63665
63699
if (error instanceof Error) {
63666
63700
yield (0, track_1.track)({ jobID, actionName: 'approver', error: error.message });
63667
- if (error.message === 'Resource not accessible by integration') {
63668
- core.setFailed('Codeball Approver failed to access GitHub. Check the "GITHUB_TOKEN Permissions" of this job and make sure that the job has WRITE permissions to Pull Requests.');
63669
- }
63670
- else {
63671
- core.setFailed(error.message);
63672
- }
63701
+ core.setFailed(error.message);
63673
63702
}
63674
63703
}));
63675
63704
@@ -63761,19 +63790,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
63761
63790
return (mod && mod.__esModule) ? mod : { "default": mod };
63762
63791
};
63763
63792
Object.defineProperty(exports, "__esModule", ({ value: true }));
63764
- exports.post = exports.get = exports.NotFoundError = exports.BadRequestError = void 0;
63793
+ exports.post = exports.get = exports.NotFoundError = exports.BadRequestError = exports.ForbiddenError = void 0;
63765
63794
const node_fetch_1 = __importDefault(__nccwpck_require__(4429));
63766
63795
const BASE_URL = process.env.CODEBALL_API_HOST || 'https://api.codeball.ai';
63796
+ class ForbiddenError extends Error {
63797
+ constructor(message) {
63798
+ super(message || 'Forbidden');
63799
+ this.name = 'ForbiddenError';
63800
+ }
63801
+ }
63802
+ exports.ForbiddenError = ForbiddenError;
63767
63803
class BadRequestError extends Error {
63768
63804
constructor(message) {
63769
- super(message);
63805
+ super(message || 'Bad Request' );
63770
63806
this.name = 'BadRequestError';
63771
63807
}
63772
63808
}
63773
63809
exports.BadRequestError = BadRequestError;
63774
63810
class NotFoundError extends Error {
63775
- constructor() {
63776
- super('Not found ');
63811
+ constructor(message ) {
63812
+ super(message || 'Not Found ');
63777
63813
this.name = 'NotFoundError';
63778
63814
}
63779
63815
}
@@ -63788,16 +63824,19 @@ const handleResponse = (response) => __awaiter(void 0, void 0, void 0, function*
63788
63824
else if (response.status === 400) {
63789
63825
throw new BadRequestError(yield response.text());
63790
63826
}
63827
+ else if (response.status === 403) {
63828
+ throw new ForbiddenError(yield response.text());
63829
+ }
63791
63830
else {
63792
63831
throw new Error(yield response.text());
63793
63832
}
63794
63833
});
63795
63834
const get = (path) => __awaiter(void 0, void 0, void 0, function* () {
63796
63835
return (0, node_fetch_1.default)(new URL(path, BASE_URL).toString(), {
63797
63836
headers: {
63798
- 'User-Agent': 'github-actions',
63837
+ 'User-Agent': 'github-actions'
63799
63838
},
63800
- redirect: 'follow',
63839
+ redirect: 'follow'
63801
63840
}).then(handleResponse);
63802
63841
});
63803
63842
exports.get = get;
@@ -63809,12 +63848,73 @@ const post = (path, body) => __awaiter(void 0, void 0, void 0, function* () {
63809
63848
'User-Agent': 'github-actions',
63810
63849
'Content-Type': 'application/json'
63811
63850
},
63812
- redirect: 'follow',
63851
+ redirect: 'follow'
63813
63852
}).then(handleResponse);
63814
63853
});
63815
63854
exports.post = post;
63816
63855
63817
63856
63857
+ /***/ }),
63858
+
63859
+ /***/ 8216:
63860
+ /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
63861
+
63862
+ "use strict";
63863
+
63864
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
63865
+ if (k2 === undefined) k2 = k;
63866
+ var desc = Object.getOwnPropertyDescriptor(m, k);
63867
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
63868
+ desc = { enumerable: true, get: function() { return m[k]; } };
63869
+ }
63870
+ Object.defineProperty(o, k2, desc);
63871
+ }) : (function(o, m, k, k2) {
63872
+ if (k2 === undefined) k2 = k;
63873
+ o[k2] = m[k];
63874
+ }));
63875
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
63876
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
63877
+ };
63878
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
63879
+ __exportStar(__nccwpck_require__(7571), exports);
63880
+
63881
+
63882
+ /***/ }),
63883
+
63884
+ /***/ 7571:
63885
+ /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
63886
+
63887
+ "use strict";
63888
+
63889
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
63890
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
63891
+ return new (P || (P = Promise))(function (resolve, reject) {
63892
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
63893
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
63894
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
63895
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
63896
+ });
63897
+ };
63898
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
63899
+ exports.label = exports.approve = void 0;
63900
+ const api_1 = __nccwpck_require__(9095);
63901
+ const approve = ({ link, message }) => __awaiter(void 0, void 0, void 0, function* () {
63902
+ const body = message ? { link, message } : { link };
63903
+ return (0, api_1.post)('/github/pulls/approve', body);
63904
+ });
63905
+ exports.approve = approve;
63906
+ const label = (params) => __awaiter(void 0, void 0, void 0, function* () {
63907
+ const body = Object.entries(params)
63908
+ .filter(([_, value]) => value)
63909
+ .reduce((acc, [key, value]) => {
63910
+ acc[key] = value;
63911
+ return acc;
63912
+ }, {});
63913
+ return (0, api_1.post)('/github/pulls/label', body);
63914
+ });
63915
+ exports.label = label;
63916
+
63917
+
63818
63918
/***/ }),
63819
63919
63820
63920
/***/ 6791:
@@ -63840,6 +63940,8 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
63840
63940
__exportStar(__nccwpck_require__(7527), exports);
63841
63941
__exportStar(__nccwpck_require__(6518), exports);
63842
63942
__exportStar(__nccwpck_require__(3769), exports);
63943
+ __exportStar(__nccwpck_require__(4154), exports);
63944
+ __exportStar(__nccwpck_require__(8216), exports);
63843
63945
63844
63946
63845
63947
/***/ }),
@@ -63941,7 +64043,7 @@ function getApiBaseUrl() {
63941
64043
63942
64044
/***/ }),
63943
64045
63944
- /***/ 1263 :
64046
+ /***/ 4154 :
63945
64047
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
63946
64048
63947
64049
"use strict";
@@ -63959,7 +64061,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
63959
64061
exports.track = void 0;
63960
64062
const api_1 = __nccwpck_require__(9095);
63961
64063
const track = ({ jobID, actionName, error }) => __awaiter(void 0, void 0, void 0, function* () {
63962
- return (0, api_1.post)(" /track" , {
64064
+ return (0, api_1.post)(' /track' , {
63963
64065
job_id: jobID !== null && jobID !== void 0 ? jobID : null,
63964
64066
name: actionName,
63965
64067
error: error !== null && error !== void 0 ? error : null
0 commit comments