-
Notifications
You must be signed in to change notification settings - Fork 54
Forward precommit hook failures from "Create PR" to the agent #2968
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
salihudickson
wants to merge
2
commits into
PostHog:main
Choose a base branch
from
salihudickson:feat/precommit-hook-failure
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
57 changes: 57 additions & 0 deletions
57
packages/ui/src/features/sessions/components/session-update/CommitFailureActions.tsx
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,57 @@ | ||
| import { Check, Copy, Sparkle } from "@phosphor-icons/react"; | ||
| import { buildCommitHookErrorPrompt } from "@posthog/core/git-interaction/errorPrompts"; | ||
| import { useFixWithAgent } from "@posthog/ui/features/git-interaction/useFixWithAgent"; | ||
| import { Button, Flex } from "@radix-ui/themes"; | ||
| import { useState } from "react"; | ||
|
|
||
| const ICON_SIZE = 12; | ||
|
|
||
| /** | ||
| * Action footer shown under a failed `git commit` Bash tool call (e.g. a | ||
| * pre-commit hook rejected the commit). Lets the user hand the failure output | ||
| * to the agent to resolve, or copy it to paste into a new message. | ||
| */ | ||
| export function CommitFailureActions({ | ||
| command, | ||
| output, | ||
| }: { | ||
| command: string; | ||
| output: string; | ||
| }) { | ||
| const { canFixWithAgent, fixWithAgent } = useFixWithAgent(() => | ||
| buildCommitHookErrorPrompt(command), | ||
| ); | ||
| const [copied, setCopied] = useState(false); | ||
|
|
||
| const handleCopy = async () => { | ||
| try { | ||
| await navigator.clipboard.writeText(output); | ||
| setCopied(true); | ||
| setTimeout(() => setCopied(false), 2000); | ||
| } catch { | ||
| // Clipboard writes can reject when the document isn't focused; ignore. | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Flex gap="2" justify="end" align="center"> | ||
| {canFixWithAgent && ( | ||
| <Button | ||
| size="1" | ||
| variant="soft" | ||
| color="gray" | ||
| onClick={() => { | ||
| void fixWithAgent(output); | ||
| }} | ||
| > | ||
| <Sparkle size={ICON_SIZE} /> | ||
| Fix with agent | ||
| </Button> | ||
| )} | ||
| <Button size="1" variant="soft" color="gray" onClick={handleCopy}> | ||
| {copied ? <Check size={ICON_SIZE} /> : <Copy size={ICON_SIZE} />} | ||
| {copied ? "Copied" : "Copy error"} | ||
| </Button> | ||
| </Flex> | ||
| ); | ||
| } |
93 changes: 93 additions & 0 deletions
93
packages/ui/src/features/sessions/components/session-update/ExecuteToolView.test.tsx
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,93 @@ | ||
| import type { ToolCall } from "@posthog/ui/features/sessions/types"; | ||
| import { Theme } from "@radix-ui/themes"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { ExecuteToolView } from "./ExecuteToolView"; | ||
|
|
||
| vi.mock("./CommitFailureActions", () => ({ | ||
| CommitFailureActions: ({ | ||
| command, | ||
| output, | ||
| }: { | ||
| command: string; | ||
| output: string; | ||
| }) => ( | ||
| <div | ||
| data-testid="commit-failure-actions" | ||
| data-command={command} | ||
| data-output={output} | ||
| /> | ||
| ), | ||
| })); | ||
|
|
||
| function makeToolCall(overrides: Partial<ToolCall> = {}): ToolCall { | ||
| return { | ||
| toolCallId: "tc-1", | ||
| title: "bash", | ||
| kind: "execute", | ||
| status: "completed", | ||
| rawInput: { | ||
| command: 'git commit -m "test"', | ||
| description: "run commit", | ||
| }, | ||
| content: [], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function textContent(text: string): NonNullable<ToolCall["content"]> { | ||
| return [{ type: "content", content: { type: "text", text } }]; | ||
| } | ||
|
|
||
| function renderView(toolCall: ToolCall) { | ||
| return render( | ||
| <Theme> | ||
| <ExecuteToolView toolCall={toolCall} expanded /> | ||
| </Theme>, | ||
| ); | ||
| } | ||
|
|
||
| describe("ExecuteToolView", () => { | ||
| it("shows commit failure actions for a git commit failure with hook output", () => { | ||
| renderView( | ||
| makeToolCall({ | ||
| status: "failed", | ||
| rawInput: { command: 'git commit -m "test"' }, | ||
| content: textContent(`Exit code 1\n╭───╮\n│ hook: pre-commit │\n╰───╯`), | ||
| }), | ||
| ); | ||
|
|
||
| const actions = screen.getByTestId("commit-failure-actions"); | ||
| expect(actions).toHaveAttribute("data-command", 'git commit -m "test"'); | ||
| expect(actions).toHaveAttribute( | ||
| "data-output", | ||
| expect.stringContaining("hook: pre-commit"), | ||
| ); | ||
| }); | ||
|
|
||
| it("still shows commit failure actions when the tool status is completed but the output has a non-zero exit signal", () => { | ||
| renderView( | ||
| makeToolCall({ | ||
| status: "completed", | ||
| rawInput: { command: "pre-commit run --all-files" }, | ||
| content: textContent("exit status 1\npre-commit failed"), | ||
| }), | ||
| ); | ||
|
|
||
| expect(screen.getByTestId("commit-failure-actions")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("does not show commit failure actions for unrelated failed bash output", () => { | ||
| renderView( | ||
| makeToolCall({ | ||
| status: "failed", | ||
| rawInput: { command: "git push origin main" }, | ||
| content: textContent("exit status 1\nnetwork error"), | ||
| }), | ||
| ); | ||
|
|
||
| expect( | ||
| screen.queryByTestId("commit-failure-actions"), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
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
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.
The three
itblocks all exercise the same condition (whetherCommitFailureActionsis rendered) with differentcommand/output/statuscombinations. Per project convention, tests like these should be collapsed into a singleit.eachtable. The attribute assertions from the first test can live in a short standaloneitif needed.Context Used: Do not attempt to comment on incorrect alphabetica... (source)
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!