2019-11-18 15:40:46 +01:00
|
|
|
package gerrit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
goGerrit "github.com/andygrunwald/go-gerrit"
|
2019-12-02 10:00:32 +01:00
|
|
|
"github.com/apex/log"
|
2019-11-18 15:40:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Changeset represents a single changeset
|
|
|
|
// Relationships between different changesets are described in Series
|
|
|
|
type Changeset struct {
|
|
|
|
changeInfo *goGerrit.ChangeInfo
|
|
|
|
ChangeID string
|
|
|
|
Number int
|
2019-12-02 10:00:32 +01:00
|
|
|
Verified int
|
|
|
|
CodeReviewed int
|
2021-12-09 11:49:16 +01:00
|
|
|
Autosubmit int
|
2019-12-03 10:16:32 +01:00
|
|
|
Submittable bool
|
2019-11-18 15:40:46 +01:00
|
|
|
CommitID string
|
|
|
|
ParentCommitIDs []string
|
|
|
|
OwnerName string
|
|
|
|
Subject string
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeChangeset creates a new Changeset object out of a goGerrit.ChangeInfo object
|
|
|
|
func MakeChangeset(changeInfo *goGerrit.ChangeInfo) *Changeset {
|
|
|
|
return &Changeset{
|
|
|
|
changeInfo: changeInfo,
|
|
|
|
ChangeID: changeInfo.ChangeID,
|
|
|
|
Number: changeInfo.Number,
|
2019-12-02 10:00:32 +01:00
|
|
|
Verified: labelInfoToInt(changeInfo.Labels["Verified"]),
|
|
|
|
CodeReviewed: labelInfoToInt(changeInfo.Labels["Code-Review"]),
|
2021-12-09 11:49:16 +01:00
|
|
|
Autosubmit: labelInfoToInt(changeInfo.Labels["Autosubmit"]),
|
2019-12-03 10:16:32 +01:00
|
|
|
Submittable: changeInfo.Submittable,
|
2019-11-18 15:40:46 +01:00
|
|
|
CommitID: changeInfo.CurrentRevision, // yes, this IS the commit ID.
|
|
|
|
ParentCommitIDs: getParentCommitIDs(changeInfo),
|
|
|
|
OwnerName: changeInfo.Owner.Name,
|
|
|
|
Subject: changeInfo.Subject,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 11:49:16 +01:00
|
|
|
// IsAutosubmit returns true if the changeset is intended to be
|
|
|
|
// automatically submitted by gerrit-queue.
|
|
|
|
//
|
|
|
|
// This is determined by the Change Owner setting +1 on the
|
|
|
|
// "Autosubmit" label.
|
|
|
|
func (c *Changeset) IsAutosubmit() bool {
|
|
|
|
return c.Autosubmit == 1
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
// IsVerified returns true if the changeset passed CI,
|
|
|
|
// that's when somebody left the Approved (+1) on the "Verified" label
|
|
|
|
func (c *Changeset) IsVerified() bool {
|
|
|
|
return c.Verified == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCodeReviewed returns true if the changeset passed code review,
|
|
|
|
// that's when somebody left the Recommended (+2) on the "Code-Review" label
|
|
|
|
func (c *Changeset) IsCodeReviewed() bool {
|
|
|
|
return c.CodeReviewed == 2
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:40:46 +01:00
|
|
|
func (c *Changeset) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("Changeset")
|
2019-12-03 10:16:32 +01:00
|
|
|
b.WriteString(fmt.Sprintf("(commitID: %.7s, author: %s, subject: %s, submittable: %v)",
|
|
|
|
c.CommitID, c.OwnerName, c.Subject, c.Submittable))
|
2019-11-18 15:40:46 +01:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterChangesets filters a list of Changeset by a given filter function
|
|
|
|
func FilterChangesets(changesets []*Changeset, f func(*Changeset) bool) []*Changeset {
|
|
|
|
newChangesets := make([]*Changeset, 0)
|
|
|
|
for _, changeset := range changesets {
|
|
|
|
if f(changeset) {
|
|
|
|
newChangesets = append(newChangesets, changeset)
|
|
|
|
} else {
|
|
|
|
log.WithField("changeset", changeset.String()).Debug("dropped by filter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newChangesets
|
|
|
|
}
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
// labelInfoToInt converts a goGerrit.LabelInfo to -2…+2 int
|
|
|
|
func labelInfoToInt(labelInfo goGerrit.LabelInfo) int {
|
|
|
|
if labelInfo.Recommended.AccountID != 0 {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
if labelInfo.Approved.AccountID != 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if labelInfo.Disliked.AccountID != 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if labelInfo.Rejected.AccountID != 0 {
|
|
|
|
return -2
|
|
|
|
}
|
|
|
|
return 0
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getParentCommitIDs returns the parent commit IDs of the goGerrit.ChangeInfo
|
|
|
|
// There is usually only one parent commit ID, except for merge commits.
|
|
|
|
func getParentCommitIDs(changeInfo *goGerrit.ChangeInfo) []string {
|
|
|
|
// obtain the RevisionInfo object
|
|
|
|
revisionInfo := changeInfo.Revisions[changeInfo.CurrentRevision]
|
|
|
|
|
|
|
|
// obtain the Commit object
|
|
|
|
commit := revisionInfo.Commit
|
|
|
|
|
|
|
|
commitIDs := make([]string, len(commit.Parents))
|
|
|
|
for i, commit := range commit.Parents {
|
|
|
|
commitIDs[i] = commit.Commit
|
|
|
|
}
|
|
|
|
return commitIDs
|
|
|
|
}
|