@@ -3,6 +3,7 @@ package github
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
6
7
7
8
"github.com/google/go-github/v38/github"
8
9
"golang.org/x/oauth2"
@@ -29,18 +30,113 @@ func NewClient(token, owner, repo string) *Client {
29
30
}
30
31
}
31
32
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
+
32
88
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
+ }
38
107
}
39
108
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 )
41
116
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 )
43
118
}
44
119
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
+ }
46
142
}
0 commit comments