|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + "github.com/google/go-github/v51/github" |
| 12 | + "github.com/jessevdk/go-flags" |
| 13 | + "github.com/sashabaranov/go-openai" |
| 14 | + |
| 15 | + ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github" |
| 16 | + oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai" |
| 17 | +) |
| 18 | + |
| 19 | +var opts struct { |
| 20 | + GithubToken string `long:"gh-token" env:"GITHUB_TOKEN" description:"GitHub token" required:"true"` |
| 21 | + OpenAIToken string `long:"openai-token" env:"OPENAI_TOKEN" description:"OpenAI token" required:"true"` |
| 22 | + Owner string `long:"owner" env:"OWNER" description:"GitHub owner" required:"true"` |
| 23 | + Repo string `long:"repo" env:"REPO" description:"GitHub repo" required:"true"` |
| 24 | + PRNumber int `long:"pr-number" env:"PR_NUMBER" description:"Pull request number" required:"true"` |
| 25 | + Test bool `long:"test" env:"TEST" description:"Test mode"` |
| 26 | +} |
| 27 | + |
| 28 | +func main() { |
| 29 | + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 30 | + defer cancel() |
| 31 | + |
| 32 | + if _, err := flags.Parse(&opts); err != nil { |
| 33 | + if err.(*flags.Error).Type != flags.ErrHelp { |
| 34 | + fmt.Printf("Error parsing flags: %v \n", err) |
| 35 | + } |
| 36 | + os.Exit(0) |
| 37 | + } |
| 38 | + |
| 39 | + if err := run(ctx); err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func run(ctx context.Context) error { |
| 45 | + openAIClient := oAIClient.NewClient(opts.OpenAIToken) |
| 46 | + githubClient := ghClient.NewClient(ctx, opts.GithubToken) |
| 47 | + |
| 48 | + pr, err := githubClient.GetPullRequest(ctx, opts.Owner, opts.Repo, opts.PRNumber) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("error getting pull request: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + diff, err := githubClient.CompareCommits(ctx, opts.Owner, opts.Repo, pr.GetBase().GetSHA(), pr.GetHead().GetSHA()) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("error getting commits: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + var OverallReviewCompletion string |
| 59 | + for _, file := range diff.Files { |
| 60 | + if file.GetStatus() == "removed" || file.GetStatus() == "renamed" { |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + prompt := fmt.Sprintf(oAIClient.PromptReview, *file.Patch) |
| 65 | + |
| 66 | + if len(prompt) > 4096 { |
| 67 | + prompt = fmt.Sprintf("%s...", prompt[:4093]) |
| 68 | + } |
| 69 | + |
| 70 | + completion, err := openAIClient.ChatCompletion(ctx, []openai.ChatCompletionMessage{ |
| 71 | + { |
| 72 | + Role: openai.ChatMessageRoleUser, |
| 73 | + Content: prompt, |
| 74 | + }, |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("error getting review: %w", err) |
| 78 | + } |
| 79 | + OverallReviewCompletion += fmt.Sprintf("File: %s \nReview: %s \n\n", file.GetFilename(), completion) |
| 80 | + |
| 81 | + position := len(strings.Split(*file.Patch, "\n")) - 1 |
| 82 | + |
| 83 | + comment := &github.PullRequestComment{ |
| 84 | + CommitID: diff.Commits[len(diff.Commits)-1].SHA, |
| 85 | + Path: file.Filename, |
| 86 | + Body: &completion, |
| 87 | + Position: &position, |
| 88 | + } |
| 89 | + |
| 90 | + if opts.Test { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + if _, err := githubClient.CreatePullRequestComment(ctx, opts.Owner, opts.Repo, opts.PRNumber, comment); err != nil { |
| 95 | + return fmt.Errorf("error creating comment: %w", err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + overallCompletion, err := openAIClient.ChatCompletion(ctx, []openai.ChatCompletionMessage{ |
| 100 | + { |
| 101 | + Role: openai.ChatMessageRoleUser, |
| 102 | + Content: fmt.Sprintf(oAIClient.PromptOverallReview, OverallReviewCompletion), |
| 103 | + }, |
| 104 | + }) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("error getting overall review: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + if opts.Test { |
| 110 | + fmt.Println(OverallReviewCompletion) |
| 111 | + fmt.Println("=====================================") |
| 112 | + fmt.Println(overallCompletion) |
| 113 | + |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + comment := &github.PullRequestReviewRequest{Body: &overallCompletion} |
| 118 | + if _, err = githubClient.CreateReview(ctx, opts.Owner, opts.Repo, opts.PRNumber, comment); err != nil { |
| 119 | + return fmt.Errorf("error creating comment: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
0 commit comments