9
9
10
10
"github.com/google/go-github/v51/github"
11
11
"github.com/jessevdk/go-flags"
12
- "github.com/sashabaranov/go-openai"
13
12
13
+ "github.com/ravilushqa/gpt-pullrequest-updater/description"
14
14
ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
15
15
"github.com/ravilushqa/gpt-pullrequest-updater/jira"
16
16
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
@@ -37,10 +37,6 @@ func main() {
37
37
os .Exit (0 )
38
38
}
39
39
40
- if opts .Test {
41
- fmt .Println ("Test mode" )
42
- }
43
-
44
40
if err := run (ctx ); err != nil {
45
41
panic (err )
46
42
}
@@ -60,25 +56,9 @@ func run(ctx context.Context) error {
60
56
return fmt .Errorf ("error getting commits: %w" , err )
61
57
}
62
58
63
- var sumDiffs int
64
- for _ , file := range diff .Files {
65
- if file .Patch == nil {
66
- continue
67
- }
68
- sumDiffs += len (* file .Patch )
69
- }
70
-
71
- var completion string
72
- if sumDiffs < 4000 {
73
- completion , err = genCompletionOnce (ctx , openAIClient , diff )
74
- if err != nil {
75
- return fmt .Errorf ("error generating completition once: %w" , err )
76
- }
77
- } else {
78
- completion , err = genCompletionPerFile (ctx , openAIClient , diff , pr )
79
- if err != nil {
80
- return fmt .Errorf ("error generating completition twice: %w" , err )
81
- }
59
+ completion , err := description .GenerateCompletion (ctx , openAIClient , diff , pr )
60
+ if err != nil {
61
+ return fmt .Errorf ("error generating completion: %w" , err )
82
62
}
83
63
84
64
if opts .JiraURL != "" {
@@ -105,71 +85,3 @@ func run(ctx context.Context) error {
105
85
106
86
return nil
107
87
}
108
-
109
- func genCompletionOnce (ctx context.Context , client * oAIClient.Client , diff * github.CommitsComparison ) (string , error ) {
110
- fmt .Println ("Generating completion once" )
111
- messages := make ([]openai.ChatCompletionMessage , 0 , len (diff .Files ))
112
- messages = append (messages , openai.ChatCompletionMessage {
113
- Role : openai .ChatMessageRoleUser ,
114
- Content : oAIClient .PromptDescribeChanges ,
115
- })
116
- for _ , file := range diff .Files {
117
- if file .Patch == nil {
118
- continue
119
- }
120
-
121
- messages = append (messages , openai.ChatCompletionMessage {
122
- Role : openai .ChatMessageRoleUser ,
123
- Content : * file .Patch ,
124
- })
125
- }
126
-
127
- fmt .Println ("Sending prompt to OpenAI" )
128
- completion , err := client .ChatCompletion (ctx , messages )
129
- if err != nil {
130
- return "" , fmt .Errorf ("error completing prompt: %w" , err )
131
- }
132
-
133
- return completion , nil
134
- }
135
-
136
- func genCompletionPerFile (ctx context.Context , client * oAIClient.Client , diff * github.CommitsComparison , pr * github.PullRequest ) (string , error ) {
137
- fmt .Println ("Generating completion per file" )
138
- OverallDescribeCompletion := fmt .Sprintf ("Pull request title: %s, body: %s\n \n " , pr .GetTitle (), pr .GetBody ())
139
-
140
- for i , file := range diff .Files {
141
- if file .Patch == nil {
142
- continue
143
- }
144
- prompt := fmt .Sprintf (oAIClient .PromptDescribeChanges , * file .Patch )
145
-
146
- if len (prompt ) > 4096 {
147
- prompt = fmt .Sprintf ("%s..." , prompt [:4093 ])
148
- }
149
-
150
- fmt .Printf ("Sending prompt to OpenAI for file %d/%d\n " , i + 1 , len (diff .Files ))
151
- completion , err := client .ChatCompletion (ctx , []openai.ChatCompletionMessage {
152
- {
153
- Role : openai .ChatMessageRoleUser ,
154
- Content : prompt ,
155
- },
156
- })
157
- if err != nil {
158
- return "" , fmt .Errorf ("error getting review: %w" , err )
159
- }
160
- OverallDescribeCompletion += fmt .Sprintf ("File: %s \n Description: %s \n \n " , file .GetFilename (), completion )
161
- }
162
-
163
- fmt .Println ("Sending final prompt to OpenAI" )
164
- overallCompletion , err := client .ChatCompletion (ctx , []openai.ChatCompletionMessage {
165
- {
166
- Role : openai .ChatMessageRoleUser ,
167
- Content : fmt .Sprintf (oAIClient .PromptOverallDescribe , OverallDescribeCompletion ),
168
- },
169
- })
170
- if err != nil {
171
- return "" , fmt .Errorf ("error getting overall review: %w" , err )
172
- }
173
-
174
- return overallCompletion , nil
175
- }
0 commit comments