-
Notifications
You must be signed in to change notification settings - Fork 0
/
change.go
177 lines (158 loc) · 4.97 KB
/
change.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
package gerrittest
import (
"io/ioutil"
"os"
"strconv"
"github.com/andygrunwald/go-gerrit"
log "github.com/sirupsen/logrus"
)
var (
// DefaultRevision is the revision to use in the Change struct when
// no other revision is provided.
DefaultRevision = "current"
)
const (
// VerifiedLabel is a string representing the 'Verified' label
VerifiedLabel = "Verified"
// CodeReviewLabel is a string representing the 'Code-Review' label
CodeReviewLabel = "Code-Review"
)
// Change is used to interact with an manipulate a single change.
type Change struct {
api *gerrit.Client
log *log.Entry
ChangeID string
Repo *Repository
}
func (c *Change) logError(err error, logger *log.Entry, response *gerrit.Response) {
if err != nil {
logger = logger.WithError(err)
body, _ := ioutil.ReadAll(response.Body) // nolint: errcheck
logger.WithField("body", string(body)).Error()
}
}
// Destroy is responsible for removing any files on disk related to
// this change.
func (c *Change) Destroy() error {
return c.Repo.Destroy()
}
// Push pushes changes to Gerrit.
func (c *Change) Push() error {
return c.Repo.Push("HEAD:refs/for/master")
}
// Add writes a file to the repository but does not commit it. The added or
// modified path will be staged for commit.
func (c *Change) Add(relative string, mode os.FileMode, content string) error {
if err := c.Repo.Add(relative, mode, []byte(content)); err != nil {
return err
}
return c.Repo.Amend()
}
// Remove will remove the given relative path from the repository. If the file
// or directory does not exist this function does nothing. The removed path
// will staged for commit.
func (c *Change) Remove(relative string) error {
if err := c.Repo.Remove(relative); err != nil {
return err
}
return c.Repo.Amend()
}
// ApplyLabel will apply the requested label to the current change. Examples
// of labels include 'Code-Review +2' or 'Verified +1'. If a specific revision
// is not provided then 'current' will be used.
func (c *Change) ApplyLabel(revision string, label string, value int) (*gerrit.ReviewResult, error) {
if revision == "" {
revision = DefaultRevision
}
logger := c.log.WithFields(log.Fields{
"phase": "apply-label",
"revision": revision,
"label": label,
"value": value,
})
logger = logger.WithField("id", c.ChangeID)
logger.Debug()
info, response, err := c.api.Changes.SetReview(c.ChangeID, revision, &gerrit.ReviewInput{
Labels: map[string]string{
label: strconv.Itoa(value),
},
Drafts: "PUBLISH_ALL_REVISIONS",
})
c.logError(err, logger, response)
return info, err
}
// Submit will submit the change. Note, this typically will only work if the
// change has Code-Review +2 and Verified +1 labels applied.
func (c *Change) Submit() (*gerrit.ChangeInfo, error) {
logger := c.log.WithField("phase", "submit")
logger.Debug()
info, response, err := c.api.Changes.SubmitChange(c.ChangeID, &gerrit.SubmitInput{})
c.logError(err, logger, response)
return info, err
}
// Abandon will abandon the change.
func (c *Change) Abandon() (*gerrit.ChangeInfo, error) {
logger := c.log.WithField("phase", "abandon")
logger.Debug()
info, response, err := c.api.Changes.AbandonChange(c.ChangeID, &gerrit.AbandonInput{
Notify: "NONE",
})
c.logError(err, logger, response)
return info, err
}
// AddTopLevelComment will a single top level comment to the current
// change.
func (c *Change) AddTopLevelComment(revision string, comment string) (*gerrit.ReviewResult, error) {
if revision == "" {
revision = DefaultRevision
}
logger := c.log.WithFields(log.Fields{
"phase": "add-top-level-comment",
"revision": revision,
"comment": comment,
})
id, err := c.Repo.ChangeID()
if err != nil {
return nil, err
}
logger = logger.WithField("id", id)
logger.Debug()
result, response, err := c.api.Changes.SetReview(id, revision, &gerrit.ReviewInput{
Message: comment,
Drafts: "PUBLISH_ALL_REVISIONS",
Notify: "NONE", // Don't send email
OmitDuplicateComments: true,
})
c.logError(err, logger, response)
return result, err
}
// AddFileComment will apply a comment to a specific file in a specific
// location
func (c *Change) AddFileComment(revision string, path string, line int, comment string) (*gerrit.ReviewResult, error) {
if revision == "" {
revision = DefaultRevision
}
logger := c.log.WithFields(log.Fields{
"phase": "add-file-comment",
"revision": revision,
"comment": comment,
})
comments := map[string][]gerrit.CommentInput{}
comments[path] = append(comments[path], gerrit.CommentInput{
Message: comment,
Line: line,
Side: "REVISION",
Range: gerrit.CommentRange{
StartLine: line,
EndLine: line,
},
})
result, response, err := c.api.Changes.SetReview(c.ChangeID, revision, &gerrit.ReviewInput{
Comments: comments,
Drafts: "PUBLISH_ALL_REVISIONS",
Notify: "NONE", // Don't send email
OmitDuplicateComments: true,
})
c.logError(err, logger, response)
return result, err
}