Skip to content

Commit

Permalink
cmd/gopherbot, maintner: add the author of the change to the notifica…
Browse files Browse the repository at this point in the history
…tion on github

When a new change is pushed, the bot posts a message on Github following this new format:
Change https://golang.org/cl/NNNN by AUTHOR mentions this issue

A new method to the CL's API in the maintner package is added
to provide the related absolute url.

Fixes #26816
  • Loading branch information
Ivan Palladino committed Jun 12, 2019
1 parent a3d123a commit 590a834
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cmd/gopherbot/gopherbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ func (b *gopherbot) closeStaleWaitingForInfo(ctx context.Context) error {

}

// cl2issue writes "Change https://golang.org/cl/NNNN mentions this issue"
// cl2issue writes "Change https://golang.org/cl/NNNN by AUTHOR mentions this issue"
// and the change summary on GitHub when a new Gerrit change references a GitHub issue.
func (b *gopherbot) cl2issue(ctx context.Context) error {
monthAgo := time.Now().Add(-30 * 24 * time.Hour)
Expand All @@ -915,6 +915,7 @@ func (b *gopherbot) cl2issue(ctx context.Context) error {
// already processed this issue.
return nil
}
prefix := fmt.Sprintf("Change %s", cl.URL().String())
for _, ref := range cl.GitHubIssueRefs {
if id := ref.Repo.ID(); id.Owner != "golang" || id.Repo != "go" {
continue
Expand All @@ -924,17 +925,17 @@ func (b *gopherbot) cl2issue(ctx context.Context) error {
continue
}
hasComment := false
substr := fmt.Sprintf("%d mentions this issue", cl.Number)
gi.ForeachComment(func(c *maintner.GitHubComment) error {
if strings.Contains(c.Body, substr) {
if strings.HasPrefix(c.Body, prefix) {
hasComment = true
return errStopIteration
}
return nil
})
if !hasComment {
printIssue("cl2issue", gi)
msg := fmt.Sprintf("Change https://golang.org/cl/%d mentions this issue: `%s`", cl.Number, cl.Commit.Summary())
author := cl.Owner().Name()
msg := fmt.Sprintf("%s by %s mentions this issue: `%s`", prefix, author, cl.Commit.Summary())
if err := b.addGitHubComment(ctx, "golang", "go", gi.Number, msg); err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions maintner/gerrit.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,16 @@ func (cl *GerritCL) CommitAtVersion(version int32) *GitCommit {
return cl.Project.commit[hash]
}

// URL returns the absolute url of the CL.
func (cl *GerritCL) URL() *url.URL {
s := fmt.Sprintf("https://golang.org/cl/%d", cl.Number)
u, err := url.Parse(s)
if err != nil {
return &url.URL{}
}
return u
}

func (cl *GerritCL) updateGithubIssueRefs() {
gp := cl.Project
gerrit := gp.gerrit
Expand Down
17 changes: 17 additions & 0 deletions maintner/gerrit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,20 @@ func TestGerritHashtagsLen(t *testing.T) {
}
}
}

func TestGerritURL(t *testing.T) {
tests := []struct {
set int32
want string
}{
{0, "https://golang.org/cl/0"},
{1238812784, "https://golang.org/cl/1238812784"},
}
for _, tt := range tests {
cl := &GerritCL{Number: tt.set}
got := cl.URL().String()
if got != tt.want {
t.Errorf("For set %d, url = %#v; want %v", tt.set, got, tt.want)
}
}
}

0 comments on commit 590a834

Please sign in to comment.