Skip to content

Commit d4f3be6

Browse files
peter-evansobnyis
andauthored
fix: provider list pulls fallback for multi fork same owner (#4245)
* fix: GitHub API not providing details for existing PRs in private repos (#4064) * fix: extract fallback into func getPullNumber --------- Co-authored-by: Noah Miller <[email protected]>
1 parent bc8a47f commit d4f3be6

File tree

2 files changed

+96
-9
lines changed

2 files changed

+96
-9
lines changed

dist/index.js

Lines changed: 46 additions & 2 deletions
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
};
@@ -1390,6 +1397,43 @@ class GitHubHelper {
13901397
repo: repo
13911398
};
13921399
}
1400+
getPullNumber(baseRepository, headBranch, baseBranch) {
1401+
return __awaiter(this, void 0, void 0, function* () {
1402+
var _a, e_1, _b, _c;
1403+
const { data: pulls } = yield this.octokit.rest.pulls.list(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { state: 'open', head: headBranch, base: baseBranch }));
1404+
let pullNumber = undefined;
1405+
if ((pulls === null || pulls === void 0 ? void 0 : pulls.length) === 0 || pulls === null || pulls === undefined) {
1406+
// This is a fallback due to a bug that affects the list endpoint when called on forks with the same owner as the repository parent.
1407+
core.info(`Pull request not found via list endpoint; attempting fallback mechanism`);
1408+
try {
1409+
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: baseBranch }))), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
1410+
_c = _f.value;
1411+
_d = false;
1412+
const response = _c;
1413+
const existingPull = response.data.find(pull => pull.head.label === headBranch);
1414+
if (existingPull !== undefined) {
1415+
pullNumber = existingPull.number;
1416+
break;
1417+
}
1418+
}
1419+
}
1420+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
1421+
finally {
1422+
try {
1423+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
1424+
}
1425+
finally { if (e_1) throw e_1.error; }
1426+
}
1427+
}
1428+
else {
1429+
pullNumber = pulls[0].number;
1430+
}
1431+
if (pullNumber === undefined) {
1432+
throw new Error(`Failed to find pull request number for branch ${headBranch}`);
1433+
}
1434+
return pullNumber;
1435+
});
1436+
}
13931437
createOrUpdate(inputs, baseRepository, headRepository) {
13941438
return __awaiter(this, void 0, void 0, function* () {
13951439
const [headOwner] = headRepository.split('/');
@@ -1423,9 +1467,9 @@ class GitHubHelper {
14231467
}
14241468
// Update the pull request that exists for this branch and base
14251469
core.info(`Fetching existing pull request`);
1426-
const { data: pulls } = yield this.octokit.rest.pulls.list(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { state: 'open', head: headBranch, base: inputs.base }));
1470+
const pullNumber = yield this.getPullNumber(baseRepository, headBranch, inputs.base);
14271471
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 }));
1472+
const { data: pull } = yield this.octokit.rest.pulls.update(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { pull_number: pullNumber, title: inputs.title, body: inputs.body }));
14291473
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
14301474
return {
14311475
number: pull.number,

src/github-helper.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,50 @@ export class GitHubHelper {
6363
}
6464
}
6565

66+
private async getPullNumber(
67+
baseRepository: string,
68+
headBranch: string,
69+
baseBranch: string
70+
): Promise<number> {
71+
const {data: pulls} = await this.octokit.rest.pulls.list({
72+
...this.parseRepository(baseRepository),
73+
state: 'open',
74+
head: headBranch,
75+
base: baseBranch
76+
})
77+
let pullNumber: number | undefined = undefined
78+
if (pulls?.length === 0 || pulls === null || pulls === undefined) {
79+
// This is a fallback due to a bug that affects the list endpoint when called on forks with the same owner as the repository parent.
80+
core.info(
81+
`Pull request not found via list endpoint; attempting fallback mechanism`
82+
)
83+
for await (const response of this.octokit.paginate.iterator(
84+
this.octokit.rest.pulls.list,
85+
{
86+
...this.parseRepository(baseRepository),
87+
state: 'open',
88+
base: baseBranch
89+
}
90+
)) {
91+
const existingPull = response.data.find(
92+
pull => pull.head.label === headBranch
93+
)
94+
if (existingPull !== undefined) {
95+
pullNumber = existingPull.number
96+
break
97+
}
98+
}
99+
} else {
100+
pullNumber = pulls[0].number
101+
}
102+
if (pullNumber === undefined) {
103+
throw new Error(
104+
`Failed to find pull request number for branch ${headBranch}`
105+
)
106+
}
107+
return pullNumber
108+
}
109+
66110
private async createOrUpdate(
67111
inputs: Inputs,
68112
baseRepository: string,
@@ -113,16 +157,15 @@ export class GitHubHelper {
113157

114158
// Update the pull request that exists for this branch and base
115159
core.info(`Fetching existing pull request`)
116-
const {data: pulls} = await this.octokit.rest.pulls.list({
117-
...this.parseRepository(baseRepository),
118-
state: 'open',
119-
head: headBranch,
120-
base: inputs.base
121-
})
160+
const pullNumber = await this.getPullNumber(
161+
baseRepository,
162+
headBranch,
163+
inputs.base
164+
)
122165
core.info(`Attempting update of pull request`)
123166
const {data: pull} = await this.octokit.rest.pulls.update({
124167
...this.parseRepository(baseRepository),
125-
pull_number: pulls[0].number,
168+
pull_number: pullNumber,
126169
title: inputs.title,
127170
body: inputs.body
128171
})

0 commit comments

Comments
 (0)