@@ -23,6 +23,12 @@ var opts struct {
2323 Test bool `long:"test" env:"TEST" description:"Test mode"`
2424}
2525
26+ // FileDiff represents a single file diff.
27+ type FileDiff struct {
28+ Header string
29+ Diff string
30+ }
31+
2632func main () {
2733 if _ , err := flags .Parse (& opts ); err != nil {
2834 if err .(* flags.Error ).Type != flags .ErrHelp {
@@ -32,19 +38,12 @@ func main() {
3238 }
3339 openaiClient := openai .NewClient (opts .OpenAIToken )
3440
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-
4241 diff , err := getDiffContent (opts .GithubToken , opts .Owner , opts .Repo , opts .PRNumber )
4342 if err != nil {
4443 fmt .Printf ("Error fetching diff content: %v\n " , err )
4544 return
4645 }
47- filesDiff , err := ParseGitDiffAndSplitPerFile (diff )
46+ filesDiff , err := parseGitDiffAndSplitPerFile (diff )
4847 if err != nil {
4948 return
5049 }
@@ -71,6 +70,13 @@ func main() {
7170 return
7271 }
7372
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+
7480 description := fmt .Sprintf ("## Jira\n %s\n %s" , jiraLink , chatGPTDescription )
7581 if opts .Test {
7682 fmt .Println (description )
@@ -86,41 +92,6 @@ func main() {
8692 fmt .Println ("Pull request description updated successfully" )
8793}
8894
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-
12495func getDiffContent (token , owner , repo string , prNumber int ) (string , error ) {
12596 url := fmt .Sprintf ("https://api.github.com/repos/%s/%s/pulls/%d" , owner , repo , prNumber )
12697 method := "GET"
@@ -150,31 +121,8 @@ func getDiffContent(token, owner, repo string, prNumber int) (string, error) {
150121 return string (body ), nil
151122}
152123
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 ) {
178126 lines := strings .Split (diff , "\n " )
179127 var fileDiffs []FileDiff
180128
@@ -216,6 +164,23 @@ func getFilenameFromDiffHeader(diffHeader string) string {
216164 }
217165}
218166
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+
219184func getPullRequestTitle (token , owner , repo string , prNumber int ) (string , error ) {
220185 url := fmt .Sprintf ("https://api.github.com/repos/%s/%s/pulls/%d" , owner , repo , prNumber )
221186
@@ -260,3 +225,38 @@ func generateJiraLinkByTitle(title string) string {
260225
261226 return fmt .Sprintf ("[%s](%s%s)" , issueKey , jiraBaseURL , issueKey )
262227}
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