Skip to content

Commit 92b7e07

Browse files
committed
fix github pullrequest creation error
1 parent fe012c5 commit 92b7e07

File tree

3 files changed

+152
-26
lines changed

3 files changed

+152
-26
lines changed

cmd/unleash-checker-ai/main.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,34 @@ func main() {
3737
}
3838

3939
targetFolder := os.Args[1]
40-
removedFlags, err := finder.FindAndReplaceFlags(targetFolder, staleFlags, cfg.OpenAIAPIKey)
40+
changedFiles, removedFlags, err := finder.FindAndReplaceFlags(targetFolder, staleFlags, cfg.OpenAIAPIKey)
4141
if err != nil {
4242
fmt.Printf("Error finding affected files: %v\n", err)
4343
os.Exit(1)
4444
}
4545

4646
summary := report.CreateSummary(staleFlags, removedFlags)
4747

48+
// chanedFilesに変更がない場合は終了
49+
if len(changedFiles) == 0 {
50+
fmt.Println("No changes required")
51+
return
52+
}
53+
4854
// Create GitHub client
4955
githubClient := github.NewClient(cfg.GitHubToken, cfg.GitHubOwner, cfg.GitHubRepo)
5056

51-
// Create pull request
57+
// Commit changes
5258
ctx := context.Background()
53-
pr, err := githubClient.CreatePullRequest(ctx, "Update stale Unleash flags", summary, "unleash-checker-updates", "main")
59+
branchName := "unleash-checker-updates"
60+
err = githubClient.CommitChanges(ctx, branchName, "Update stale Unleash flags", changedFiles)
61+
if err != nil {
62+
fmt.Printf("Error committing changes: %v\n", err)
63+
os.Exit(1)
64+
}
65+
66+
// Create pull request
67+
pr, err := githubClient.CreatePullRequest(ctx, "Update stale Unleash flags", summary, branchName, "main")
5468
if err != nil {
5569
fmt.Printf("Error creating pull request: %v\n", err)
5670
os.Exit(1)

internal/finder/finder.go

+31-15
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,33 @@ import (
88
"github.com/kromiii/unleash-checker-ai/internal/modifier"
99
)
1010

11-
func FindAndReplaceFlags(root string, flags []string, apiKey string) ([]string, error) {
11+
func FindAndReplaceFlags(root string, flags []string, apiKey string) ([]string, []string, error) {
1212
removedFlags := []string{}
13+
changedFiles := []string{}
1314

1415
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
15-
if err != nil {
16-
return err
17-
}
18-
if info.IsDir() {
16+
if err != nil {
17+
return err
18+
}
19+
if info.IsDir() {
20+
return nil
21+
}
22+
if isFlagUsedInFile(path, flags) {
23+
modifier := modifier.NewModifier(apiKey)
24+
currentRemovedFlags, err := modifier.ModifyFile(path, flags) // 新しい変数で削除されたフラグを受け取る
25+
if err != nil {
26+
return err
27+
}
28+
for _, flag := range currentRemovedFlags {
29+
if !contains(removedFlags, flag) {
30+
removedFlags = append(removedFlags, flag)
31+
}
32+
}
33+
changedFiles = append(changedFiles, path)
34+
}
1935
return nil
20-
}
21-
if isFlagUsedInFile(path, flags) {
22-
modifier := modifier.NewModifier(apiKey)
23-
removedFlags, err = modifier.ModifyFile(path, flags)
24-
if err != nil {
25-
return err
26-
}
27-
}
28-
return nil
2936
})
30-
return removedFlags, err
37+
return changedFiles, removedFlags, err
3138
}
3239

3340
func isFlagUsedInFile(path string, flags []string) bool {
@@ -52,3 +59,12 @@ func isFlagUsedInFile(path string, flags []string) bool {
5259

5360
return false
5461
}
62+
63+
func contains(slice []string, str string) bool {
64+
for _, v := range slice {
65+
if v == str {
66+
return true
67+
}
68+
}
69+
return false
70+
}

internal/github/github.go

+104-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"fmt"
6+
"os"
67

78
"github.com/google/go-github/v38/github"
89
"golang.org/x/oauth2"
@@ -29,18 +30,113 @@ func NewClient(token, owner, repo string) *Client {
2930
}
3031
}
3132

