Repository navigation
create_pull_request: create fallback issue with one-click PR link on permission error #19466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)}`; | ||
|
|
||
| // 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
|
||
|
|
||
| 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) { | ||
|
|
||
There was a problem hiding this comment.
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 becauseencodeURIComponentdoes not escape.. Consider switching to thepulls/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.