Skip to content
Merged
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
67 changes: 59 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,14 @@ function createPullRequest(inputs) {
if (branchRemoteName == 'origin' && base == inputs.branch) {
throw new Error(`The 'base' and 'branch' for a pull request must be different branches. Unable to continue.`);
}
// For self-hosted runners the repository state persists between runs.
// This command prunes the stale remote ref when the pull request branch was
// deleted after being merged or closed. Without this the push using
// '--force-with-lease' fails due to "stale info."
// https://github.com/peter-evans/create-pull-request/issues/633
yield git.exec(['remote', 'prune', branchRemoteName]);
if (utils.isSelfHosted()) {
// For self-hosted runners the repository state persists between runs.
// This command prunes the stale remote ref when the pull request branch was
// deleted after being merged or closed. Without this the push using
// '--force-with-lease' fails due to "stale info."
// https://github.com/peter-evans/create-pull-request/issues/633
yield git.exec(['remote', 'prune', branchRemoteName]);
}
core.endGroup();
// Apply the branch suffix if set
if (inputs.branchSuffix) {
Expand Down Expand Up @@ -1355,6 +1357,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 @@ -1390,6 +1399,43 @@ class GitHubHelper {
repo: repo
};
}
getPullNumber(baseRepository, headBranch, baseBranch) {
return __awaiter(this, void 0, void 0, function* () {
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('/');
Expand Down Expand Up @@ -1423,9 +1469,9 @@ 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 }));
const pullNumber = yield this.getPullNumber(baseRepository, headBranch, inputs.base);
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: pullNumber, title: inputs.title, body: inputs.body }));
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
return {
number: pull.number,
Expand Down Expand Up @@ -1844,6 +1890,7 @@ var __importStar = (this && this.__importStar) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isSelfHosted = void 0;
exports.getInputAsArray = getInputAsArray;
exports.getStringAsArray = getStringAsArray;
exports.stripOrgPrefixFromTeams = stripOrgPrefixFromTeams;
Expand Down Expand Up @@ -1952,6 +1999,10 @@ function getErrorMessage(error) {
return error.message;
return String(error);
}
const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
(process.env['AGENT_ISSELFHOSTED'] === '1' ||
process.env['AGENT_ISSELFHOSTED'] === undefined);
exports.isSelfHosted = isSelfHosted;


/***/ }),
Expand Down
14 changes: 8 additions & 6 deletions src/create-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
`The 'base' and 'branch' for a pull request must be different branches. Unable to continue.`
)
}
// For self-hosted runners the repository state persists between runs.
// This command prunes the stale remote ref when the pull request branch was
// deleted after being merged or closed. Without this the push using
// '--force-with-lease' fails due to "stale info."
// https://github.com/peter-evans/create-pull-request/issues/633
await git.exec(['remote', 'prune', branchRemoteName])
if (utils.isSelfHosted()) {
// For self-hosted runners the repository state persists between runs.
// This command prunes the stale remote ref when the pull request branch was
// deleted after being merged or closed. Without this the push using
// '--force-with-lease' fails due to "stale info."
// https://github.com/peter-evans/create-pull-request/issues/633
await git.exec(['remote', 'prune', branchRemoteName])
}
core.endGroup()

// Apply the branch suffix if set
Expand Down
57 changes: 50 additions & 7 deletions src/github-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
inputs: Inputs,
baseRepository: string,
Expand Down Expand Up @@ -113,16 +157,15 @@ export class GitHubHelper {

// Update the pull request that exists for this branch and base
core.info(`Fetching existing pull request`)
const {data: pulls} = await this.octokit.rest.pulls.list({
...this.parseRepository(baseRepository),
state: 'open',
head: headBranch,
base: inputs.base
})
const pullNumber = await this.getPullNumber(
baseRepository,
headBranch,
inputs.base
)
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: pullNumber,
title: inputs.title,
body: inputs.body
})
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ export function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
}

export const isSelfHosted = (): boolean =>
process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
(process.env['AGENT_ISSELFHOSTED'] === '1' ||
process.env['AGENT_ISSELFHOSTED'] === undefined)
Loading