-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
235 lines (205 loc) · 6.12 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os/exec"
"strings"
"github.com/kelseyhightower/envconfig"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/cli/go-gh/v2/pkg/repository"
)
const openAIURL = "https://api.openai.com/v1/chat/completions"
type Config struct {
OpenAIKey string `envconfig:"OPENAI_API_KEY" required:"true"`
OpenAIModel string `envconfig:"OPENAI_MODEL" default:"gpt-4o"`
OpenAITemperature float64 `envconfig:"OPENAI_TEMPERATURE" default:"0.1"`
OpenAIMaxTokens int `envconfig:"OPENAI_MAX_TOKENS" default:"450"`
}
var (
verbose bool // Global flag to control verbose output
create bool // Global flag to control pull request creation
titleOnly bool // Global flag to control title-only output
bodyOnly bool // Global flag to control body-only output
japanise bool // Global flag to control Japanese output
)
func printHelp() {
helpMessage := `
This program generates a pull request title and description based on the git diff with the default branch.
USAGE
gh aipr [flags]
FLAGS
--help Show help for command
--verbose Enable verbose output
--create Create a pull request
--title Output only the title
--body Output only the body
--japanise Output in Japanese
EXAMPLES
$ gh aipr --help
$ gh aipr --verbose
ENVIRONMENT VARIABLES
OPENAI_API_KEY Your OpenAI API key (required)
OPENAI_MODEL The OpenAI model to use (default: gpt-4o)
OPENAI_TEMPERATURE The temperature to use for the OpenAI model (default: 0.1)
OPENAI_MAX_TOKENS The maximum number of tokens to use for the OpenAI model (default: 450)
`
fmt.Println(helpMessage)
}
func getDefaultBranch() (string, error) {
client, err := api.DefaultRESTClient()
if err != nil {
return "", err
}
repo, err := repository.Current()
if err != nil {
return "", err
}
var repoInfo struct {
DefaultBranch string `json:"default_branch"`
}
err = client.Get(fmt.Sprintf("repos/%s/%s", repo.Owner, repo.Name), &repoInfo)
if err != nil {
return "", err
}
return repoInfo.DefaultBranch, nil
}
func getGitDiff() (string, error) {
defaultBranch, err := getDefaultBranch()
if err != nil {
return "", err
}
// Get the git diff with the default branch
diffCmd := exec.Command("git", "diff", "origin/"+defaultBranch)
var diffOut bytes.Buffer
diffCmd.Stdout = &diffOut
err = diffCmd.Run()
if err != nil {
return "", err
}
return diffOut.String(), nil
}
func getCurrentBranch() (string, error) {
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
var branchOut bytes.Buffer
branchCmd.Stdout = &branchOut
err := branchCmd.Run()
if err != nil {
return "", err
}
return strings.TrimSpace(branchOut.String()), nil
}
func createPullRequest(title, body string, defaultBranch string) (int, error) {
client, err := api.DefaultRESTClient()
if err != nil {
return 0, err
}
repo, err := repository.Current()
if err != nil {
return 0, err
}
currentBranch, err := getCurrentBranch()
if err != nil {
return 0, err
}
prData := map[string]interface{}{
"title": title,
"body": body,
"head": currentBranch,
"base": defaultBranch, // Use the default branch
}
//fmt.Printf("prData: %+v", prData)
payloadBytes, err := json.Marshal(prData)
if err != nil {
return 0, err
}
bodyReader := bytes.NewReader(payloadBytes)
var prResponse struct {
Number int `json:"number"`
}
err = client.Post(fmt.Sprintf("repos/%s/%s/pulls", repo.Owner, repo.Name), bodyReader, &prResponse)
if err != nil {
return 0, err
}
return prResponse.Number, nil
}
func main() {
var config Config
err := envconfig.Process("", &config)
if err != nil {
fmt.Printf("Failed to process environment variables: %s\n", err)
return
}
var showHelp bool
flag.BoolVar(&verbose, "verbose", false, "Enable verbose output")
flag.BoolVar(&create, "create", false, "Create a pull request")
flag.BoolVar(&showHelp, "help", false, "Show help for command")
flag.BoolVar(&titleOnly, "title", false, "Output only the title")
flag.BoolVar(&bodyOnly, "body", false, "Output only the body")
flag.BoolVar(&japanise, "japanise", false, "Output in Japanese")
flag.Parse()
if showHelp {
printHelp()
return
}
defaultBranch, err := getDefaultBranch()
if err != nil {
fmt.Println("Error getting default branch:", err)
return
}
diffOutput, err := getGitDiff()
if err != nil {
fmt.Println("Error getting git diff:", err)
return
}
var title, body string
if titleOnly {
titlePrompt := CreateOpenAIQuestion(PrTitle, diffOutput, japanise)
title, err = AskOpenAI(openAIURL, config.OpenAIKey, config.OpenAIModel, config.OpenAITemperature, config.OpenAIMaxTokens, titlePrompt, verbose)
if err != nil {
fmt.Println("Error asking OpenAI for title:", err)
return
}
fmt.Println("Generated Pull Request Title:")
fmt.Println(title)
return
}
if bodyOnly {
bodyPrompt := CreateOpenAIQuestion(PrBody, diffOutput, japanise)
body, err = AskOpenAI(openAIURL, config.OpenAIKey, config.OpenAIModel, config.OpenAITemperature, config.OpenAIMaxTokens, bodyPrompt, verbose)
if err != nil {
fmt.Println("Error asking OpenAI for body:", err)
return
}
fmt.Println("Generated Pull Request Description:")
fmt.Println(body)
return
}
titlePrompt := CreateOpenAIQuestion(PrTitle, diffOutput, japanise)
bodyPrompt := CreateOpenAIQuestion(PrBody, diffOutput, japanise)
title, err = AskOpenAI(openAIURL, config.OpenAIKey, config.OpenAIModel, config.OpenAITemperature, config.OpenAIMaxTokens, titlePrompt, verbose)
if err != nil {
fmt.Println("Error asking OpenAI for title:", err)
return
}
body, err = AskOpenAI(openAIURL, config.OpenAIKey, config.OpenAIModel, config.OpenAITemperature, config.OpenAIMaxTokens, bodyPrompt, verbose)
if err != nil {
fmt.Println("Error asking OpenAI for body:", err)
return
}
if create {
prNumber, err := createPullRequest(title, body, defaultBranch)
if err != nil {
fmt.Println("Error creating pull request:", err)
} else {
fmt.Println(prNumber)
}
} else {
fmt.Println("Generated Pull Request Title:")
fmt.Println(title)
fmt.Println("")
fmt.Println("Generated Pull Request Description:")
fmt.Println(body)
}
}