cmd/gerritbot: sync abandon message to PR
Fixes golang/go#28855
Change-Id: I769cca7a509a429fad1789a8984f62f05329196d
Reviewed-on: https://go-review.googlesource.com/c/150817
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/gerritbot/gerritbot.go b/cmd/gerritbot/gerritbot.go
index 56cee60..82dd1d7 100644
--- a/cmd/gerritbot/gerritbot.go
+++ b/cmd/gerritbot/gerritbot.go
@@ -513,7 +513,8 @@
// are available, the first closed change is returned.
func (b *bot) gerritChangeForPR(pr *github.PullRequest) (*gerrit.ChangeInfo, error) {
q := fmt.Sprintf(`"%s %s"`, prefixGitFooterPR, prShortLink(pr))
- cs, err := b.gerritClient.QueryChanges(context.Background(), q)
+ o := gerrit.QueryChangesOpt{Fields: []string{"MESSAGES"}}
+ cs, err := b.gerritClient.QueryChanges(context.Background(), q, o)
if err != nil {
return nil, fmt.Errorf("c.QueryChanges(ctx, %q): %v", q, err)
}
@@ -541,6 +542,12 @@
return fmt.Errorf("invalid status for closed Gerrit change: %q", ch.Status)
}
+ if ch.Status == gerrit.ChangeStatusAbandoned {
+ if reason := getAbandonReason(ch); reason != "" {
+ msg += "\n\n" + reason
+ }
+ }
+
repo := pr.GetBase().GetRepo()
if err := b.postGitHubMessageNoDup(ctx, repo.GetOwner().GetLogin(), repo.GetName(), pr.GetNumber(), msg); err != nil {
return fmt.Errorf("postGitHubMessageNoDup: %v", err)
@@ -558,6 +565,23 @@
return nil
}
+// getAbandonReason returns the last abandon reason in ch,
+// or the empty string if a reason doesn't exist.
+func getAbandonReason(ch *gerrit.ChangeInfo) string {
+ for i := len(ch.Messages) - 1; i >= 0; i-- {
+ msg := ch.Messages[i]
+ if msg.Tag != "autogenerated:gerrit:abandon" {
+ continue
+ }
+ if msg.Message == "Abandoned" {
+ // An abandon reason wasn't provided.
+ return ""
+ }
+ return strings.TrimPrefix(msg.Message, "Abandoned\n\n")
+ }
+ return ""
+}
+
// downloadRef calls the Gerrit API to retrieve the ref (such as refs/changes/16/81116/1)
// of the most recent patch set of the change with changeID.
func (b *bot) downloadRef(ctx context.Context, changeID string) (string, error) {
diff --git a/gerrit/gerrit.go b/gerrit/gerrit.go
index ebc3b77..1956720 100644
--- a/gerrit/gerrit.go
+++ b/gerrit/gerrit.go
@@ -296,6 +296,7 @@
Author *AccountInfo `json:"author"`
Time TimeStamp `json:"date"`
Message string `json:"message"`
+ Tag string `json:"tag,omitempty"`
RevisionNumber int `json:"_revision_number"`
}