Skip to content

Commit d723226

Browse files
authored
Merge pull request #24 from ravilushqa/refactor_describe
refactor
2 parents f5f869e + 82a2d06 commit d723226

File tree

5 files changed

+121
-96
lines changed

5 files changed

+121
-96
lines changed

cmd/description/main.go

+4-92
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
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 \nDescription: %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-
}

description/description.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package description
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/google/go-github/v51/github"
8+
"github.com/sashabaranov/go-openai"
9+
10+
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
11+
)
12+
13+
func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
14+
sumDiffs := calculateSumDiffs(diff)
15+
16+
var completion string
17+
var err error
18+
if sumDiffs < 4000 {
19+
completion, err = genCompletionOnce(ctx, client, diff)
20+
} else {
21+
completion, err = genCompletionPerFile(ctx, client, diff, pr)
22+
}
23+
24+
return completion, err
25+
}
26+
27+
func calculateSumDiffs(diff *github.CommitsComparison) int {
28+
sumDiffs := 0
29+
for _, file := range diff.Files {
30+
if file.Patch == nil {
31+
continue
32+
}
33+
sumDiffs += len(*file.Patch)
34+
}
35+
return sumDiffs
36+
}
37+
38+
func genCompletionOnce(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison) (string, error) {
39+
fmt.Println("Generating completion once")
40+
messages := make([]openai.ChatCompletionMessage, 0, len(diff.Files))
41+
messages = append(messages, openai.ChatCompletionMessage{
42+
Role: openai.ChatMessageRoleUser,
43+
Content: oAIClient.PromptDescribeChanges,
44+
})
45+
for _, file := range diff.Files {
46+
if file.Patch == nil {
47+
continue
48+
}
49+
50+
messages = append(messages, openai.ChatCompletionMessage{
51+
Role: openai.ChatMessageRoleUser,
52+
Content: *file.Patch,
53+
})
54+
}
55+
56+
fmt.Println("Sending prompt to OpenAI")
57+
completion, err := client.ChatCompletion(ctx, messages)
58+
if err != nil {
59+
return "", fmt.Errorf("error completing prompt: %w", err)
60+
}
61+
62+
return completion, nil
63+
}
64+
65+
func genCompletionPerFile(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
66+
fmt.Println("Generating completion per file")
67+
OverallDescribeCompletion := fmt.Sprintf("Pull request title: %s, body: %s\n\n", pr.GetTitle(), pr.GetBody())
68+
69+
for i, file := range diff.Files {
70+
if file.Patch == nil {
71+
continue
72+
}
73+
prompt := fmt.Sprintf(oAIClient.PromptDescribeChanges, *file.Patch)
74+
75+
if len(prompt) > 4096 {
76+
prompt = fmt.Sprintf("%s...", prompt[:4093])
77+
}
78+
79+
fmt.Printf("Sending prompt to OpenAI for file %d/%d\n", i+1, len(diff.Files))
80+
completion, err := client.ChatCompletion(ctx, []openai.ChatCompletionMessage{
81+
{
82+
Role: openai.ChatMessageRoleUser,
83+
Content: prompt,
84+
},
85+
})
86+
if err != nil {
87+
return "", fmt.Errorf("error getting review: %w", err)
88+
}
89+
OverallDescribeCompletion += fmt.Sprintf("File: %s \nDescription: %s \n\n", file.GetFilename(), completion)
90+
}
91+
92+
fmt.Println("Sending final prompt to OpenAI")
93+
overallCompletion, err := client.ChatCompletion(ctx, []openai.ChatCompletionMessage{
94+
{
95+
Role: openai.ChatMessageRoleUser,
96+
Content: fmt.Sprintf(oAIClient.PromptDescribeOverall, OverallDescribeCompletion),
97+
},
98+
})
99+
if err != nil {
100+
return "", fmt.Errorf("error completing final prompt: %w", err)
101+
}
102+
103+
return overallCompletion, nil
104+
}

openai/openai.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
//go:embed prompts/review
1313
var PromptReview string
1414

15-
const (
16-
PromptDescribeChanges = "Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix\n%s\n"
17-
PromptOverallDescribe = "Below comments are generated by AI, Generate a GitHub pull request description based on the following comments without basic prefix in markdown format with ### Description and ### Changes blocks:\n%s\n"
18-
)
15+
//go:embed prompts/describe_changes
16+
var PromptDescribeChanges string
17+
18+
//go:embed prompts/describe_overall
19+
var PromptDescribeOverall string
1920

2021
type Client struct {
2122
client *openai.Client

openai/prompts/describe_changes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix
2+
%s

openai/prompts/describe_overall

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Below comments are generated by AI
2+
Generate a GitHub pull request description based on the following comments without basic prefix in markdown format.
3+
Response should contain ### Description and ### Changes blocks
4+
5+
PR comments:
6+
%s

0 commit comments

Comments
 (0)