-
Notifications
You must be signed in to change notification settings - Fork 14
fix(agent): only attach PR URLs from gh pr create #1964
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
Open
adboio
wants to merge
1
commit into
main
Choose a base branch
from
adam/gate-pr-url-detection-on-gh-pr-create
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { extractCreatedPrUrl } from "./pr-url-detector"; | ||
|
|
||
| describe("extractCreatedPrUrl", () => { | ||
| const PR_URL = "https://github.com/PostHog/posthog/pull/12345"; | ||
|
|
||
| it("returns the URL when gh pr create produced it (string toolResponse)", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: 'gh pr create --title "x" --body "y"', | ||
| toolResponse: `${PR_URL}\n`, | ||
| }), | ||
| ).toBe(PR_URL); | ||
| }); | ||
|
|
||
| it("returns the URL when gh pr create produced it (object toolResponse)", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "gh pr create --fill", | ||
| toolResponse: { stdout: `${PR_URL}\n`, stderr: "" }, | ||
| }), | ||
| ).toBe(PR_URL); | ||
| }); | ||
|
|
||
| it("ignores PR URLs from gh pr view", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: `gh pr view ${PR_URL}`, | ||
| toolResponse: { stdout: PR_URL }, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("ignores PR URLs from gh search prs", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: 'gh search prs "fix login"', | ||
| toolResponse: PR_URL, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("ignores PR URLs from gh pr list", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "gh pr list --json url", | ||
| toolResponse: PR_URL, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when bashCommand is missing", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: undefined, | ||
| toolResponse: PR_URL, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for non-Bash tools", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Edit", | ||
| bashCommand: "gh pr create", | ||
| toolResponse: PR_URL, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("accepts the lowercase 'bash' tool variant", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "bash", | ||
| bashCommand: "gh pr create", | ||
| toolResponse: PR_URL, | ||
| }), | ||
| ).toBe(PR_URL); | ||
| }); | ||
|
|
||
| it("returns null when output has no PR URL", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "gh pr create", | ||
| toolResponse: "no pr was created", | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("finds the URL in the content array when toolResponse is empty", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "gh pr create --fill", | ||
| toolResponse: undefined, | ||
| content: [{ type: "text", text: `Created: ${PR_URL}` }], | ||
| }), | ||
| ).toBe(PR_URL); | ||
| }); | ||
|
|
||
| it("handles output field on object toolResponse", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "gh pr create", | ||
| toolResponse: { output: PR_URL }, | ||
| }), | ||
| ).toBe(PR_URL); | ||
| }); | ||
|
|
||
| it("matches gh pr create even with a chained command", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "git push -u origin feat/x && gh pr create --fill", | ||
| toolResponse: { stdout: PR_URL }, | ||
| }), | ||
| ).toBe(PR_URL); | ||
| }); | ||
|
|
||
| it("does not match a fake command containing 'pr create' as text", () => { | ||
| expect( | ||
| extractCreatedPrUrl({ | ||
| toolName: "Bash", | ||
| bashCommand: "echo 'i should pr create later'", | ||
| toolResponse: PR_URL, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const PR_URL_REGEX = /https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/; | ||
| const GH_PR_CREATE_REGEX = /\bgh\s+pr\s+create\b/; | ||
|
|
||
| export interface ExtractCreatedPrUrlInput { | ||
| toolName: string | undefined; | ||
| bashCommand: string | undefined; | ||
| toolResponse: unknown; | ||
| content?: Array<{ type?: string; text?: string }>; | ||
| } | ||
|
|
||
| export function extractCreatedPrUrl( | ||
| input: ExtractCreatedPrUrlInput, | ||
| ): string | null { | ||
| const { toolName, bashCommand, toolResponse, content } = input; | ||
|
|
||
| if (!toolName || !/bash/i.test(toolName)) return null; | ||
| if (!bashCommand || !GH_PR_CREATE_REGEX.test(bashCommand)) return null; | ||
|
|
||
| let textToSearch = ""; | ||
|
|
||
| if (toolResponse) { | ||
| if (typeof toolResponse === "string") { | ||
| textToSearch = toolResponse; | ||
| } else if (typeof toolResponse === "object" && toolResponse !== null) { | ||
| const respObj = toolResponse as Record<string, unknown>; | ||
| textToSearch = | ||
| String(respObj.stdout || "") + String(respObj.stderr || ""); | ||
| if (!textToSearch && respObj.output) { | ||
| textToSearch = String(respObj.output); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (Array.isArray(content)) { | ||
| for (const item of content) { | ||
| if (item.type === "text" && item.text) { | ||
| textToSearch += ` ${item.text}`; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!textToSearch) return null; | ||
|
|
||
| const match = textToSearch.match(PR_URL_REGEX); | ||
| return match ? match[0] : null; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
All 13 test cases follow the exact same shape — vary
toolName,bashCommand,toolResponse/content, and expect a URL ornull. Per the project's simplicity rules, this should be a singleit.each()table rather than 13 separateit()blocks.This also makes it trivial to add new cases without boilerplate.
Context Used: Do not attempt to comment on incorrect alphabetica... (source)
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!