-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
182 lines (138 loc) · 4.45 KB
/
main.go
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
package main
import (
"fmt"
"strconv"
"strings"
"sync"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
critic "github.com/kmesiab/ai-code-critic/internal"
"github.com/kmesiab/ai-code-critic/ui"
)
var criticWindow *ui.CriticWindow
func main() {
application := app.New()
// application.Settings().SetTheme(theme.DarkTheme())
criticWindow = ui.Initialize(application,
onFileOpenButtonClickedHandler,
onAnalyzeButtonClickedHandler,
onPullRequestModalClickedHandler,
)
_, err := critic.GetConfig()
if err != nil {
dialog.ShowError(err, *criticWindow.Window)
}
(*criticWindow.Window).ShowAndRun()
}
func getCodeReview(prContents, gptModel string) {
criticWindow.ProgressBar.StartProgressBar()
config, err := critic.GetConfig()
if err != nil {
dialog.ShowError(fmt.Errorf("error getting config: %s", err), *criticWindow.Window)
ResetCenterStage()
return
}
ignoreList := strings.Split(config.IgnoreFiles, ",")
// Step 1: Split and parse PR contents into individual GitDiff objects.
gitDiffs := critic.ParseGitDiff(prContents, ignoreList)
// Channel to collect responses from goroutines.
reviewsChan := make(chan string, len(gitDiffs))
var wg sync.WaitGroup
for _, gitDiff := range gitDiffs {
wg.Add(1)
go func(gd *critic.GitDiff) {
defer wg.Done()
// Step 2: Send the diff to GetCodeReviewFromAPI in a goroutine.
review, err := critic.GetCodeReviewFromAPI(gd.DiffContents, gptModel)
if err != nil {
reviewsChan <- fmt.Sprintf("Error getting review for %s: %s\n", gd.FilePathNew, err)
return
}
// Prepend the diff header to the review.
header := fmt.Sprintf("Review for %s:\n", gd.FilePathNew)
reviewsChan <- header + review
}(gitDiff)
}
// Wait for all goroutines to complete.
wg.Wait()
close(reviewsChan)
// Step 4: Assemble all responses into a single string.
var fullReview strings.Builder
for review := range reviewsChan {
fullReview.WriteString(review + "\n\n")
}
criticWindow.ReportPanel.Canvas.ParseMarkdown(fullReview.String())
ResetCenterStage()
}
func onPullRequestModalClickedHandler(ok bool) {
if !ok {
return
}
input := criticWindow.PullRequestURLModal.TextEntry.Text
gptModel := criticWindow.PullRequestURLModal.GPTModel.Text
if input == "" || gptModel == "" {
return
}
criticWindow.ProgressBar.Canvas.Start()
criticWindow.ProgressBar.Canvas.Show()
owner, repo, prNumber, err := critic.ParseGithubPullRequestURL(input)
if err != nil {
dialog.ShowError(fmt.Errorf("error parsing URL: %s", err), *criticWindow.Window)
ResetCenterStage()
return
}
prNumberInt, err := strconv.Atoi(prNumber)
if err != nil {
dialog.ShowError(fmt.Errorf("invalid PR number: %s", err), *criticWindow.Window)
ResetCenterStage()
return
}
err = critic.GetPullRequest(owner, repo, gptModel, prNumberInt, onGetPullRequestHandler)
if err != nil {
dialog.ShowError(fmt.Errorf("error getting PR: %s", err), *criticWindow.Window)
ResetCenterStage()
return
}
}
func onGetPullRequestHandler(prContents, gptModel string) {
// Set the diff text
criticWindow.DiffPanel.SetDiffText(prContents)
// Set the report
criticWindow.ReportPanel.Canvas.ParseMarkdown(critic.WaitingForReportMarkdown)
// Send the pull request to the LLM
go getCodeReview(prContents, gptModel)
}
func onFileOpenButtonClickedHandler() {
criticWindow.PullRequestURLModal.TextEntry.Text = ""
criticWindow.PullRequestURLModal.GPTModel.Text = "Select one or type"
criticWindow.PullRequestURLModal.Form.Show()
}
func onAnalyzeButtonClickedHandler() {
// Prevent re-entry if already processing
if criticWindow.IsAnalyzeButtonActive {
dialog.ShowInformation("Analyze in Progress", "Analysis is already in progress. Please wait until the current process is complete.", *criticWindow.Window)
return
}
diff := criticWindow.DiffPanel.TextGrid.Text()
gptModel := criticWindow.PullRequestURLModal.GPTModel.Text
if diff == "" || criticWindow.DiffPanel.IsDefaultText() {
dialog.ShowError(fmt.Errorf("the diff is empty"), *criticWindow.Window)
return
}
if gptModel == "" {
dialog.ShowError(fmt.Errorf("no gpt model specified"), *criticWindow.Window)
return
}
criticWindow.IsAnalyzeButtonActive = true
go func() {
getCodeReview(diff, gptModel)
criticWindow.IsAnalyzeButtonActive = false
}()
}
func ResetCenterStage() {
criticWindow.DiffPanel.Resize()
criticWindow.ReportPanel.Resize()
criticWindow.ReportPanel.Canvas.Scroll = container.ScrollBoth
criticWindow.ProgressBar.StopProgressBar()
}