-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
61 lines (48 loc) · 1.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/jessevdk/go-flags"
ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if _, err := flags.Parse(&opts); err != nil {
if err.(*flags.Error).Type != flags.ErrHelp {
fmt.Printf("Error parsing flags: %v \n", err)
}
os.Exit(0)
}
if err := run(ctx); err != nil {
panic(err)
}
}
func run(ctx context.Context) error {
openAIClient := oAIClient.NewClient(opts.OpenAIToken)
githubClient := ghClient.NewClient(ctx, opts.GithubToken)
pr, err := githubClient.GetPullRequest(ctx, opts.Owner, opts.Repo, opts.PRNumber)
if err != nil {
return fmt.Errorf("error getting pull request: %w", err)
}
diff, err := githubClient.CompareCommits(ctx, opts.Owner, opts.Repo, pr.GetBase().GetSHA(), pr.GetHead().GetSHA())
if err != nil {
return fmt.Errorf("error getting commits: %w", err)
}
comments, err := processFiles(ctx, openAIClient, diff)
if err != nil {
return err
}
if opts.Test {
fmt.Printf("Comments: %v \n", comments)
}
err = createComments(ctx, githubClient, comments)
if err != nil {
return fmt.Errorf("error creating comments: %w", err)
}
return nil
}