99
1010 "github.com/google/go-github/v51/github"
1111 "github.com/jessevdk/go-flags"
12- "github.com/sashabaranov/go-openai"
1312
13+ "github.com/ravilushqa/gpt-pullrequest-updater/description"
1414 ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
1515 "github.com/ravilushqa/gpt-pullrequest-updater/jira"
1616 oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
@@ -37,10 +37,6 @@ func main() {
3737 os .Exit (0 )
3838 }
3939
40- if opts .Test {
41- fmt .Println ("Test mode" )
42- }
43-
4440 if err := run (ctx ); err != nil {
4541 panic (err )
4642 }
@@ -60,25 +56,9 @@ func run(ctx context.Context) error {
6056 return fmt .Errorf ("error getting commits: %w" , err )
6157 }
6258
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 )
8262 }
8363
8464 if opts .JiraURL != "" {
@@ -105,71 +85,3 @@ func run(ctx context.Context) error {
10585
10686 return nil
10787}
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