@@ -2,30 +2,17 @@ package main
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
- "errors"
7
5
"fmt"
8
6
"os"
9
7
"os/signal"
10
8
"syscall"
11
9
12
- "github.com/google/go-github/v51/github"
13
10
"github.com/jessevdk/go-flags"
14
- "github.com/sashabaranov/go-openai"
15
11
16
12
ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
17
13
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
18
14
)
19
15
20
- var opts struct {
21
- GithubToken string `long:"gh-token" env:"GITHUB_TOKEN" description:"GitHub token" required:"true"`
22
- OpenAIToken string `long:"openai-token" env:"OPENAI_TOKEN" description:"OpenAI token" required:"true"`
23
- Owner string `long:"owner" env:"OWNER" description:"GitHub owner" required:"true"`
24
- Repo string `long:"repo" env:"REPO" description:"GitHub repo" required:"true"`
25
- PRNumber int `long:"pr-number" env:"PR_NUMBER" description:"Pull request number" required:"true"`
26
- Test bool `long:"test" env:"TEST" description:"Test mode"`
27
- }
28
-
29
16
func main () {
30
17
ctx , cancel := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGTERM )
31
18
defer cancel ()
@@ -56,122 +43,19 @@ func run(ctx context.Context) error {
56
43
return fmt .Errorf ("error getting commits: %w" , err )
57
44
}
58
45
59
- var comments []* github.PullRequestComment
60
-
61
- for i , file := range diff .Files {
62
- patch := file .GetPatch ()
63
- fmt .Printf ("processing file: %s %d/%d\n " , file .GetFilename (), i + 1 , len (diff .Files ))
64
- if patch == "" || file .GetStatus () == "removed" || file .GetStatus () == "renamed" {
65
- continue
66
- }
67
-
68
- if len (patch ) > 3000 {
69
- fmt .Println ("Patch is too long, truncating" )
70
- patch = fmt .Sprintf ("%s..." , patch [:3000 ])
71
- }
72
- completion , err := openAIClient .ChatCompletion (ctx , []openai.ChatCompletionMessage {
73
- {
74
- Role : openai .ChatMessageRoleUser ,
75
- Content : oAIClient .PromptReview ,
76
- },
77
- {
78
- Role : openai .ChatMessageRoleUser ,
79
- Content : patch ,
80
- },
81
- })
82
-
83
- if err != nil {
84
- return fmt .Errorf ("error getting completion: %w" , err )
85
- }
86
-
87
- if opts .Test {
88
- fmt .Println ("Completion:" , completion )
89
- }
90
-
91
- review , err := extractJSON (completion )
92
- if err != nil {
93
- fmt .Println ("Error extracting JSON:" , err )
94
- continue
95
- }
96
-
97
- if review .Quality == Good {
98
- fmt .Println ("Review is good" )
99
- continue
100
- }
101
- for _ , issue := range review .Issues {
102
- body := fmt .Sprintf ("[%s] %s" , issue .Type , issue .Description )
103
- comment := & github.PullRequestComment {
104
- CommitID : diff .Commits [len (diff .Commits )- 1 ].SHA ,
105
- Path : file .Filename ,
106
- Body : & body ,
107
- Position : & issue .Line ,
108
- }
109
- comments = append (comments , comment )
110
- }
111
-
112
- if opts .Test {
113
- continue
114
- }
115
-
116
- for i , c := range comments {
117
- fmt .Printf ("creating comment: %s %d/%d\n " , * c .Path , i + 1 , len (comments ))
118
- if _ , err := githubClient .CreatePullRequestComment (ctx , opts .Owner , opts .Repo , opts .PRNumber , c ); err != nil {
119
- return fmt .Errorf ("error creating comment: %w" , err )
120
- }
121
- }
122
- }
123
- return nil
124
- }
125
-
126
- type Review struct {
127
- Quality Quality `json:"quality"`
128
- Issues []struct {
129
- Type string `json:"type"`
130
- Line int `json:"line"`
131
- Description string `json:"description"`
132
- } `json:"issues"`
133
- }
134
-
135
- type Quality string
136
-
137
- const (
138
- Good Quality = "good"
139
- Bad Quality = "bad"
140
- Neutral Quality = "neutral"
141
- )
142
-
143
- func extractJSON (input string ) (* Review , error ) {
144
- var jsonObj * Review
145
-
146
- // find the start and end positions of the JSON object
147
- start := 0
148
- end := len (input )
149
- for i , c := range input {
150
- if c == '{' {
151
- start = i
152
- break
153
- }
154
- if i == len (input )- 1 {
155
- return nil , errors .New ("invalid JSON object" )
156
- }
46
+ comments , err := processFiles (ctx , openAIClient , diff )
47
+ if err != nil {
48
+ return err
157
49
}
158
- for i := len (input ) - 1 ; i >= 0 ; i -- {
159
- if input [i ] == '}' {
160
- end = i + 1
161
- break
162
- }
163
50
164
- if i == 0 {
165
- return nil , errors .New ("invalid JSON object" )
166
- }
51
+ if opts .Test {
52
+ fmt .Printf ("Comments: %v \n " , comments )
167
53
}
168
54
169
- // extract the JSON object from the input
170
- jsonStr := input [start :end ]
171
- err := json .Unmarshal ([]byte (jsonStr ), & jsonObj )
55
+ err = createComments (ctx , githubClient , comments )
172
56
if err != nil {
173
- return nil , errors . New ( "invalid JSON object" )
57
+ return fmt . Errorf ( "error creating comments: %w" , err )
174
58
}
175
59
176
- return jsonObj , nil
60
+ return nil
177
61
}
0 commit comments