-
Notifications
You must be signed in to change notification settings - Fork 170
feat: add pull request progress comments #177
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import * as core from "@actions/core"; | ||
| import { Octokit } from "@octokit/rest"; | ||
| import { readFile } from "node:fs/promises"; | ||
|
|
||
| export type ProgressStatus = "running" | "completed" | "failed"; | ||
|
|
||
| const MARKER = "<!-- codex-action-progress -->"; | ||
|
|
||
| export async function updatePullRequestProgress({ | ||
| status, | ||
| token = process.env.GITHUB_TOKEN ?? "", | ||
| repository = process.env.GITHUB_REPOSITORY ?? "", | ||
| eventPath = process.env.GITHUB_EVENT_PATH ?? "", | ||
| octokit, | ||
| }: { | ||
| status: ProgressStatus; | ||
| token?: string; | ||
| repository?: string; | ||
| eventPath?: string; | ||
| octokit?: Octokit; | ||
| }): Promise<void> { | ||
| if (!eventPath) { | ||
| core.info("Skipping progress comment: this is not a pull request event."); | ||
| return; | ||
| } | ||
| if (!repository.includes("/")) { | ||
| throw new Error("GITHUB_REPOSITORY must be in the format owner/repo."); | ||
| } | ||
| if (!token) { | ||
| throw new Error("A GitHub token is required to post a progress comment."); | ||
| } | ||
|
|
||
| const event = JSON.parse(await readFile(eventPath, "utf8")) as { | ||
| pull_request?: { number?: number }; | ||
| }; | ||
| const issueNumber = event.pull_request?.number; | ||
| if (!issueNumber) { | ||
| core.info("Skipping progress comment: this is not a pull request event."); | ||
| return; | ||
| } | ||
|
|
||
| const [owner, repo] = repository.split("/", 2); | ||
| const client = octokit ?? new Octokit({ auth: token }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When GitHub returns a transient 5xx response or closes a connection during comment listing, creation, or update, this plain Octokit client does not apply the retry plugin already used by Useful? React with 👍 / 👎. |
||
| const body = `${MARKER}\n${progressMessage(status)}`; | ||
| const comments = await client.paginate(client.issues.listComments, { | ||
| owner, | ||
| repo, | ||
| issue_number: issueNumber, | ||
| per_page: 100, | ||
| }); | ||
| const existing = comments.find( | ||
| (comment) => | ||
| comment.user?.login === "github-actions[bot]" && | ||
| comment.body?.includes(MARKER) | ||
|
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When two action runs overlap for the same pull request—for example, a second commit arrives while the first Codex run is active—both runs select the same bot-authored marker comment. The older run can therefore write Useful? React with 👍 / 👎. |
||
| ); | ||
|
|
||
| if (existing) { | ||
| await client.issues.updateComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: existing.id, | ||
| body, | ||
| }); | ||
| } else { | ||
| await client.issues.createComment({ owner, repo, issue_number: issueNumber, body }); | ||
| } | ||
| } | ||
|
|
||
| function progressMessage(status: ProgressStatus): string { | ||
| switch (status) { | ||
| case "running": | ||
| return "🤖 Codex is working on this pull request."; | ||
| case "completed": | ||
| return "✅ Codex completed its work on this pull request."; | ||
| case "failed": | ||
| return "❌ Codex did not complete successfully on this pull request."; | ||
| } | ||
| } | ||
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.
On GitHub Enterprise Server, this client defaults to
https://api.github.cominstead of the server URL provided throughGITHUB_API_URL. The repository's existing permissions client explicitly forwards that environment variable for the same reason, but an opted-in progress step will instead send the enterprise token to GitHub.com and fail before Codex runs. Construct this client with the configuredbaseUrlas well.Useful? React with 👍 / 👎.