-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-pr-dialog.tsx
More file actions
219 lines (202 loc) · 7.38 KB
/
merge-pr-dialog.tsx
File metadata and controls
219 lines (202 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use client'
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Label } from '@/components/ui/label'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { toast } from 'sonner'
import { Loader2, AlertTriangle } from 'lucide-react'
interface MergePRDialogProps {
taskId: string
prUrl: string
prNumber: number
open: boolean
onOpenChange: (open: boolean) => void
onPRMerged?: () => void
onMergeInitiated?: () => void
}
function buildClientMessageId(prefix: string): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return `${prefix}:${crypto.randomUUID()}`
}
return `${prefix}:${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
}
export function MergePRDialog({
taskId,
prUrl: _prUrl,
prNumber,
open,
onOpenChange,
onPRMerged,
onMergeInitiated,
}: MergePRDialogProps) {
const [mergeMethod, setMergeMethod] = useState<'squash' | 'merge' | 'rebase'>('squash')
const [isMerging, setIsMerging] = useState(false)
const [showConflictDialog, setShowConflictDialog] = useState(false)
const [isSendingMessage, setIsSendingMessage] = useState(false)
const handleMergePR = async () => {
setIsMerging(true)
// Notify parent that merge is initiated (for loading state)
if (onMergeInitiated) {
onMergeInitiated()
}
try {
const response = await fetch(`/api/tasks/${taskId}/merge-pr`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
mergeMethod,
}),
})
const result = await response.json()
if (response.ok && result.success) {
// Don't show toast here - parent will show it when status updates
if (onPRMerged) {
onPRMerged()
}
onOpenChange(false)
} else {
// Check if this is a merge conflict error
// GitHub returns "Pull request is not mergeable" (405) or "Merge conflict - cannot auto-merge" (409)
if (result.error && (result.error.includes('conflict') || result.error.includes('mergeable'))) {
// Show the conflict resolution dialog
setShowConflictDialog(true)
} else {
toast.error(result.error || 'Failed to merge pull request')
}
}
} catch (error) {
console.error('Error merging PR:', error)
toast.error('Failed to merge pull request')
} finally {
setIsMerging(false)
}
}
const handleAgentFixConflict = async () => {
setIsSendingMessage(true)
try {
// Send a follow-up message to the current task to fix merge conflicts
const response = await fetch(`/api/tasks/${taskId}/chat/v2`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
clientMessageId: buildClientMessageId('merge-conflict-follow-up'),
prompt:
'Fix merge conflicts in the current branch and prepare it for merging. Review the conflicting changes carefully and resolve them intelligently, preserving the intent of both sets of changes where possible.',
}),
})
const result = await response.json()
if (response.ok && result.success) {
toast.success('Agent is now fixing the merge conflict')
setShowConflictDialog(false)
onOpenChange(false)
} else {
toast.error(result.error || 'Failed to send message to agent')
}
} catch (error) {
console.error('Error sending message to agent:', error)
toast.error('Failed to send message to agent')
} finally {
setIsSendingMessage(false)
}
}
const handleCancelConflictDialog = () => {
setShowConflictDialog(false)
onOpenChange(false)
}
return (
<>
{/* Main Merge Dialog */}
<Dialog open={open && !showConflictDialog} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle>Merge Pull Request</DialogTitle>
<DialogDescription>
This will merge PR #{prNumber} into the main branch. Choose your preferred merge method.
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-2">
<Label htmlFor="mergeMethod">Merge Method</Label>
<Select
value={mergeMethod}
onValueChange={(value: 'squash' | 'merge' | 'rebase') => setMergeMethod(value)}
disabled={isMerging}
>
<SelectTrigger id="mergeMethod">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="squash">Squash and merge</SelectItem>
<SelectItem value="merge">Create a merge commit</SelectItem>
<SelectItem value="rebase">Rebase and merge</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={isMerging}>
Cancel
</Button>
<Button onClick={handleMergePR} disabled={isMerging}>
{isMerging && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{isMerging ? 'Merging...' : 'Merge Pull Request'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{/* Merge Conflict Resolution Dialog */}
<Dialog
open={open && showConflictDialog}
onOpenChange={(isOpen) => {
if (!isOpen) {
setShowConflictDialog(false)
}
onOpenChange(isOpen)
}}
>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<AlertTriangle className="h-5 w-5 text-yellow-500" />
Merge Conflict Detected
</DialogTitle>
<DialogDescription>
This pull request has merge conflicts that prevent automatic merging. Would you like an AI agent to
resolve the conflicts for you?
</DialogDescription>
</DialogHeader>
<div className="py-4">
<p className="text-sm text-muted-foreground">The agent will:</p>
<ul className="list-disc list-inside text-sm text-muted-foreground mt-2 space-y-1">
<li>Analyze the conflicting changes</li>
<li>Intelligently merge the code</li>
<li>Create a new commit with the resolved conflicts</li>
<li>Push the changes to your branch</li>
</ul>
</div>
<DialogFooter>
<Button variant="outline" onClick={handleCancelConflictDialog} disabled={isSendingMessage}>
Cancel
</Button>
<Button onClick={handleAgentFixConflict} disabled={isSendingMessage}>
{isSendingMessage && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{isSendingMessage ? 'Sending Message...' : 'Fix with Agent'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
)
}