Skip to content
Merged
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-17474-prow-ok-to-test-fork-prs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The contributor lane now posts `/ok-to-test` once per head on a fork PR it admitted whose author is not an org member or collaborator, when the repository is Prow-managed, the PR has no `ok-to-test` label and Prow has reported no presubmit on the head ([#17474](https://github.com/hivecommons/hive/issues/17474)). Previously Prow's trigger plugin never ran its presubmits on such a PR, so Tide reported `Jobs … have not succeeded` on a head that was green on GitHub Actions until a person posted the command (#17359 sat about four hours). If Prow rejects the App (`Cannot trigger testing until a trusted user`), the lane logs `skipped=prow-rejects-bot-commands` once, leaves one needs-human note and stops posting on that PR.
8 changes: 6 additions & 2 deletions src/docs/fork-pr-presubmit-stall-runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ gh pr view <n> --repo <org>/<repo> --comments | grep -i -E "tide|not mergeable|r

## 2. Start the presubmits

The contributor lane now posts `/ok-to-test` itself, once per head, on a fork PR it admitted whose author is not an org member or collaborator ([#17474](https://github.com/hivecommons/hive/issues/17474); see [hive-merge.md](hive-merge.md)). This step is needed only when that did not help: the PR carries the hive's `needs-human` note saying Prow rejected the App (`Cannot trigger testing until a trusted user`), or the lane did not admit the PR.

A human with write access comments on the PR. This is Prow's `/ok-to-test` (trigger plugin, shows as Tide `Jobs X, Y have not succeeded`), not the same-repo `hive-ok-to-test` GitHub Actions workflow (shows as `workflow-approval-required:<ctx>`; see [github-app-setup.md](github-app-setup.md)), which never covers fork heads:

```text
Expand All @@ -43,8 +45,10 @@ ask that owner to comment `/approve`.

## 4. Permanent fixes (not done by this runbook)

- Post `/ok-to-test` once when the hive opens or pushes to a fork PR for a
non-member agent, or add contributor agents to the Prow trusted org or team.
- Done in #17474: the contributor lane posts `/ok-to-test` once per head on an
admitted non-member fork PR. Making the hive App (or contributor agents) a
Prow trusted user remains a Prow configuration choice; until then Prow
rejects the App's command and step 2 applies.
- Relay `/lgtm` and `/approve` with the approving human's token, or add the
hive App to `OWNERS`; surface Prow's rejection reply on the board instead of
showing "approved".
Expand Down
18 changes: 18 additions & 0 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,24 @@ nor one touching `.github/workflows/**`. The App needs **Actions: Read and
write** for the call; without it the approve fails, is logged once per head,
and the PR waits for a person as before.

The lane also asks Prow to start presubmits for any admitted author
(hivecommons/hive#17474). On a Prow-managed repository (Prow's labels such as
`size/*` or `needs-ok-to-test`, or its `[APPROVALNOTIFIER]` comment) the
trigger plugin runs presubmits for a non-member's fork PR only after a trusted
user comments `/ok-to-test`, so #17359 sat green on GitHub Actions while Tide
reported `Jobs … have not succeeded`. While such a fork head is not green, the
author's `author_association` is not `OWNER`, `MEMBER` or `COLLABORATOR`, the
PR has no `ok-to-test` label and Prow has reported no presubmit status on the
head (a status other than `tide` from the account that reports `tide`), the
lane posts `/ok-to-test` once per head with the same per-head marker and
mutation-boundary dedupe as the trusted-lane command. If Prow replies
`Cannot trigger testing until a trusted user`, the lane logs it once with
`skipped=prow-rejects-bot-commands`, leaves one
`<!-- hive-ok-to-test-rejected -->` needs-human note and never posts the
command on that PR again; making the App a Prow trusted user is the
repository's Prow configuration. The merge-request relay's read-only
evaluation posts nothing.

A PR merges through this tier only when all of the following hold:

- The PR author is admitted by one of three paths:
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,11 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own
return eval, reason, err
}
if !green {
if !relay {
// A non-member fork head on a Prow repo gets no presubmits until
// someone posts /ok-to-test (#17474); the relay stays read-only.
c.requestProwPresubmitsForFork(ctx, displayRepo, owner, repo, pr, number, headSHA)
}
if strings.HasPrefix(reason, requiredCheckMissingPrefix) {
_, reason = c.approveHeldRunsForRegistryTrusted(ctx, displayRepo, owner, repo, pr, number, headSHA, claim, policy, files, reason)
}
Expand Down
9 changes: 9 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_lane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type contributorLaneFixture struct {
// headStatus, when set, is the combined-status state and check-run
// conclusion reported for sha7; empty means "success".
headStatus string
// headStatuses is what GET commits/sha7/statuses (every commit status,
// with its creator) serves; nil serves an empty list.
headStatuses []map[string]any
// baseRef, when set, gives the PR base a branch name and serves the
// branch, protection and compare routes the current-base gate
// (automerge_current_base.go) and release hold need; empty leaves the
Expand Down Expand Up @@ -347,6 +350,12 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S
"total_count": 1,
"statuses": []map[string]string{{"context": "ci/build", "state": headStatus}},
})
case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/sha7/statuses":
statuses := fx.headStatuses
if statuses == nil {
statuses = []map[string]any{}
}
json.NewEncoder(w).Encode(statuses)
case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/sha7/check-runs":
run := map[string]string{"name": "build", "status": "completed", "conclusion": headStatus}
if fx.checkCompletedAt != "" {
Expand Down
149 changes: 149 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_prow_oktotest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package automerge

// Prow presubmits on non-member fork PRs (hivecommons/hive#17474, #17451).
//
// On a Prow-managed repository the trigger plugin runs presubmits only for a
// PR whose author is one of its trusted users (an org member or collaborator)
// or that carries `ok-to-test`. A contributor-lane fork PR by a non-member
// therefore never gets them, neither when it is opened nor after the hive's
// own base refresh, and Tide reports "Jobs … have not succeeded" on a PR that
// is green on GitHub Actions (#17359 sat about four hours until a person
// commented `/ok-to-test`).
//
// For a fork PR the contributor lane admitted (the hive vouches for the author
// and refreshes or merges the PR), the lane posts `/ok-to-test` once per head
// through the mutation boundary and per-head marker the held-runs path uses
// (postWorkflowApprovalCommand). If Prow answers that the App is not a trusted
// user, the lane records that once (hgithub.PRApprovalCommandsSkippedProw),
// leaves one needs-human note and never posts the command on the PR again.
// Whether the App is a Prow trusted user is the repository's Prow config, not
// the hive's to decide.

import (
"context"
"strings"

gh "github.com/google/go-github/v72/github"
hgithub "github.com/hivecommons/hive/pkg/github"
)

const (
// prowOkToTestLabel is the trigger plugin's label for a PR whose
// presubmits a trusted user released; it persists across pushes.
prowOkToTestLabel = "ok-to-test"
// prowTideContext is the commit status Tide itself reports; its creator
// is the Prow account that also reports the presubmit jobs.
prowTideContext = "tide"
)

// prowTrustedAssociations are the author_association values Prow's trigger
// plugin trusts by default: presubmits already run for these authors.
var prowTrustedAssociations = map[string]bool{
"OWNER": true,
"MEMBER": true,
"COLLABORATOR": true,
}

// requestProwPresubmitsForFork posts `/ok-to-test` once for headSHA when pr is
// a fork PR by a non-member on a Prow-managed repository (Prow's labels or its
// approve plugin's comment), carries no `ok-to-test` label, and Prow has
// reported no presubmit status on the head. A Prow rejection of the App's
// command is recorded once per PR and stops every later post. The caller
// must already have admitted the PR to the contributor lane.
func (c *Engine) requestProwPresubmitsForFork(ctx context.Context, displayRepo, owner, repo string, pr *gh.PullRequest, number int, headSHA string) {
if c == nil || c.gh == nil || pr == nil || strings.TrimSpace(headSHA) == "" || !c.isForkPR(pr) {
return
}
if prowTrustedAssociations[strings.ToUpper(strings.TrimSpace(pr.GetAuthorAssociation()))] {
return
}
labels := labelNames(pr.Labels)
if hasLabel(labels, prowOkToTestLabel) || c.workflowApprovalUntrustedKnown(owner, repo, number) {
return
}
_, first := c.markWorkflowApprovalCommandSeen(owner, repo, number, headSHA)
state, scanned := c.workflowApprovalCommentState(ctx, displayRepo, owner, repo, number, headSHA, first)
if !scanned {
return
}
if state.rejected {
c.markWorkflowApprovalUntrusted(owner, repo, number)
if !state.rejectionNoted {
c.postWorkflowApprovalUntrustedNote(ctx, displayRepo, owner, repo, number, headSHA, prowPresubmitsRejectedNote(strings.TrimSpace(c.transport.AppBotLogin())))
}
c.warn("contributor-lane ok-to-test not posted again: Prow rejected the App's command on this fork PR; a trusted user must post it",
"repo", displayRepo, "pr", number, "sha", headSHA, "skipped", hgithub.PRApprovalCommandsSkippedProw)
return
}
if state.postedForHead || !(hgithub.ProwManagedLabels(labels) || state.prowManaged) {
return
}
if c.prowPresubmitStatusPresent(ctx, displayRepo, owner, repo, number, headSHA) {
return
}
c.postWorkflowApprovalCommand(ctx, displayRepo, owner, repo, number, headSHA)
}

// prowPresubmitStatusPresent reports whether Prow already reported a job on
// headSHA: a commit status other than Tide's own, created by the account that
// created Tide's status. Without a Tide status nothing on the head can be
// attributed to Prow, so it reports false. A lookup failure reports true so
// nothing is posted on evidence that could not be read; the next comment scan
// tries again.
func (c *Engine) prowPresubmitStatusPresent(ctx context.Context, displayRepo, owner, repo string, number int, headSHA string) bool {
var statuses []*gh.RepoStatus
opts := &gh.ListOptions{PerPage: 100}
for {
page, resp, err := c.gh.Repositories.ListStatuses(hgithub.WithRESTCaller(ctx, "hive:contributor_lane_automerge_sweep"), owner, repo, headSHA, opts)
if err != nil {
c.warn("contributor-lane could not list commit statuses for Prow presubmits", "repo", displayRepo, "pr", number, "sha", headSHA, "error", err)
return true
}
statuses = append(statuses, page...)
if resp == nil || resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
prow := ""
for _, st := range statuses {
if st != nil && strings.EqualFold(strings.TrimSpace(st.GetContext()), prowTideContext) {
prow = hgithub.SafeGetLogin(st.GetCreator())
break
}
}
if prow == "" {
return false
}
for _, st := range statuses {
if st == nil || strings.EqualFold(strings.TrimSpace(st.GetContext()), prowTideContext) {
continue
}
if strings.EqualFold(hgithub.SafeGetLogin(st.GetCreator()), prow) {
return true
}
}
return false
}

// prowPresubmitsRejectedNote is the one needs-human comment posted when Prow
// refuses the App's `/ok-to-test` on a non-member fork PR.
func prowPresubmitsRejectedNote(appLogin string) string {
if appLogin == "" {
appLogin = "the Hive App"
}
var b strings.Builder
b.WriteString(workflowApprovalRejectedMarker)
b.WriteString("\nneeds-human: Prow has not run this fork PR's presubmits. Its author is not one of Prow's trusted users, so the trigger plugin waits for `")
b.WriteString(workflowApprovalCommand)
b.WriteString("`; Hive posted it and Prow rejected `")
b.WriteString(appLogin)
b.WriteString("` (\"")
b.WriteString(prowOkToTestRejection)
b.WriteString("\"). Hive will not post the command again on this PR.\n\nA maintainer can comment `")
b.WriteString(workflowApprovalCommand)
b.WriteString("` (plus `/test all` for the current head), or make `")
b.WriteString(appLogin)
b.WriteString("` a Prow trusted user so Hive's command counts.")
return b.String()
}
Loading
Loading