Skip to content

Commit 6d27e14

Browse files
committed
cmd/gerritbot: leave a CL comment on a freshly imported PR to help confirm author has a Gerrit account
1 parent 2312c11 commit 6d27e14

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

cmd/gerritbot/gerritbot.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ func (b *bot) importGerritChangeFromPR(ctx context.Context, pr *github.PullReque
729729
pushOpts = "%ready"
730730
}
731731

732-
if cl == nil {
732+
newCL := cl == nil
733+
if newCL {
733734
// Add this informational message only on CL creation.
734735
msg := fmt.Sprintf("This Gerrit CL corresponds to GitHub PR %s.\n\nAuthor: %s", prShortLink(pr), author)
735736
pushOpts += ",m=" + url.QueryEscape(msg)
@@ -754,7 +755,49 @@ Please visit %s to see it.
754755
Tip: You can toggle comments from me using the %s slash command (e.g. %s)
755756
See the [Uncyclo page](https://golang.org/wiki/GerritBot) for more info`,
756757
pr.Head.GetSHA(), changeURL, "`comments`", "`/comments off`")
757-
return b.postGitHubMessageNoDup(ctx, repo.GetOwner().GetLogin(), repo.GetName(), pr.GetNumber(), "", msg)
758+
759+
err = b.postGitHubMessageNoDup(ctx, repo.GetOwner().GetLogin(), repo.GetName(), pr.GetNumber(), "", msg)
760+
if err != nil {
761+
return err
762+
}
763+
764+
if newCL {
765+
// Add an unresolved welcome comment to Gerrit to provide some context to the PR author
766+
// but also to help a reviewer see the author has registered for a Gerrit account,
767+
// knows how to reply in Gerrit, and thinks the CL is ready for review.
768+
// TODO: consider a reminder comment with reply hints if they don't reply after something like 7 days.
769+
// TODO: consider abandoning the CL if no reply after something like 30 days.
770+
gcl, err := b.gerritChangeForPR(pr)
771+
if err != nil {
772+
return fmt.Errorf("could not look up CL after creation: %v", err)
773+
}
774+
question := fmt.Sprintf(`Hello %v, is this CL ready for review?
775+
776+
If so, please mark this comment as 'Done'. If not, please write a reply to this comment.
777+
778+
(In Gerrit code reviews, the CL author is expected to close out each piece of feedback by
779+
marking it as 'Done' if implemented as suggested or otherwise reply to each review comment.
780+
See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)`,
781+
author)
782+
783+
unresolved := true
784+
ri := gerrit.ReviewInput{
785+
Comments: map[string][]gerrit.CommentInput{
786+
"/PATCHSET_LEVEL": {{Message: question, Unresolved: &unresolved}},
787+
},
788+
}
789+
// TODO: might be able to use gcl.ID instead of genChangeTriple? Note gcl.ID says it is URL encoded.
790+
revision, err := genChangeTriple(gcl)
791+
if err != nil {
792+
return fmt.Errorf("cannot add welcome comment to CL: %v", err)
793+
}
794+
err = b.gerritClient.SetReview(ctx, gcl.ChangeID, revision, ri)
795+
if err != nil {
796+
return fmt.Errorf("could not add welcome comment to CL: %v", err)
797+
}
798+
}
799+
800+
return nil
758801
}
759802

760803
var changeIdentRE = regexp.MustCompile(`(?m)^Change-Id: (I[0-9a-fA-F]{40})\n?`)
@@ -814,6 +857,17 @@ func genChangeID(pr *github.PullRequest) string {
814857
return fmt.Sprintf("I%x", sha1.Sum(buf.Bytes()))
815858
}
816859

860+
// genChangeTriple returns the Gerrit (project, branch, change-ID) triple
861+
// uniquely identifying this change.
862+
func genChangeTriple(gcl *gerrit.ChangeInfo) (string, error) {
863+
// TODO: this is modified from cmd/coordinator. Consider consolidating.
864+
if gcl.Project == "" || gcl.Branch == "" || gcl.ChangeID == "" {
865+
return "", fmt.Errorf("cannot generate change triple: gcl.Project: %q, gcl.Branch: %q, gcl.ChangeID: %q",
866+
gcl.Project, gcl.Branch, gcl.ChangeID)
867+
}
868+
return fmt.Sprintf("%s~%s~%s", gcl.Project, gcl.Branch, gcl.ChangeID), nil
869+
}
870+
817871
func cmdOut(cmd *exec.Cmd) (string, error) {
818872
log.Printf("Executing %v", cmd.Args)
819873
out, err := cmd.CombinedOutput()

0 commit comments

Comments
 (0)