2019-11-18 15:40:46 +01:00
|
|
|
package gerrit
|
|
|
|
|
|
|
|
import (
|
2019-12-02 10:00:32 +01:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-18 15:40:46 +01:00
|
|
|
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
|
|
|
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// passed to gerrit when retrieving changesets
|
2019-12-02 10:00:32 +01:00
|
|
|
var additionalFields = []string{
|
|
|
|
"LABELS",
|
|
|
|
"CURRENT_REVISION",
|
|
|
|
"CURRENT_COMMIT",
|
|
|
|
"DETAILED_ACCOUNTS",
|
2019-12-03 10:16:32 +01:00
|
|
|
"SUBMITTABLE",
|
2019-12-02 10:00:32 +01:00
|
|
|
}
|
2019-11-18 15:40:46 +01:00
|
|
|
|
|
|
|
// IClient defines the gerrit.Client interface
|
|
|
|
type IClient interface {
|
2019-12-02 10:00:32 +01:00
|
|
|
Refresh() error
|
|
|
|
GetHEAD() string
|
|
|
|
GetBaseURL() string
|
|
|
|
GetChangesetURL(changeset *Changeset) string
|
2019-11-18 15:40:46 +01:00
|
|
|
SubmitChangeset(changeset *Changeset) (*Changeset, error)
|
|
|
|
RebaseChangeset(changeset *Changeset, ref string) (*Changeset, error)
|
|
|
|
RemoveTag(changeset *Changeset, tag string) (*Changeset, error)
|
2019-12-02 10:00:32 +01:00
|
|
|
ChangesetIsRebasedOnHEAD(changeset *Changeset) bool
|
|
|
|
SerieIsRebasedOnHEAD(serie *Serie) bool
|
|
|
|
FilterSeries(filter func(s *Serie) bool) []*Serie
|
|
|
|
FindSerie(filter func(s *Serie) bool) *Serie
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ IClient = &Client{}
|
|
|
|
|
|
|
|
// Client provides some ways to interact with a gerrit instance
|
|
|
|
type Client struct {
|
2019-12-02 10:00:32 +01:00
|
|
|
client *goGerrit.Client
|
|
|
|
logger *log.Logger
|
|
|
|
baseURL string
|
|
|
|
projectName string
|
|
|
|
branchName string
|
|
|
|
series []*Serie
|
|
|
|
head string
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient initializes a new gerrit client
|
2019-12-02 10:00:32 +01:00
|
|
|
func NewClient(logger *log.Logger, URL, username, password, projectName, branchName string) (*Client, error) {
|
2019-11-18 15:40:46 +01:00
|
|
|
urlParsed, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
urlParsed.User = url.UserPassword(username, password)
|
|
|
|
|
|
|
|
goGerritClient, err := goGerrit.NewClient(urlParsed.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-21 16:12:04 +01:00
|
|
|
return &Client{
|
2019-12-02 14:38:23 +01:00
|
|
|
client: goGerritClient,
|
|
|
|
baseURL: URL,
|
|
|
|
logger: logger,
|
|
|
|
projectName: projectName,
|
|
|
|
branchName: branchName,
|
2019-11-21 16:12:04 +01:00
|
|
|
}, nil
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
// refreshHEAD queries the commit ID of the selected project and branch
|
|
|
|
func (c *Client) refreshHEAD() (string, error) {
|
|
|
|
branchInfo, _, err := c.client.Projects.GetBranch(c.projectName, c.branchName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return branchInfo.Revision, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHEAD returns the internally stored HEAD
|
|
|
|
func (c *Client) GetHEAD() string {
|
|
|
|
return c.head
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh causes the client to refresh internal view of gerrit
|
|
|
|
func (c *Client) Refresh() error {
|
|
|
|
c.logger.Debug("refreshing from gerrit")
|
|
|
|
HEAD, err := c.refreshHEAD()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.head = HEAD
|
|
|
|
|
|
|
|
var queryString = fmt.Sprintf("status:open project:%s branch:%s", c.projectName, c.branchName)
|
|
|
|
c.logger.Debugf("fetching changesets: %s", queryString)
|
|
|
|
changesets, err := c.fetchChangesets(queryString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-04 15:50:15 +01:00
|
|
|
c.logger.Infof("assembling series…")
|
2019-12-02 10:00:32 +01:00
|
|
|
series, err := AssembleSeries(changesets, c.logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
series = SortSeries(series)
|
|
|
|
c.series = series
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchChangesets fetches a list of changesets matching a passed query string
|
|
|
|
func (c *Client) fetchChangesets(queryString string) (changesets []*Changeset, Error error) {
|
2019-11-18 15:40:46 +01:00
|
|
|
opt := &goGerrit.QueryChangeOptions{}
|
|
|
|
opt.Query = []string{
|
|
|
|
queryString,
|
|
|
|
}
|
2019-12-02 10:00:32 +01:00
|
|
|
opt.AdditionalFields = additionalFields
|
|
|
|
changes, _, err := c.client.Changes.QueryChanges(opt)
|
2019-11-18 15:40:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
changesets = make([]*Changeset, 0)
|
|
|
|
for _, change := range *changes {
|
|
|
|
changesets = append(changesets, MakeChangeset(&change))
|
|
|
|
}
|
|
|
|
|
|
|
|
return changesets, nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
// fetchChangeset downloads an existing Changeset from gerrit, by its ID
|
2019-11-18 15:40:46 +01:00
|
|
|
// Gerrit's API is a bit sparse, and only returns what you explicitly ask it
|
|
|
|
// This is used to refresh an existing changeset with more data.
|
2019-12-02 10:00:32 +01:00
|
|
|
func (c *Client) fetchChangeset(changeID string) (*Changeset, error) {
|
2019-11-18 15:40:46 +01:00
|
|
|
opt := goGerrit.ChangeOptions{}
|
|
|
|
opt.AdditionalFields = []string{"LABELS", "DETAILED_ACCOUNTS"}
|
2019-12-02 10:00:32 +01:00
|
|
|
changeInfo, _, err := c.client.Changes.GetChange(changeID, &opt)
|
2019-11-18 15:40:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return MakeChangeset(changeInfo), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitChangeset submits a given changeset, and returns a changeset afterwards.
|
2019-12-02 10:00:32 +01:00
|
|
|
func (c *Client) SubmitChangeset(changeset *Changeset) (*Changeset, error) {
|
|
|
|
changeInfo, _, err := c.client.Changes.SubmitChange(changeset.ChangeID, &goGerrit.SubmitInput{})
|
2019-11-18 15:40:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-04 11:42:23 +01:00
|
|
|
c.head = changeInfo.CurrentRevision
|
2019-12-02 10:00:32 +01:00
|
|
|
return c.fetchChangeset(changeInfo.ChangeID)
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RebaseChangeset rebases a given changeset on top of a given ref
|
2019-12-02 10:00:32 +01:00
|
|
|
func (c *Client) RebaseChangeset(changeset *Changeset, ref string) (*Changeset, error) {
|
|
|
|
changeInfo, _, err := c.client.Changes.RebaseChange(changeset.ChangeID, &goGerrit.RebaseInput{
|
2019-11-18 15:40:46 +01:00
|
|
|
Base: ref,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return changeset, err
|
|
|
|
}
|
2019-12-02 10:00:32 +01:00
|
|
|
return c.fetchChangeset(changeInfo.ChangeID)
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTag removes the submit queue tag from a changeset and updates gerrit
|
|
|
|
// we never add, that's something users should do in the GUI.
|
2019-12-02 10:00:32 +01:00
|
|
|
func (c *Client) RemoveTag(changeset *Changeset, tag string) (*Changeset, error) {
|
2019-11-18 15:40:46 +01:00
|
|
|
hashTags := changeset.HashTags
|
|
|
|
newHashTags := []string{}
|
|
|
|
for _, hashTag := range hashTags {
|
|
|
|
if hashTag != tag {
|
|
|
|
newHashTags = append(newHashTags, hashTag)
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 10:00:32 +01:00
|
|
|
// TODO: implement setting hashtags api in go-gerrit and use here
|
2019-11-18 15:40:46 +01:00
|
|
|
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#set-hashtags
|
|
|
|
return changeset, nil
|
|
|
|
}
|
2019-11-21 16:12:04 +01:00
|
|
|
|
|
|
|
// GetBaseURL returns the gerrit base URL
|
2019-12-02 10:00:32 +01:00
|
|
|
func (c *Client) GetBaseURL() string {
|
|
|
|
return c.baseURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProjectName returns the configured gerrit project name
|
|
|
|
func (c *Client) GetProjectName() string {
|
|
|
|
return c.projectName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranchName returns the configured gerrit branch name
|
|
|
|
func (c *Client) GetBranchName() string {
|
|
|
|
return c.branchName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChangesetURL returns the URL to view a given changeset
|
|
|
|
func (c *Client) GetChangesetURL(changeset *Changeset) string {
|
|
|
|
return fmt.Sprintf("%s/c/%s/+/%d", c.GetBaseURL(), c.projectName, changeset.Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangesetIsRebasedOnHEAD returns true if the changeset is rebased on the current HEAD
|
|
|
|
func (c *Client) ChangesetIsRebasedOnHEAD(changeset *Changeset) bool {
|
|
|
|
if len(changeset.ParentCommitIDs) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return changeset.ParentCommitIDs[0] == c.head
|
|
|
|
}
|
|
|
|
|
|
|
|
// SerieIsRebasedOnHEAD returns true if the whole series is rebased on the current HEAD
|
|
|
|
// this is already the case if the first changeset in the series is rebased on the current HEAD
|
|
|
|
func (c *Client) SerieIsRebasedOnHEAD(serie *Serie) bool {
|
|
|
|
// an empty serie should not exist
|
|
|
|
if len(serie.ChangeSets) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return c.ChangesetIsRebasedOnHEAD(serie.ChangeSets[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterSeries returns a subset of all Series, passing the given filter function
|
|
|
|
func (c *Client) FilterSeries(filter func(s *Serie) bool) []*Serie {
|
|
|
|
matchedSeries := []*Serie{}
|
|
|
|
for _, serie := range c.series {
|
|
|
|
if filter(serie) {
|
|
|
|
matchedSeries = append(matchedSeries, serie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matchedSeries
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindSerie returns the first serie that matches the filter, or nil if none was found
|
|
|
|
func (c *Client) FindSerie(filter func(s *Serie) bool) *Serie {
|
|
|
|
for _, serie := range c.series {
|
|
|
|
if filter(serie) {
|
|
|
|
return serie
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-11-21 16:12:04 +01:00
|
|
|
}
|