cmd/gopherbot: skip commenting on nonexistent issues

When an issue is transferred, we incorrectly continue trying to manage
the issue with Gopherbot, even though we should be able to pick up the
new issue. As a temporary fix, this change skips issues which return a
404 when trying to comment.

For golang/go#40640

Change-Id: I9280fab7a8ec4ead0d3e1cc5695d9b7eb39d6b32
Reviewed-on: https://go-review.googlesource.com/c/build/+/247409
Run-TryBot: Alexander Rakoczy <alex@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
diff --git a/cmd/gopherbot/gopherbot.go b/cmd/gopherbot/gopherbot.go
index 041de03..ab9e30f 100644
--- a/cmd/gopherbot/gopherbot.go
+++ b/cmd/gopherbot/gopherbot.go
@@ -508,11 +508,17 @@
 	}
 	// See if there is a dup comment from when gopherbot last got
 	// its data from maintner.
-	ics, _, err := b.ghc.Issues.ListComments(ctx, repo.ID().Owner, repo.ID().Repo, int(issueNum), &github.IssueListCommentsOptions{
+	ics, resp, err := b.ghc.Issues.ListComments(ctx, repo.ID().Owner, repo.ID().Repo, int(issueNum), &github.IssueListCommentsOptions{
 		Since:       since,
 		ListOptions: github.ListOptions{PerPage: 1000},
 	})
 	if err != nil {
+		// TODO(golang/go#40640) - This issue was transferred or otherwise is gone. We should permanently skip it. This
+		// is a temporary fix to keep gopherbot working.
+		if resp != nil && resp.StatusCode == http.StatusNotFound {
+			log.Printf("addGitHubComment: Issue %v#%v returned a 404 when trying to load comments. Skipping. See golang/go#40640.", repo.ID(), issueNum)
+			return nil
+		}
 		return err
 	}
 	for _, ic := range ics {
@@ -1021,7 +1027,7 @@
 			// TODO: write a task that reopens issues if the OP speaks up.
 			if err := b.addGitHubComment(ctx, repo, gi.Number,
 				"Timed out in state WaitingForInfo. Closing.\n\n(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)"); err != nil {
-				return err
+				return fmt.Errorf("b.addGitHubComment(_, %v, %v) = %w", repo.ID(), gi.Number, err)
 			}
 			return b.closeGitHubIssue(ctx, repo.ID(), gi.Number)
 		})