generated from kijimaD/go_skel
/
process.go
191 lines (156 loc) · 3.34 KB
/
process.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
package gh
import (
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/kijimaD/gar/strutil"
"github.com/google/go-github/v48/github"
"github.com/olekukonko/tablewriter"
)
type CallClient struct {
API clientI
Writer io.Writer
Commits []*github.RepositoryCommit
Replys []Reply
PRComment string
}
func NewClient(api clientI, writer io.Writer) *CallClient {
return &CallClient{
API: api,
Writer: writer,
}
}
type Reply struct {
ReplyID int64
GitHash string
CommitMsg string
OriginalComment string
IsValid bool
}
func (c *CallClient) GetCommits() {
c.Commits = c.API.PRCommits()
}
func (c *CallClient) ParseCommit() {
var replys []Reply
for _, commit := range c.Commits {
id, err := c.parseMsg(*commit.Commit.Message)
if err != nil {
panic(err)
}
reply := Reply{
ReplyID: id,
GitHash: *commit.SHA,
CommitMsg: *commit.Commit.Message,
}
replys = append(replys, reply)
}
c.Replys = replys
}
func (c *CallClient) parseMsg(s string) (int64, error) {
user := c.API.GetPR().User
repo := c.API.GetPR().Repo
num := c.API.GetPR().Number
regex := fmt.Sprintf(`https://github.com/%s/%s/pull/%d#discussion_r(\d+)`, user, repo, num)
r := regexp.MustCompile(regex)
result := r.FindAllStringSubmatch(s, -1)
if len(result) == 0 {
return -1, nil
}
int64, err := strconv.ParseInt(result[0][1], 10, 64)
if err != nil {
return -1, err
}
return int64, nil
}
func (c *CallClient) FetchCommentById() {
for i, r := range c.Replys {
if r.ReplyID == -1 {
continue
}
comment := c.API.GetCommentByID(r.ReplyID)
c.Replys[i].OriginalComment = *comment.Body
}
}
func (c *CallClient) FetchPRComment() {
comments := c.API.GetCommentList()
var commentsStr []string
for _, com := range comments {
commentsStr = append(commentsStr, *com.Body)
}
c.PRComment = strings.Join(commentsStr, " ")
}
func (c *CallClient) Validate() {
for i, r := range c.Replys {
// 返信先があるかどうか
valid1 := false
if r.OriginalComment == "" {
valid1 = false
} else {
valid1 = true
}
// 重複してるかどうか
valid2 := false
if strings.Contains(c.PRComment, r.GitHash) {
valid2 = false
} else {
valid2 = true
}
c.Replys[i].IsValid = valid1 && valid2
}
}
func (c *CallClient) Display() {
if len(c.Replys) == 0 {
fmt.Fprintf(c.Writer, "Not found commit!\n")
} else {
data := [][]string{}
for i, r := range c.Replys {
idx := fmt.Sprintf("%02d", i)
hash := r.GitHash[0:7]
commitMsg := strutil.Substring(r.CommitMsg, 0, 9)
linkedComment := strutil.Substring(r.OriginalComment, 0, 17)
isSend := strutil.YorN(r.IsValid)
data = append(data, []string{
idx,
fmt.Sprintf(
"%s %s",
hash,
commitMsg,
),
linkedComment,
isSend,
})
}
table := tablewriter.NewWriter(c.Writer)
table.SetHeader([]string{"IDX", "COMMIT", "LINKED COMMENT", "SEND"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
}
func (c *CallClient) SendReply() error {
for _, r := range c.Replys {
if !r.IsValid {
continue
}
err := c.API.SendReply(Reply{
ReplyID: r.ReplyID,
GitHash: r.GitHash,
})
if err != nil {
return err
}
}
return nil
}
func (c *CallClient) ValidCount() int {
var count int
for _, r := range c.Replys {
if r.IsValid {
count += 1
}
}
return count
}