Skip to content

Commit 8889508

Browse files
authored
fix: GitHub API not providing details for existing PRs in private repos (#4064)
1 parent bc8a47f commit 8889508

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

dist/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13551355
step((generator = generator.apply(thisArg, _arguments || [])).next());
13561356
});
13571357
};
1358+
var __asyncValues = (this && this.__asyncValues) || function (o) {
1359+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
1360+
var m = o[Symbol.asyncIterator], i;
1361+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
1362+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
1363+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
1364+
};
13581365
var __importDefault = (this && this.__importDefault) || function (mod) {
13591366
return (mod && mod.__esModule) ? mod : { "default": mod };
13601367
};
@@ -1392,6 +1399,7 @@ class GitHubHelper {
13921399
}
13931400
createOrUpdate(inputs, baseRepository, headRepository) {
13941401
return __awaiter(this, void 0, void 0, function* () {
1402+
var _a, e_1, _b, _c;
13951403
const [headOwner] = headRepository.split('/');
13961404
const headBranch = `${headOwner}:${inputs.branch}`;
13971405
// Try to create the pull request
@@ -1424,8 +1432,37 @@ class GitHubHelper {
14241432
// Update the pull request that exists for this branch and base
14251433
core.info(`Fetching existing pull request`);
14261434
const { data: pulls } = yield this.octokit.rest.pulls.list(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { state: 'open', head: headBranch, base: inputs.base }));
1435+
let existingPullNumber = undefined;
1436+
if ((pulls === null || pulls === void 0 ? void 0 : pulls.length) === 0 || pulls === null || pulls === undefined) {
1437+
core.error(`Failed to fetch existing pull request details through API - fetching all pull requests to manually check`);
1438+
try {
1439+
for (var _d = true, _e = __asyncValues(this.octokit.paginate.iterator(this.octokit.rest.pulls.list, Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { state: 'open', base: inputs.base }))), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
1440+
_c = _f.value;
1441+
_d = false;
1442+
const response = _c;
1443+
const existingPull = response.data.find(pull => pull.head.label === headBranch);
1444+
if (existingPull !== undefined) {
1445+
existingPullNumber = existingPull.number;
1446+
break;
1447+
}
1448+
}
1449+
}
1450+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
1451+
finally {
1452+
try {
1453+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
1454+
}
1455+
finally { if (e_1) throw e_1.error; }
1456+
}
1457+
}
1458+
else {
1459+
existingPullNumber = pulls[0].number;
1460+
}
1461+
if (existingPullNumber === undefined) {
1462+
throw new Error(`A pull request already exists for ${headBranch} but couldn't acquire the pull number`);
1463+
}
14271464
core.info(`Attempting update of pull request`);
1428-
const { data: pull } = yield this.octokit.rest.pulls.update(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { pull_number: pulls[0].number, title: inputs.title, body: inputs.body }));
1465+
const { data: pull } = yield this.octokit.rest.pulls.update(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { pull_number: existingPullNumber, title: inputs.title, body: inputs.body }));
14291466
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
14301467
return {
14311468
number: pull.number,

src/github-helper.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,39 @@ export class GitHubHelper {
119119
head: headBranch,
120120
base: inputs.base
121121
})
122+
let existingPullNumber: number | undefined = undefined
123+
if (pulls?.length === 0 || pulls === null || pulls === undefined) {
124+
core.error(
125+
`Failed to fetch existing pull request details through API - fetching all pull requests to manually check`
126+
)
127+
for await (const response of this.octokit.paginate.iterator(
128+
this.octokit.rest.pulls.list,
129+
{
130+
...this.parseRepository(baseRepository),
131+
state: 'open',
132+
base: inputs.base
133+
}
134+
)) {
135+
const existingPull = response.data.find(
136+
pull => pull.head.label === headBranch
137+
)
138+
if (existingPull !== undefined) {
139+
existingPullNumber = existingPull.number
140+
break
141+
}
142+
}
143+
} else {
144+
existingPullNumber = pulls[0].number
145+
}
146+
if (existingPullNumber === undefined) {
147+
throw new Error(
148+
`A pull request already exists for ${headBranch} but couldn't acquire the pull number`
149+
)
150+
}
122151
core.info(`Attempting update of pull request`)
123152
const {data: pull} = await this.octokit.rest.pulls.update({
124153
...this.parseRepository(baseRepository),
125-
pull_number: pulls[0].number,
154+
pull_number: existingPullNumber,
126155
title: inputs.title,
127156
body: inputs.body
128157
})

0 commit comments

Comments
 (0)