fix: extract fallback into func getPullNumber
This commit is contained in:
parent
8889508eab
commit
b617f67221
71
dist/index.js
vendored
71
dist/index.js
vendored
@ -1397,9 +1397,45 @@ class GitHubHelper {
|
|||||||
repo: repo
|
repo: repo
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
createOrUpdate(inputs, baseRepository, headRepository) {
|
getPullNumber(baseRepository, headBranch, baseBranch) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var _a, e_1, _b, _c;
|
var _a, e_1, _b, _c;
|
||||||
|
const { data: pulls } = yield this.octokit.rest.pulls.list(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { state: 'open', head: headBranch, base: baseBranch }));
|
||||||
|
let pullNumber = undefined;
|
||||||
|
if ((pulls === null || pulls === void 0 ? void 0 : pulls.length) === 0 || pulls === null || pulls === undefined) {
|
||||||
|
// 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.
|
||||||
|
core.info(`Pull request not found via list endpoint; attempting fallback mechanism`);
|
||||||
|
try {
|
||||||
|
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) {
|
||||||
|
_c = _f.value;
|
||||||
|
_d = false;
|
||||||
|
const response = _c;
|
||||||
|
const existingPull = response.data.find(pull => pull.head.label === headBranch);
|
||||||
|
if (existingPull !== undefined) {
|
||||||
|
pullNumber = existingPull.number;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
||||||
|
}
|
||||||
|
finally { if (e_1) throw e_1.error; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pullNumber = pulls[0].number;
|
||||||
|
}
|
||||||
|
if (pullNumber === undefined) {
|
||||||
|
throw new Error(`Failed to find pull request number for branch ${headBranch}`);
|
||||||
|
}
|
||||||
|
return pullNumber;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
createOrUpdate(inputs, baseRepository, headRepository) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const [headOwner] = headRepository.split('/');
|
const [headOwner] = headRepository.split('/');
|
||||||
const headBranch = `${headOwner}:${inputs.branch}`;
|
const headBranch = `${headOwner}:${inputs.branch}`;
|
||||||
// Try to create the pull request
|
// Try to create the pull request
|
||||||
@ -1431,38 +1467,9 @@ class GitHubHelper {
|
|||||||
}
|
}
|
||||||
// Update the pull request that exists for this branch and base
|
// Update the pull request that exists for this branch and base
|
||||||
core.info(`Fetching existing pull request`);
|
core.info(`Fetching existing pull request`);
|
||||||
const { data: pulls } = yield this.octokit.rest.pulls.list(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { state: 'open', head: headBranch, base: inputs.base }));
|
const pullNumber = yield this.getPullNumber(baseRepository, headBranch, inputs.base);
|
||||||
let existingPullNumber = undefined;
|
|
||||||
if ((pulls === null || pulls === void 0 ? void 0 : pulls.length) === 0 || pulls === null || pulls === undefined) {
|
|
||||||
core.error(`Failed to fetch existing pull request details through API - fetching all pull requests to manually check`);
|
|
||||||
try {
|
|
||||||
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) {
|
|
||||||
_c = _f.value;
|
|
||||||
_d = false;
|
|
||||||
const response = _c;
|
|
||||||
const existingPull = response.data.find(pull => pull.head.label === headBranch);
|
|
||||||
if (existingPull !== undefined) {
|
|
||||||
existingPullNumber = existingPull.number;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
||||||
finally {
|
|
||||||
try {
|
|
||||||
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
||||||
}
|
|
||||||
finally { if (e_1) throw e_1.error; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
existingPullNumber = pulls[0].number;
|
|
||||||
}
|
|
||||||
if (existingPullNumber === undefined) {
|
|
||||||
throw new Error(`A pull request already exists for ${headBranch} but couldn't acquire the pull number`);
|
|
||||||
}
|
|
||||||
core.info(`Attempting update of pull request`);
|
core.info(`Attempting update of pull request`);
|
||||||
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 }));
|
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 }));
|
||||||
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
|
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
|
||||||
return {
|
return {
|
||||||
number: pull.number,
|
number: pull.number,
|
||||||
|
|||||||
@ -63,6 +63,50 @@ export class GitHubHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getPullNumber(
|
||||||
|
baseRepository: string,
|
||||||
|
headBranch: string,
|
||||||
|
baseBranch: string
|
||||||
|
): Promise<number> {
|
||||||
|
const {data: pulls} = await this.octokit.rest.pulls.list({
|
||||||
|
...this.parseRepository(baseRepository),
|
||||||
|
state: 'open',
|
||||||
|
head: headBranch,
|
||||||
|
base: baseBranch
|
||||||
|
})
|
||||||
|
let pullNumber: number | undefined = undefined
|
||||||
|
if (pulls?.length === 0 || pulls === null || pulls === undefined) {
|
||||||
|
// 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.
|
||||||
|
core.info(
|
||||||
|
`Pull request not found via list endpoint; attempting fallback mechanism`
|
||||||
|
)
|
||||||
|
for await (const response of this.octokit.paginate.iterator(
|
||||||
|
this.octokit.rest.pulls.list,
|
||||||
|
{
|
||||||
|
...this.parseRepository(baseRepository),
|
||||||
|
state: 'open',
|
||||||
|
base: baseBranch
|
||||||
|
}
|
||||||
|
)) {
|
||||||
|
const existingPull = response.data.find(
|
||||||
|
pull => pull.head.label === headBranch
|
||||||
|
)
|
||||||
|
if (existingPull !== undefined) {
|
||||||
|
pullNumber = existingPull.number
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pullNumber = pulls[0].number
|
||||||
|
}
|
||||||
|
if (pullNumber === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to find pull request number for branch ${headBranch}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return pullNumber
|
||||||
|
}
|
||||||
|
|
||||||
private async createOrUpdate(
|
private async createOrUpdate(
|
||||||
inputs: Inputs,
|
inputs: Inputs,
|
||||||
baseRepository: string,
|
baseRepository: string,
|
||||||
@ -113,45 +157,15 @@ export class GitHubHelper {
|
|||||||
|
|
||||||
// Update the pull request that exists for this branch and base
|
// Update the pull request that exists for this branch and base
|
||||||
core.info(`Fetching existing pull request`)
|
core.info(`Fetching existing pull request`)
|
||||||
const {data: pulls} = await this.octokit.rest.pulls.list({
|
const pullNumber = await this.getPullNumber(
|
||||||
...this.parseRepository(baseRepository),
|
baseRepository,
|
||||||
state: 'open',
|
headBranch,
|
||||||
head: headBranch,
|
inputs.base
|
||||||
base: inputs.base
|
)
|
||||||
})
|
|
||||||
let existingPullNumber: number | undefined = undefined
|
|
||||||
if (pulls?.length === 0 || pulls === null || pulls === undefined) {
|
|
||||||
core.error(
|
|
||||||
`Failed to fetch existing pull request details through API - fetching all pull requests to manually check`
|
|
||||||
)
|
|
||||||
for await (const response of this.octokit.paginate.iterator(
|
|
||||||
this.octokit.rest.pulls.list,
|
|
||||||
{
|
|
||||||
...this.parseRepository(baseRepository),
|
|
||||||
state: 'open',
|
|
||||||
base: inputs.base
|
|
||||||
}
|
|
||||||
)) {
|
|
||||||
const existingPull = response.data.find(
|
|
||||||
pull => pull.head.label === headBranch
|
|
||||||
)
|
|
||||||
if (existingPull !== undefined) {
|
|
||||||
existingPullNumber = existingPull.number
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
existingPullNumber = pulls[0].number
|
|
||||||
}
|
|
||||||
if (existingPullNumber === undefined) {
|
|
||||||
throw new Error(
|
|
||||||
`A pull request already exists for ${headBranch} but couldn't acquire the pull number`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
core.info(`Attempting update of pull request`)
|
core.info(`Attempting update of pull request`)
|
||||||
const {data: pull} = await this.octokit.rest.pulls.update({
|
const {data: pull} = await this.octokit.rest.pulls.update({
|
||||||
...this.parseRepository(baseRepository),
|
...this.parseRepository(baseRepository),
|
||||||
pull_number: existingPullNumber,
|
pull_number: pullNumber,
|
||||||
title: inputs.title,
|
title: inputs.title,
|
||||||
body: inputs.body
|
body: inputs.body
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user