Skip to content

Commit 6a5da5b

Browse files
dmitshurgopherbot
authored andcommitted
gerrit: delete GetProjects in favor of ListProjects
The ListProjects and GetProjects methods were added in CL 38717 and CL 37252 at around the same time, and they're both exposing the same GET /projects/ Gerrit API endpoint. The latter is hardcoded to provide the 'b' query parameter, one of many that GET /projects/ offers. Rewrite cmd/gitmirror and cmd/updatestd to use ListProjects and N calls to GetBranch instead, and delete GetProjects since we no longer use it. I considered deprecating GetProjects instead of deleting, but x/build and x/build/gerrit specifically are both documented to be for internal Go project needs and not subject to the Go 1 compatibility promise, so skip the overhead of doing that. If it happens to cause a problem that can't be worked around in a better way, we'll consider re-adding it in deprecated form. Add a pkg.go.dev link to increase visibility to the general-purpose Gerrit API clients written in Go. Updates golang/go#18743. Change-Id: I583356526af04e915bce5464004eb68a650221d5 Reviewed-on: https://go-review.googlesource.com/c/build/+/518676 Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 1729064 commit 6a5da5b

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

cmd/gitmirror/gitmirror.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package main
1111
import (
1212
"bytes"
1313
"context"
14+
"errors"
1415
"flag"
1516
"fmt"
1617
"io/ioutil"
@@ -696,15 +697,19 @@ func (m *gitMirror) subscribeToMaintnerAndTickle() error {
696697
func (m *gitMirror) gerritMetaMap() (map[string]string, error) {
697698
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
698699
defer cancel()
699-
meta, err := m.gerritClient.GetProjects(ctx, "master")
700+
projs, err := m.gerritClient.ListProjects(ctx)
700701
if err != nil {
701-
return nil, fmt.Errorf("gerritClient.GetProjects: %v", err)
702+
return nil, fmt.Errorf("gerritClient.ListProjects: %v", err)
702703
}
703704
result := map[string]string{}
704-
for repo, v := range meta {
705-
if master, ok := v.Branches["master"]; ok {
706-
result[repo] = master
705+
for _, p := range projs {
706+
b, err := m.gerritClient.GetBranch(ctx, p.Name, "master")
707+
if errors.Is(err, gerrit.ErrResourceNotExist) {
708+
continue
709+
} else if err != nil {
710+
return nil, fmt.Errorf(`gerritClient.GetBranch(ctx, %q, "master"): %v`, p.Name, err)
707711
}
712+
result[p.Name] = b.Revision
708713
}
709714
return result, nil
710715
}

cmd/updatestd/updatestd.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func main() {
7070
// Fetch latest hashes of Go projects from Gerrit,
7171
// using the specified branch name.
7272
//
73-
// This gives us a consistent snapshot of all golang.org/x module versions
73+
// We get a fairly consistent snapshot of all golang.org/x module versions
7474
// at a given point in time. This ensures selection of latest available
7575
// pseudo-versions is done without being subject to module mirror caching,
7676
// and that selected pseudo-versions can be re-used across multiple modules.
@@ -79,18 +79,19 @@ func main() {
7979
// commits that are selected and reporting if any of them have a failure.
8080
//
8181
cl := gerrit.NewClient("https://go-review.googlesource.com", gerrit.NoAuth)
82-
projs, err := cl.GetProjects(context.Background(), *branch)
82+
projs, err := cl.ListProjects(context.Background())
8383
if err != nil {
8484
log.Fatalln("failed to get a list of Gerrit projects:", err)
8585
}
8686
hashes := map[string]string{}
87-
for name, p := range projs {
88-
if p.State != "ACTIVE" {
87+
for _, p := range projs {
88+
b, err := cl.GetBranch(context.Background(), p.Name, *branch)
89+
if errors.Is(err, gerrit.ErrResourceNotExist) {
8990
continue
91+
} else if err != nil {
92+
log.Fatalf("failed to get the %q branch of Gerrit project %q: %v\n", *branch, p.Name, err)
9093
}
91-
if hash, ok := p.Branches[*branch]; ok {
92-
hashes[name] = hash
93-
}
94+
hashes[p.Name] = b.Revision
9495
}
9596

9697
w := Work{

gerrit/gerrit.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
// Package gerrit contains code to interact with Gerrit servers.
66
//
7-
// The API is not subject to the Go 1 compatibility promise and may change at
8-
// any time.
7+
// This package doesn't intend to provide complete coverage of the Gerrit API,
8+
// but only a small subset for the current needs of the Go project infrastructure.
9+
// Its API is not subject to the Go 1 compatibility promise and may change at any time.
10+
// For general-purpose Gerrit API clients, see https://pkg.go.dev/search?q=gerrit.
911
package gerrit
1012

1113
import (
@@ -732,7 +734,7 @@ type ProjectInfo struct {
732734
//
733735
// The returned slice is sorted by project ID and excludes the "All-Projects" and "All-Users" projects.
734736
//
735-
// See https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#list-projects
737+
// See https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#list-projects.
736738
func (c *Client) ListProjects(ctx context.Context) ([]ProjectInfo, error) {
737739
var res map[string]ProjectInfo
738740
err := c.do(ctx, &res, "GET", "/projects/")
@@ -1013,13 +1015,6 @@ func (c *Client) QueryAccounts(ctx context.Context, q string, opts ...QueryAccou
10131015
return changes, err
10141016
}
10151017

1016-
// GetProjects returns a map of all projects on the Gerrit server.
1017-
func (c *Client) GetProjects(ctx context.Context, branch string) (map[string]*ProjectInfo, error) {
1018-
mp := make(map[string]*ProjectInfo)
1019-
err := c.do(ctx, &mp, "GET", fmt.Sprintf("/projects/?b=%s&format=JSON", branch))
1020-
return mp, err
1021-
}
1022-
10231018
type TimeStamp time.Time
10241019

10251020
func (ts TimeStamp) Equal(v TimeStamp) bool {

0 commit comments

Comments
 (0)