33+
func (c *Client) CommitChanges(ctx context.Context, branch, message string, files []string) error {
34+
// Get the current commit SHA
35+
ref, _, err := c.client.Git.GetRef(ctx, c.owner, c.repo, "refs/heads/"+branch)
36+
if err != nil {
37+
return fmt.Errorf("error getting ref: %v", err)
38+
}
39+
40+
// Create a tree with the updated files
41+
var entries []*github.TreeEntry
42+
for _, file := range files {
43+
content, err := os.ReadFile(file)
44+
if err != nil {
45+
return fmt.Errorf("error reading file %s: %v", file, err)
46+
}
47+
48+
entries = append(entries, &github.TreeEntry{
49+
Path: github.String(file),
50+
Mode: github.String("100644"),
51+
Type: github.String("blob"),
52+
Content: github.String(string(content)),
53+
})
54+
}
55+
56+
tree, _, err := c.client.Git.CreateTree(ctx, c.owner, c.repo, *ref.Object.SHA, entries)
57+
if err != nil {
58+
return fmt.Errorf("error creating tree: %v", err)
59+
}
60+
61+
// Create a new commit
62+
parent, _, err := c.client.Repositories.GetCommit(ctx, c.owner, c.repo, *ref.Object.SHA, nil)
63+
if err != nil {
64+
return fmt.Errorf("error getting parent commit: %v", err)
65+
}
66+
67+
commit, _, err := c.client.Git.CreateCommit(ctx, c.owner, c.repo, &github.Commit{
68+
Message: github.String(message),
69+
Tree: tree,
70+
Parents: []*github.Commit{{SHA: parent.SHA}},
71+
})
72+
if err != nil {
73+
return fmt.Errorf("error creating commit: %v", err)
74+
}
75+
76+
// Update the reference
77+
_, _, err = c.client.Git.UpdateRef(ctx, c.owner, c.repo, &github.Reference{
78+
Ref: github.String("refs/heads/" + branch),
79+
Object: &github.GitObject{SHA: commit.SHA},
80+
}, false)
81+
if err != nil {
82+
return fmt.Errorf("error updating ref: %v", err)
83+
}
84+
85+
return nil
86+
}
87+
3288
func (c *Client) CreatePullRequest(ctx context.Context, title, body, head, base string) (*github.PullRequest, error) {
33-
newPR := &github.NewPullRequest{
34-
Title: github.String(title),
35-
Body: github.String(body),
36-
Head: github.String(head),
37-
Base: github.String(base),
89+
// Check if the branch exists
90+
_, _, err := c.client.Repositories.GetBranch(ctx, c.owner, c.repo, head, false)
91+
if err != nil {
92+
// Branch doesn't exist, create it
93+
ref, _, err := c.client.Git.GetRef(ctx, c.owner, c.repo, "refs/heads/"+base)
94+
if err != nil {
95+
return nil, fmt.Errorf("error getting base branch ref: %v", err)
96+
}
97+
98+
newRef := &github.Reference{
99+
Ref: github.String("refs/heads/" + head),
100+
Object: &github.GitObject{SHA: ref.Object.SHA},
101+
}
102+
103+
_, _, err = c.client.Git.CreateRef(ctx, c.owner, c.repo, newRef)
104+
if err != nil {
105+
return nil, fmt.Errorf("error creating new branch: %v", err)
106+
}
38107
}
39108

40-
pr, _, err := c.client.PullRequests.Create(ctx, c.owner, c.repo, newPR)
109+
// 既存のプルリクエストを検索
110+
opts := &github.PullRequestListOptions{
111+
State: "open",
112+
Head: fmt.Sprintf("%s:%s", c.owner, head),
113+
Base: base,
114+
}
115+
prs, _, err := c.client.PullRequests.List(ctx, c.owner, c.repo, opts)
41116
if err != nil {
42-
return nil, fmt.Errorf("error creating pull request: %v", err)
117+
return nil, fmt.Errorf("error listing pull requests: %v", err)
43118
}
44119

45-
return pr, nil
120+
// 該当するプルリクエストが見つかった場合、更新を行う
121+
if len(prs) > 0 {
122+
pr := prs[0] // 最初のプルリクエストを対象とする
123+
pr.Title = github.String(title)
124+
pr.Body = github.String(body)
125+
return pr, nil
126+
} else {
127+
// 該当するプルリクエストがない場合、新しいプルリクエストを作成
128+
newPR := &github.NewPullRequest{
129+
Title: github.String(title),
130+
Body: github.String(body),
131+
Head: github.String(head),
132+
Base: github.String(base),
133+
}
134+
135+
pr, _, err := c.client.PullRequests.Create(ctx, c.owner, c.repo, newPR)
136+
if err != nil {
137+
return nil, fmt.Errorf("error creating pull request: %v", err)
138+
}
139+
140+
return pr, nil
141+
}
46142
}

0 commit comments

Comments
 (0)