-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
86 lines (71 loc) · 2.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/google/go-github/v51/github"
"github.com/jessevdk/go-flags"
"github.com/ravilushqa/gpt-pullrequest-updater/description"
ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
"github.com/ravilushqa/gpt-pullrequest-updater/jira"
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
)
var opts struct {
GithubToken string `long:"gh-token" env:"GITHUB_TOKEN" description:"GitHub token" required:"true"`
OpenAIToken string `long:"openai-token" env:"OPENAI_TOKEN" description:"OpenAI token" required:"true"`
Owner string `long:"owner" env:"OWNER" description:"GitHub owner" required:"true"`
Repo string `long:"repo" env:"REPO" description:"GitHub repo" required:"true"`
PRNumber int `long:"pr-number" env:"PR_NUMBER" description:"Pull request number" required:"true"`
Test bool `long:"test" env:"TEST" description:"Test mode"`
JiraURL string `long:"jira-url" env:"JIRA_URL" description:"Jira URL. Example: https://jira.atlassian.com"`
}
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)
}
completion, err := description.GenerateCompletion(ctx, openAIClient, diff, pr)
if err != nil {
return fmt.Errorf("error generating completion: %w", err)
}
if opts.JiraURL != "" {
fmt.Println("Adding Jira ticket")
id, err := jira.ExtractJiraTicketID(*pr.Title)
if err != nil {
fmt.Printf("Error extracting Jira ticket ID: %v \n", err)
} else {
completion = fmt.Sprintf("### JIRA ticket: [%s](%s) \n\n%s", id, jira.GenerateJiraTicketURL(opts.JiraURL, id), completion)
}
}
if opts.Test {
return nil
}
// Update the pull request description
fmt.Println("Updating pull request")
updatePr := &github.PullRequest{Body: github.String(completion)}
if _, err = githubClient.UpdatePullRequest(ctx, opts.Owner, opts.Repo, opts.PRNumber, updatePr); err != nil {
return fmt.Errorf("error updating pull request: %w", err)
}
return nil
}