about summary refs log tree commit diff
path: root/gerrit/client.go
blob: 314f97281c7e6b1e8ad3d8bf67e174b439542655 (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package gerrit

import (
	"fmt"

	goGerrit "github.com/andygrunwald/go-gerrit"
	"github.com/apex/log"

	"net/url"
)

// passed to gerrit when retrieving changesets
var additionalFields = []string{
	"LABELS",
	"CURRENT_REVISION",
	"CURRENT_COMMIT",
	"DETAILED_ACCOUNTS",
	"SUBMITTABLE",
}

// IClient defines the gerrit.Client interface
type IClient interface {
	Refresh() error
	GetHEAD() string
	GetBaseURL() string
	GetChangesetURL(changeset *Changeset) string
	SubmitChangeset(changeset *Changeset) (*Changeset, error)
	RebaseChangeset(changeset *Changeset, ref string) (*Changeset, error)
	ChangesetIsRebasedOnHEAD(changeset *Changeset) bool
	SerieIsRebasedOnHEAD(serie *Serie) bool
	FilterSeries(filter func(s *Serie) bool) []*Serie
	FindSerie(filter func(s *Serie) bool) *Serie
}

var _ IClient = &Client{}

// Client provides some ways to interact with a gerrit instance
type Client struct {
	client      *goGerrit.Client
	logger      *log.Logger
	baseURL     string
	projectName string
	branchName  string
	series      []*Serie
	head        string
}

// NewClient initializes a new gerrit client
func NewClient(logger *log.Logger, URL, username, password, projectName, branchName string) (*Client, error) {
	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
	}
	return &Client{
		client:      goGerritClient,
		baseURL:     URL,
		logger:      logger,
		projectName: projectName,
		branchName:  branchName,
	}, nil
}

// 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
	}

	c.logger.Infof("assembling series…")
	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) {
	opt := &goGerrit.QueryChangeOptions{}
	opt.Query = []string{
		queryString,
	}
	opt.AdditionalFields = additionalFields
	changes, _, err := c.client.Changes.QueryChanges(opt)
	if err != nil {
		return nil, err
	}

	changesets = make([]*Changeset, 0)
	for _, change := range *changes {
		changesets = append(changesets, MakeChangeset(&change))
	}

	return changesets, nil
}

// fetchChangeset downloads an existing Changeset from gerrit, by its ID
// 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.
func (c *Client) fetchChangeset(changeID string) (*Changeset, error) {
	opt := goGerrit.ChangeOptions{}
	opt.AdditionalFields = []string{"LABELS", "DETAILED_ACCOUNTS"}
	changeInfo, _, err := c.client.Changes.GetChange(changeID, &opt)
	if err != nil {
		return nil, err
	}
	return MakeChangeset(changeInfo), nil
}

// SubmitChangeset submits a given changeset, and returns a changeset afterwards.
func (c *Client) SubmitChangeset(changeset *Changeset) (*Changeset, error) {
	changeInfo, _, err := c.client.Changes.SubmitChange(changeset.ChangeID, &goGerrit.SubmitInput{})
	if err != nil {
		return nil, err
	}
	c.head = changeInfo.CurrentRevision
	return c.fetchChangeset(changeInfo.ChangeID)
}

// RebaseChangeset rebases a given changeset on top of a given ref
func (c *Client) RebaseChangeset(changeset *Changeset, ref string) (*Changeset, error) {
	changeInfo, _, err := c.client.Changes.RebaseChange(changeset.ChangeID, &goGerrit.RebaseInput{
		Base: ref,
	})
	if err != nil {
		return changeset, err
	}
	return c.fetchChangeset(changeInfo.ChangeID)
}

// GetBaseURL returns the gerrit base URL
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
}