-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevision.go
187 lines (165 loc) · 4.2 KB
/
revision.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
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
)
const (
mdPrefix = "[//]: # (gh-pr-revision:"
mdSuffix = ")"
bodyStart = "[//]: # (gh-pr-revision-body-start)"
bodyEnd = "[//]: # (gh-pr-revision-body-end)"
)
type Revision struct {
CreatedAt string `json:"-"`
Number uint64 `json:"number"`
Hash string `json:"hash"`
BaseHash string `json:"baseHash"`
UserReviewers []string `json:"user_reviewers,omitempty"`
TeamReviewers []string `json:"team_reviewers,omitempty"`
Comment string `json:"-"`
}
func (r Revision) Dump(w io.Writer, long bool) {
if long {
fmt.Fprintf(w, "%d. %s\n", r.Number, r.CreatedAt)
fmt.Fprintf(w, "Hash: %s\n", r.Hash)
fmt.Fprintf(w, "BaseHash: %s\n", r.BaseHash)
if len(r.Comment) > 0 {
fmt.Fprintf(w, "Comment:\n%s\n", r.Comment)
}
} else {
fmt.Fprintf(w, "%2d. %s %s\n", r.Number, r.CreatedAt, r.Hash)
}
}
func (r *Revision) AddUserReviewer(login string) {
if len(login) == 0 {
return
}
for _, l := range r.UserReviewers {
if l == login {
return
}
}
r.UserReviewers = append(r.UserReviewers, login)
}
func (r *Revision) AddTeamReviewer(slug string) {
if len(slug) == 0 {
return
}
for _, s := range r.TeamReviewers {
if s == slug {
return
}
}
r.TeamReviewers = append(r.TeamReviewers, slug)
}
func (r *Revision) ExtendUserReviewers(reviewers ...string) {
for _, login := range reviewers {
r.AddUserReviewer(login)
}
}
func (r *Revision) ExtendTeamReviewers(reviewers ...string) {
for _, slug := range reviewers {
r.AddTeamReviewer(slug)
}
}
func (r *Revision) ExtendReviewers(revisions ...Revision) {
for _, other := range revisions {
for _, login := range other.UserReviewers {
r.AddUserReviewer(login)
}
for _, slug := range other.TeamReviewers {
r.AddTeamReviewer(slug)
}
}
}
func (r *Revision) ExtendReviewersFromApi(apiPr ApiPullRequest) {
for _, v := range apiPr.Reviewers {
if v.Type == "User" {
r.AddUserReviewer(v.Login)
}
}
for _, v := range apiPr.ReviewerTeams {
r.AddTeamReviewer(v.Slug)
}
}
func (r Revision) createReviewRequest() reviewRequest {
var request reviewRequest
request.Reviewers = make([]string, len(r.UserReviewers))
copy(request.Reviewers, r.UserReviewers)
request.TeamReviewers = make([]string, len(r.TeamReviewers))
copy(request.TeamReviewers, r.TeamReviewers)
return request
}
func parseRevisions(pr PullRequest) (revisions []Revision, err error) {
var rev *Revision
for _, comment := range pr.Comments {
if rev, err = parseRevision(comment); err != nil {
return nil, err
}
if rev != nil {
revisions = append(revisions, *rev)
}
}
return revisions, nil
}
func parseRevision(comment Comment) (*Revision, error) {
for _, s := range strings.Split(comment.Body, "\n") {
if tmp, ok := strings.CutPrefix(s, mdPrefix); ok {
if tmp, ok := strings.CutSuffix(tmp, mdSuffix); ok {
return parseRevisionStr(comment, tmp)
}
}
}
return nil, nil
}
func parseRevisionStr(comment Comment, encoded string) (*Revision, error) {
var rev Revision
if err := json.Unmarshal([]byte(encoded), &rev); err != nil {
return nil, fmt.Errorf("json.decode failed: %v", err)
}
rev.CreatedAt = comment.CreatedAt
rev.Comment = parseBody(comment)
return &rev, nil
}
func encodeRevision(rev Revision) (string, error) {
json, err := json.Marshal(rev)
if err != nil {
return "", fmt.Errorf("json.encode failed: %v", err)
}
return fmt.Sprintf("%s%s%s", mdPrefix, string(json), mdSuffix), nil
}
func encodeBody(body string) string {
return fmt.Sprintf("\n\n%s\n\n%s\n\n%s\n\n", bodyStart, body, bodyEnd)
}
func parseBody(comment Comment) string {
var startSeen, endSeen bool
var lines []string
for _, line := range strings.Split(comment.Body, "\n") {
if !startSeen && line == bodyStart {
startSeen = true
continue
}
if !startSeen {
continue
}
if line == bodyEnd {
endSeen = true
break
}
lines = append(lines, line)
}
if !(startSeen && endSeen) {
return ""
}
// strip empty lines in the beginning
for len(lines) > 0 && len(lines[0]) == 0 {
lines = lines[1:]
}
// strip empty lines in the end
for len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
lines = lines[:len(lines)-1]
}
return strings.Join(lines, "\n")
}