From 348ae0649aca26195b3851fc6f37a34743636a91 Mon Sep 17 00:00:00 2001 From: James Reilly Date: Tue, 10 Nov 2026 03:44:46 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(github):=20recognise=20the?= =?UTF-8?q?=20hive's=20own=20unpark=20replies=20when=20it=20posts=20as=20a?= =?UTF-8?q?n=20unconfigured=20login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The un-park sweep only believes a hive:unpark-reply / hive:label-approval marker when the comment's author is one of the hive's own accounts (markerAuthorTrusted, hivecommons/hive#11313). 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 [bot] login was never among those accounts: every sweep saw the command as unanswered and replied again. A live tuna-os hive posted 320+ identical replies on each of four issues (40-58 a day) this way. Record the author GitHub returns when the client creates an un-park reply, notice or label-approval comment. That author is by definition the account the token posts as, so isHiveAccount now matches it through the existing cachedSelfLogin check. A marker pasted by any other account still does not count. Signed-off-by: James Reilly --- changelog.d/fixed-unpark-reply-own-login.md | 1 + src/pkg/github/bot_user.go | 24 +++++++++++ src/pkg/github/issue_unpark_command.go | 12 ++++-- src/pkg/github/issue_unpark_command_test.go | 48 ++++++++++++++++++++- 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 changelog.d/fixed-unpark-reply-own-login.md diff --git a/changelog.d/fixed-unpark-reply-own-login.md b/changelog.d/fixed-unpark-reply-own-login.md new file mode 100644 index 0000000000..70e4d62d4a --- /dev/null +++ b/changelog.d/fixed-unpark-reply-own-login.md @@ -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 `[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. diff --git a/src/pkg/github/bot_user.go b/src/pkg/github/bot_user.go index 2b8602f33e..7e7ab5c7c7 100644 --- a/src/pkg/github/bot_user.go +++ b/src/pkg/github/bot_user.go @@ -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 @@ -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 + } +} diff --git a/src/pkg/github/issue_unpark_command.go b/src/pkg/github/issue_unpark_command.go index 65b94b17fe..aee1502a06 100644 --- a/src/pkg/github/issue_unpark_command.go +++ b/src/pkg/github/issue_unpark_command.go @@ -620,9 +620,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 } @@ -1199,9 +1201,11 @@ 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 } @@ -1209,9 +1213,11 @@ func (c *Client) ensureUnparkNoticeComment(ctx context.Context, owner, repo stri // 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 } diff --git a/src/pkg/github/issue_unpark_command_test.go b/src/pkg/github/issue_unpark_command_test.go index 7106afd63c..50aec24cac 100644 --- a/src/pkg/github/issue_unpark_command_test.go +++ b/src/pkg/github/issue_unpark_command_test.go @@ -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 @@ -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 @@ -656,6 +673,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,