@@ -23,6 +23,12 @@ var opts struct {
23
23
Test bool `long:"test" env:"TEST" description:"Test mode"`
24
24
}
25
25
26
+ // FileDiff represents a single file diff.
27
+ type FileDiff struct {
28
+ Header string
29
+ Diff string
30
+ }
31
+
26
32
func main () {
27
33
if _ , err := flags .Parse (& opts ); err != nil {
28
34
if err .(* flags.Error ).Type != flags .ErrHelp {
@@ -32,19 +38,12 @@ func main() {
32
38
}
33
39
openaiClient := openai .NewClient (opts .OpenAIToken )
34
40
35
- title , err := getPullRequestTitle (opts .GithubToken , opts .Owner , opts .Repo , opts .PRNumber )
36
- if err != nil {
37
- return
38
- }
39
-
40
- jiraLink := generateJiraLinkByTitle (title )
41
-
42
41
diff , err := getDiffContent (opts .GithubToken , opts .Owner , opts .Repo , opts .PRNumber )
43
42
if err != nil {
44
43
fmt .Printf ("Error fetching diff content: %v\n " , err )
45
44
return
46
45
}
47
- filesDiff , err := ParseGitDiffAndSplitPerFile (diff )
46
+ filesDiff , err := parseGitDiffAndSplitPerFile (diff )
48
47
if err != nil {
49
48
return
50
49
}
@@ -71,6 +70,13 @@ func main() {
71
70
return
72
71
}
73
72
73
+ title , err := getPullRequestTitle (opts .GithubToken , opts .Owner , opts .Repo , opts .PRNumber )
74
+ if err != nil {
75
+ return
76
+ }
77
+
78
+ jiraLink := generateJiraLinkByTitle (title )
79
+
74
80
description := fmt .Sprintf ("## Jira\n %s\n %s" , jiraLink , chatGPTDescription )
75
81
if opts .Test {
76
82
fmt .Println (description )
@@ -86,41 +92,6 @@ func main() {
86
92
fmt .Println ("Pull request description updated successfully" )
87
93
}
88
94
89
- func updatePullRequestDescription (token string , o string , r string , number int , description string ) error {
90
- url := fmt .Sprintf ("https://api.github.com/repos/%s/%s/pulls/%d" , o , r , number )
91
-
92
- data := map [string ]string {
93
- "body" : description ,
94
- }
95
-
96
- payload , err := json .Marshal (data )
97
- if err != nil {
98
- return err
99
- }
100
-
101
- req , err := http .NewRequest ("PATCH" , url , bytes .NewBuffer (payload ))
102
- if err != nil {
103
- return err
104
- }
105
-
106
- req .Header .Set ("Authorization" , fmt .Sprintf ("token %s" , token ))
107
- req .Header .Set ("Accept" , "application/vnd.github.v3+json" )
108
- req .Header .Set ("Content-Type" , "application/json" )
109
-
110
- client := & http.Client {}
111
- resp , err := client .Do (req )
112
- if err != nil {
113
- return err
114
- }
115
- defer resp .Body .Close ()
116
-
117
- if resp .StatusCode != http .StatusOK {
118
- return fmt .Errorf ("failed to update pull request description. Status code: %d" , resp .StatusCode )
119
- }
120
-
121
- return nil
122
- }
123
-
124
95
func getDiffContent (token , owner , repo string , prNumber int ) (string , error ) {
125
96
url := fmt .Sprintf ("https://api.github.com/repos/%s/%s/pulls/%d" , owner , repo , prNumber )
126
97
method := "GET"
@@ -150,31 +121,8 @@ func getDiffContent(token, owner, repo string, prNumber int) (string, error) {
150
121
return string (body ), nil
151
122
}
152
123
153
- func generatePRDescription (client * openai.Client , messages []openai.ChatCompletionMessage ) (string , error ) {
154
- resp , err := client .CreateChatCompletion (
155
- context .Background (),
156
- openai.ChatCompletionRequest {
157
- Model : openai .GPT3Dot5Turbo ,
158
- Messages : messages ,
159
- },
160
- )
161
-
162
- if err != nil {
163
- fmt .Printf ("ChatCompletion error: %v\n " , err )
164
- return "" , err
165
- }
166
-
167
- return resp .Choices [0 ].Message .Content , nil
168
- }
169
-
170
- // FileDiff represents a single file diff.
171
- type FileDiff struct {
172
- Header string
173
- Diff string
174
- }
175
-
176
- // ParseGitDiffAndSplitPerFile parses a git diff and splits it into a slice of FileDiff.
177
- func ParseGitDiffAndSplitPerFile (diff string ) ([]FileDiff , error ) {
124
+ // parseGitDiffAndSplitPerFile parses a git diff and splits it into a slice of FileDiff.
125
+ func parseGitDiffAndSplitPerFile (diff string ) ([]FileDiff , error ) {
178
126
lines := strings .Split (diff , "\n " )
179
127
var fileDiffs []FileDiff
180
128
@@ -216,6 +164,23 @@ func getFilenameFromDiffHeader(diffHeader string) string {
216
164
}
217
165
}
218
166
167
+ func generatePRDescription (client * openai.Client , messages []openai.ChatCompletionMessage ) (string , error ) {
168
+ resp , err := client .CreateChatCompletion (
169
+ context .Background (),
170
+ openai.ChatCompletionRequest {
171
+ Model : openai .GPT3Dot5Turbo ,
172
+ Messages : messages ,
173
+ },
174
+ )
175
+
176
+ if err != nil {
177
+ fmt .Printf ("ChatCompletion error: %v\n " , err )
178
+ return "" , err
179
+ }
180
+
181
+ return resp .Choices [0 ].Message .Content , nil
182
+ }
183
+
219
184
func getPullRequestTitle (token , owner , repo string , prNumber int ) (string , error ) {
220
185
url := fmt .Sprintf ("https://api.github.com/repos/%s/%s/pulls/%d" , owner , repo , prNumber )
221
186
@@ -260,3 +225,38 @@ func generateJiraLinkByTitle(title string) string {
260
225
261
226
return fmt .Sprintf ("[%s](%s%s)" , issueKey , jiraBaseURL , issueKey )
262
227
}
228
+
229
+ func updatePullRequestDescription (token string , o string , r string , number int , description string ) error {
230
+ url := fmt .Sprintf ("https://api.github.com/repos/%s/%s/pulls/%d" , o , r , number )
231
+
232
+ data := map [string ]string {
233
+ "body" : description ,
234
+ }
235
+
236
+ payload , err := json .Marshal (data )
237
+ if err != nil {
238
+ return err
239
+ }
240
+
241
+ req , err := http .NewRequest ("PATCH" , url , bytes .NewBuffer (payload ))
242
+ if err != nil {
243
+ return err
244
+ }
245
+
246
+ req .Header .Set ("Authorization" , fmt .Sprintf ("token %s" , token ))
247
+ req .Header .Set ("Accept" , "application/vnd.github.v3+json" )
248
+ req .Header .Set ("Content-Type" , "application/json" )
249
+
250
+ client := & http.Client {}
251
+ resp , err := client .Do (req )
252
+ if err != nil {
253
+ return err
254
+ }
255
+ defer resp .Body .Close ()
256
+
257
+ if resp .StatusCode != http .StatusOK {
258
+ return fmt .Errorf ("failed to update pull request description. Status code: %d" , resp .StatusCode )
259
+ }
260
+
261
+ return nil
262
+ }
0 commit comments