Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
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);
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); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand Down Expand Up @@ -1345,6 +1352,7 @@ class GitHubHelper {
}
createOrUpdate(inputs, baseRepository, headRepository) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
const [headOwner] = headRepository.split('/');
const headBranch = `${headOwner}:${inputs.branch}`;
// Try to create the pull request
Expand Down Expand Up @@ -1377,8 +1385,37 @@ class GitHubHelper {
// Update the pull request that exists for this branch and base
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 }));
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`);
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 }));
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 }));
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
return {
number: pull.number,
Expand Down
31 changes: 30 additions & 1 deletion src/github-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,39 @@ export class GitHubHelper {
head: headBranch,
base: inputs.base
})
let existingPullNumber: number | undefined = undefined
if (pulls?.length === 0 || pulls === null || pulls === undefined) {
core.error(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used error here just so I could find it easier during testing, but should I change this to info or warning instead?

`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`)
const {data: pull} = await this.octokit.rest.pulls.update({
...this.parseRepository(baseRepository),
pull_number: pulls[0].number,
pull_number: existingPullNumber,
title: inputs.title,
body: inputs.body
})
Expand Down