Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-unpark-reply-own-login.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A hive no longer answers the same `/hive` command on every sweep when it posts as a GitHub login its configured identity does not name. The un-park sweep only believes its own `hive:unpark-reply` / `hive:label-approval` markers when the comment's author is one of the hive's accounts, and an App installed under its own slug with no `github.app_slug` set falls back to the public default slug, so the hive's real `<slug>[bot]` was never recognised: each sweep saw the command as unanswered and replied again (one tuna-os hive posted 320+ identical replies on each of four issues, 40-58 a day). The client now records the author GitHub returns when it creates an un-park reply, notice or label-approval comment, which is by definition its own account, and treats that login as its own for marker checks. A marker pasted by any other account still does not count.
24 changes: 24 additions & 0 deletions src/pkg/github/bot_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"strings"
"time"

gh "github.com/google/go-github/v72/github"
)

// botUserLookupTimeout bounds the one-off bot user lookup behind
Expand Down Expand Up @@ -138,3 +140,25 @@ func (c *Client) cachedSelfLogin() string {
defer c.selfLoginMu.Unlock()
return c.selfLogin
}

// learnSelfLogin records the author of a comment this client just created.
// GitHub answers a create with the comment, so its author is the account the
// client's token posts as: known even for an App installation token, which
// cannot call GET /user, and even when the configured identity names a
// different App slug. Without it, a hive posting under an unconfigured login
// never recognised its own once-only markers and answered the same comment on
// every sweep.
func (c *Client) learnSelfLogin(created *gh.IssueComment) {
if c == nil || created == nil {
return
}
login := strings.TrimSpace(created.GetUser().GetLogin())
if login == "" {
return
}
c.selfLoginMu.Lock()
defer c.selfLoginMu.Unlock()
if c.selfLogin == "" {
c.selfLogin = login
}
}
12 changes: 9 additions & 3 deletions src/pkg/github/issue_unpark_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,11 @@ func (c *Client) recordLabelApproval(event IssueLabelApprovalEvent, la unparkLab

func (c *Client) postLabelApprovalReply(ctx context.Context, owner, repo string, number int, eventID int64, body string) error {
marked := labelApprovalMarker(eventID) + "\n" + body
if _, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(marked)}); err != nil {
created, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(marked)})
if err != nil {
return fmt.Errorf("replying to label approval on %s/%s#%d: %w", owner, repo, number, err)
}
c.learnSelfLogin(created)
return nil
}

Expand Down Expand Up @@ -1327,19 +1329,23 @@ func (c *Client) ensureUnparkNoticeComment(ctx context.Context, owner, repo stri
}
return nil
}
if _, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(desired)}); err != nil {
created, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(desired)})
if err != nil {
return fmt.Errorf("creating unpark notice on %s/%s#%d: %w", owner, repo, number, err)
}
c.learnSelfLogin(created)
return nil
}

// postUnparkReply answers one command comment exactly once; the marker carries
// the answered comment's ID so a later cycle recognises its own work.
func (c *Client) postUnparkReply(ctx context.Context, owner, repo string, number int, commentID int64, body string) error {
marked := fmt.Sprintf("%s%d -->\n%s", unparkReplyMarkerPrefix, commentID, body)
if _, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(marked)}); err != nil {
created, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(marked)})
if err != nil {
return fmt.Errorf("replying to unpark command on %s/%s#%d: %w", owner, repo, number, err)
}
c.learnSelfLogin(created)
return nil
}

Expand Down
48 changes: 47 additions & 1 deletion src/pkg/github/issue_unpark_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ type unparkServer struct {
// replyAfterPost maps issue number to comments that appear in its
// listing once a comment is POSTed on it (e.g. Prow's reply).
replyAfterPost map[int][]unparkWireComment
// postAuthor, when set, is the account GitHub reports as the author of
// every comment POSTed to the server: the response names it, and the
// comment shows up in later listings under it, as on GitHub.
postAuthor string
// permissions maps login to the permission level the API reports.
permissions map[string]string
// roles maps login to the role_name the permission endpoint reports
Expand Down Expand Up @@ -214,7 +218,20 @@ func newUnparkServer(t *testing.T, owner, repo string, issues []unparkWireIssue,
iss.Comments = append(append([]unparkWireComment(nil), iss.Comments...), extra...)
byNum[n] = iss
}
_ = json.NewEncoder(w).Encode(map[string]any{"id": 9000 + len(rec.posted[n])})
resp := map[string]any{"id": 9000 + len(rec.posted[n])}
if rec.postAuthor != "" {
resp["user"] = wireUser{Login: rec.postAuthor}
iss := byNum[n]
iss.Comments = append(append([]unparkWireComment(nil), iss.Comments...), unparkWireComment{
ID: int64(9000 + len(rec.posted[n])),
Body: body.Body,
User: wireUser{Login: rec.postAuthor},
CreatedAt: unparkNow(),
UpdatedAt: unparkNow(),
})
byNum[n] = iss
}
_ = json.NewEncoder(w).Encode(resp)

case strings.HasPrefix(path, "issues/comments/") && r.Method == http.MethodPatch:
var id int64
Expand Down Expand Up @@ -750,6 +767,35 @@ func TestSweepIssueUnparkCommandsHelpAndProseDoNotUnpark(t *testing.T) {
}
}

// TestSweepIssueUnparkCommandsAnswersOnceWhenPostingAsUnconfiguredLogin covers
// a reply flood seen on a live hive: the configured identity named one App
// bot login while the hive actually posted as another (an App installed under
// its own slug with no github.app_slug set falls back to the public default
// slug). Its unpark-reply marker then never counted as its own, so every
// sweep answered the same command again — 40-58 identical replies a day.
func TestSweepIssueUnparkCommandsAnswersOnceWhenPostingAsUnconfiguredLogin(t *testing.T) {
issues := []unparkWireIssue{parkedIssue(7, humanComment(11, "maintainer", "/hive help"))}
server, rec := newUnparkServer(t, "org", "repo", issues, map[string]string{"maintainer": "admin"})
rec.postAuthor = "acme-hive[bot]"
c := newTestClient(t, server, "org", []string{"repo"})
c.SetHiveIdentity(HiveIdentity{AppLogin: "hivecommons-hive[bot]"})

for sweep := 1; sweep <= 3; sweep++ {
if _, err := c.SweepIssueUnparkCommands(context.Background(), IssueUnparkSweepOptions{}); err != nil {
t.Fatalf("sweep %d: %v", sweep, err)
}
}
var replies int
for _, body := range rec.posted[7] {
if strings.Contains(body, unparkReplyMarkerPrefix+"11 -->") {
replies++
}
}
if replies != 1 {
t.Fatalf("command 11 answered %d times over 3 sweeps, want once; posted=%v", replies, rec.posted[7])
}
}

func TestSweepIssueUnparkCommandsSkipsUnparkedIssues(t *testing.T) {
open := unparkWireIssue{
Number: 13,
Expand Down
Loading