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
65 changes: 55 additions & 10 deletions actions/setup/js/create_pull_request.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -986,16 +986,61 @@ ${patchPreview}`;
// Check if the error is the specific "GitHub actions is not permitted to create or approve pull requests" error
if (errorMessage.includes("GitHub Actions is not permitted to create or approve pull requests")) {
core.error("Permission error: GitHub Actions is not permitted to create or approve pull requests");
// Set output variable for conclusion job to handle
core.setOutput(
"error_message",
"GitHub Actions is not permitted to create or approve pull requests. Please enable 'Allow GitHub Actions to create and approve pull requests' in repository settings: https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#preventing-github-actions-from-creating-or-approving-pull-requests"
);
return {
success: false,
error: errorMessage,
error_type: "permission_denied",
};

// Branch has already been pushed - create a fallback issue with a link to create the PR via GitHub UI
const githubServer = process.env.GITHUB_SERVER_URL || "https://github.com";
// Encode branch name path segments individually to preserve '/' while encoding other special characters
const encodedBase = baseBranch.split("/").map(encodeURIComponent).join("/");
const encodedHead = branchName.split("/").map(encodeURIComponent).join("/");
const createPrUrl = `${githubServer}/${repoParts.owner}/${repoParts.repo}/compare/${encodedBase}...${encodedHead}?expand=1&title=${encodeURIComponent(title)}`;
Comment on lines +992 to +995

Copilot AI Mar 3, 2026

Copy link

Choose a reason for hiding this comment

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

The compare URL path format uses ... as a delimiter (/compare/{base}...{head}), but branch names are allowed to contain . and can include the sequence .... In that case the generated URL becomes ambiguous/broken because encodeURIComponent does not escape .. Consider switching to the pulls/new?base=...&head=... URL format (which avoids the delimiter), or explicitly percent-encoding . in the encoded base/head refs so ... can only appear as the separator.

Suggested change
// Encode branch name path segments individually to preserve '/' while encoding other special characters
const encodedBase = baseBranch.split("/").map(encodeURIComponent).join("/");
const encodedHead = branchName.split("/").map(encodeURIComponent).join("/");
const createPrUrl = `${githubServer}/${repoParts.owner}/${repoParts.repo}/compare/${encodedBase}...${encodedHead}?expand=1&title=${encodeURIComponent(title)}`;
const encodedBase = encodeURIComponent(baseBranch);
const encodedHead = encodeURIComponent(branchName);
const encodedTitle = encodeURIComponent(title);
const createPrUrl = `${githubServer}/${repoParts.owner}/${repoParts.repo}/pulls/new?base=${encodedBase}&head=${encodedHead}&title=${encodedTitle}`;

Copilot uses AI. Check for mistakes.

// Read patch content for preview
let patchPreview = "";
if (patchFilePath && fs.existsSync(patchFilePath)) {
const patchContent = fs.readFileSync(patchFilePath, "utf8");
patchPreview = generatePatchPreview(patchContent);
}
Comment on lines +997 to +1002

Copilot AI Mar 3, 2026

Copy link

Choose a reason for hiding this comment

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

This block duplicates logic that already exists in the generic PR-creation fallback immediately below (reading patchFilePath for patchPreview, constructing a fallback issue body, and creating the issue). Consider extracting shared helpers for building the fallback issue body / reading the patch preview so the different fallback paths don't drift over time.

This issue also appears on line 986 of the same file.

Copilot uses AI. Check for mistakes.

const fallbackBody =
`${body}\n\n---\n\n` +
`> [!NOTE]\n` +
`> This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.\n` +
`> The changes have been pushed to branch \`${branchName}\`.\n` +
`>\n` +
`> **[Click here to create the pull request](${createPrUrl})**\n\n` +
`To fix the permissions issue, go to **Settings** → **Actions** → **General** and enable **Allow GitHub Actions to create and approve pull requests**.` +
patchPreview;

try {
const { data: issue } = await githubClient.rest.issues.create({
owner: repoParts.owner,
repo: repoParts.repo,
title: title,
body: fallbackBody,
labels: mergeFallbackIssueLabels(labels),
});

core.info(`Created fallback issue #${issue.number}: ${issue.html_url}`);

await updateActivationComment(github, context, core, issue.html_url, issue.number, "issue");

return {
success: true,
fallback_used: true,
issue_number: issue.number,
issue_url: issue.html_url,
branch_name: branchName,
repo: itemRepo,
};
} catch (issueError) {
const error = `Failed to create pull request (permission denied) and failed to create fallback issue. PR error: ${errorMessage}. Issue error: ${issueError instanceof Error ? issueError.message : String(issueError)}`;
core.error(error);
return {
success: false,
error,
error_type: "permission_denied",
};
}
}

if (!fallbackAsIssue) {
Expand Down
5 changes: 3 additions & 2 deletions actions/setup/js/create_pull_request.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("create_pull_request - fallback-as-issue configuration", () => {
const errorTypes = {
push_failed: "Used when git push operation fails and fallback-as-issue is false",
pr_creation_failed: "Used when PR creation fails (except permission errors) and fallback-as-issue is false",
permission_denied: "Used when GitHub Actions lacks permission to create/approve PRs (handled before fallback logic)",
permission_denied: "Used when GitHub Actions lacks permission to create/approve PRs AND fallback issue creation also fails",
};

// Verify the error types are documented
Expand All @@ -45,7 +45,8 @@ describe("create_pull_request - fallback-as-issue configuration", () => {
// These error types should be returned in the corresponding code paths:
// - push failure with fallback disabled: error_type: "push_failed"
// - PR creation failure with fallback disabled: error_type: "pr_creation_failed"
// - Permission error (always): error_type: "permission_denied"
// - Permission error with successful fallback issue: success=true, fallback_used=true
// - Permission error when fallback issue also fails: error_type: "permission_denied"
});
});
});
Expand Down
Loading