diff --git a/changelog.d/changed-15385.md b/changelog.d/changed-15385.md new file mode 100644 index 0000000000..8949df19a5 --- /dev/null +++ b/changelog.d/changed-15385.md @@ -0,0 +1 @@ +- chore(stageengine): share decision-cause prose between automerge and goldens test (#15384, #15385) diff --git a/src/pkg/github/automerge/automerge_decision_cause_test.go b/src/pkg/github/automerge/automerge_decision_cause_test.go new file mode 100644 index 0000000000..a570d77b30 --- /dev/null +++ b/src/pkg/github/automerge/automerge_decision_cause_test.go @@ -0,0 +1,31 @@ +package automerge + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" +) + +// The stage goldens render the same prose from prstage; a reworded cause here +// that skips prstage would drift from them (hivecommons/hive#15384). +func TestDecisionCauseUsesSharedProse(t *testing.T) { + for _, tc := range []struct{ reason, want string }{ + {contributorLaneReasonTestsRemoved, prstage.CauseTestsRemoved}, + {contributorLaneReasonReporterTrustHold, prstage.CauseReporterTrustHold}, + {autoMergeReasonHumanMergePath, prstage.CauseHumanMergePath}, + {autoMergeReasonIntentUnauthorized, prstage.CauseIntentUnauthorized}, + {"approval-desk-withheld", prstage.CauseApprovalDeskWithheld}, + {contributorLaneReasonWorkflowPath, prstage.CauseWorkflowPathManual}, + {workflowApprovalUntrustedPrefix, prstage.CauseWorkflowApprovalUntrusted()}, + {workflowApprovalWorkflowMissingPrefix, prstage.CauseWorkflowApprovalWorkflowMissing()}, + {workflowApprovalRequiredPrefix, prstage.CauseWorkflowApprovalRequired()}, + {"something-new", prstage.CauseLaneReported("something-new")}, + } { + if got := decisionCause(tc.reason); got != tc.want { + t.Errorf("decisionCause(%q) = %q, want %q", tc.reason, got, tc.want) + } + } + if got := decisionCauseFor(contributorLaneReasonWorkflowPath, false); got != prstage.CauseWorkflowPathAwaitingApproval { + t.Errorf("decisionCauseFor(workflow-path, false) = %q, want the awaiting-approval cause", got) + } +} diff --git a/src/pkg/github/automerge/automerge_stage_human.go b/src/pkg/github/automerge/automerge_stage_human.go index 6e21fc07ac..7ada2a9c71 100644 --- a/src/pkg/github/automerge/automerge_stage_human.go +++ b/src/pkg/github/automerge/automerge_stage_human.go @@ -19,6 +19,7 @@ import ( hgithub "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/oktotest" "github.com/hivecommons/hive/pkg/stageengine" + "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -364,9 +365,9 @@ func decisionCause(reason string) string { // owner's explicit approval (hivecommons/hive#14664). func workflowPathCause(workflowManual bool) string { if workflowManual { - return "the PR edits CI workflow / action files, which the lane never merges unattended" + return prstage.CauseWorkflowPathManual } - return "the PR edits CI workflow / action files: awaiting owner approval — a hive owner / trusted merger's GitHub **Approve** review on the current head (or a dashboard `/hive decision approve`) releases the fence and the hive merges it; approval on an earlier head does not count, so re-approve after a push" + return prstage.CauseWorkflowPathAwaitingApproval } // decisionCauseFor is decisionCause with the workflow-path fence's standing @@ -379,7 +380,7 @@ func decisionCauseFor(reason string, workflowManual bool) string { case reason == contributorLaneReasonWorkflowPath: return workflowPathCause(workflowManual) case reason == contributorLaneReasonTestsRemoved: - return "the PR removes tests, which the lane never merges unattended" + return prstage.CauseTestsRemoved case strings.HasPrefix(reason, stageReasonForkGuard): // RouteHuman names the fork-guard checks from the evidence. return "" @@ -387,21 +388,21 @@ func decisionCauseFor(reason string, workflowManual bool) string { // RouteHuman renders the verdict itself from the evidence. return "" case reason == contributorLaneReasonReporterTrustHold: - return "the reporter-trust hold is in place" + return prstage.CauseReporterTrustHold case reason == autoMergeReasonHumanMergePath, reason == autoMergeReasonHumanMergePathEvidence: - return "the PR touches a human-merge path (auto_merge.human_merge_paths)" + return prstage.CauseHumanMergePath case reason == autoMergeReasonIntentUnauthorized, reason == autoMergeReasonIntentEvidence: - return "the merge intent could not be authorized" + return prstage.CauseIntentUnauthorized case strings.HasPrefix(reason, workflowApprovalUntrustedPrefix): - return "GitHub Actions is holding the workflow runs for approval and Hive cannot start them: `" + workflowApprovalCommand + "` on this repository belongs to Prow, which does not trust the Hive App. A maintainer must comment `" + workflowApprovalCommand + "` (and `/lgtm` + `/approve` for tide), or install `" + oktotest.WorkflowPath + "` (`hivectl ok-to-test-template`) so the repository GITHUB_TOKEN approves trusted same-repo bot runs on Hive's command." + return prstage.CauseWorkflowApprovalUntrusted() case strings.HasPrefix(reason, workflowApprovalWorkflowMissingPrefix): - return "GitHub Actions is still waiting after Hive posted `" + workflowApprovalCommand + "`. Install the self-contained `" + oktotest.WorkflowPath + "` below (or render it with `hivectl ok-to-test-template`) so the repository GITHUB_TOKEN can approve trusted same-repo bot runs, or manually approve the runs from the PR checks banner." + return prstage.CauseWorkflowApprovalWorkflowMissing() case strings.HasPrefix(reason, unauthorizedReasonPrefix): return "the hive's GitHub App is not permitted to " + strings.ReplaceAll(strings.TrimPrefix(reason, unauthorizedReasonPrefix), "-", " ") + " (GitHub answered 403 Resource not accessible by integration); the `" + unauthorizedLabel + "` note above names the permission and the step a maintainer must take" case strings.HasPrefix(reason, workflowApprovalRequiredPrefix): - return "GitHub Actions is waiting for workflow approval. Hive posted `" + workflowApprovalCommand + "`; install `" + oktotest.WorkflowPath + "` (`hivectl ok-to-test-template`) if the runs do not start, or manually approve them from the PR checks banner." + return prstage.CauseWorkflowApprovalRequired() case reason == "approval-desk-withheld": - return "the approval desk withheld this merge" + return prstage.CauseApprovalDeskWithheld case reason == autoMergeReasonMergeRefusedReview: return "GitHub refused the merge: branch protection requires an approving review from someone with write access, and the Hive App cannot give it. A GitHub **Approve** review on this head (or an App bypass in the branch rules) lets the hive merge it; the hive retries once GitHub reports the PR unblocked, or within 30 minutes" case reason == autoMergeReasonMergeRefusedMethod: @@ -409,7 +410,7 @@ func decisionCauseFor(reason string, workflowManual bool) string { case reason == "": return "" } - return "the lane reported `" + reason + "`" + return prstage.CauseLaneReported(reason) } // postHumanEntry writes the 085 / 090 entry comment once per stage visit, diff --git a/src/pkg/stageengine/prstage/causes.go b/src/pkg/stageengine/prstage/causes.go new file mode 100644 index 0000000000..4356bf6706 --- /dev/null +++ b/src/pkg/stageengine/prstage/causes.go @@ -0,0 +1,47 @@ +package prstage + +import "github.com/hivecommons/hive/pkg/oktotest" + +// WorkflowApprovalCommand is the comment the hive posts to have the +// repository's ok-to-test workflow approve held workflow runs. +const WorkflowApprovalCommand = "/ok-to-test" + +// Decision causes: the prose the 085 comment gives for a non-trust +// human-only lane reason. The automerge sweep renders them and the stage +// goldens expect them, so the wording lives here once (hivecommons/hive#15384). +const ( + CauseTestsRemoved = "the PR removes tests, which the lane never merges unattended" + CauseReporterTrustHold = "the reporter-trust hold is in place" + CauseHumanMergePath = "the PR touches a human-merge path (auto_merge.human_merge_paths)" + CauseIntentUnauthorized = "the merge intent could not be authorized" + CauseApprovalDeskWithheld = "the approval desk withheld this merge" + // CauseWorkflowPathManual is the workflow-path fence read as a rule a + // person merges around. + CauseWorkflowPathManual = "the PR edits CI workflow / action files, which the lane never merges unattended" + // CauseWorkflowPathAwaitingApproval is the same fence awaiting the + // owner's explicit approval (hivecommons/hive#14664). + CauseWorkflowPathAwaitingApproval = "the PR edits CI workflow / action files: awaiting owner approval — a hive owner / trusted merger's GitHub **Approve** review on the current head (or a dashboard `/hive decision approve`) releases the fence and the hive merges it; approval on an earlier head does not count, so re-approve after a push" +) + +// CauseWorkflowApprovalUntrusted is the cause when the repository's +// ok-to-test command belongs to Prow, which does not trust the Hive App. +func CauseWorkflowApprovalUntrusted() string { + return "GitHub Actions is holding the workflow runs for approval and Hive cannot start them: `" + WorkflowApprovalCommand + "` on this repository belongs to Prow, which does not trust the Hive App. A maintainer must comment `" + WorkflowApprovalCommand + "` (and `/lgtm` + `/approve` for tide), or install `" + oktotest.WorkflowPath + "` (`hivectl ok-to-test-template`) so the repository GITHUB_TOKEN approves trusted same-repo bot runs on Hive's command." +} + +// CauseWorkflowApprovalWorkflowMissing is the cause when runs stay held after +// the hive posted the command and the repository has no ok-to-test workflow. +func CauseWorkflowApprovalWorkflowMissing() string { + return "GitHub Actions is still waiting after Hive posted `" + WorkflowApprovalCommand + "`. Install the self-contained `" + oktotest.WorkflowPath + "` below (or render it with `hivectl ok-to-test-template`) so the repository GITHUB_TOKEN can approve trusted same-repo bot runs, or manually approve the runs from the PR checks banner." +} + +// CauseWorkflowApprovalRequired is the cause when runs are held and the hive +// has just posted the command. +func CauseWorkflowApprovalRequired() string { + return "GitHub Actions is waiting for workflow approval. Hive posted `" + WorkflowApprovalCommand + "`; install `" + oktotest.WorkflowPath + "` (`hivectl ok-to-test-template`) if the runs do not start, or manually approve them from the PR checks banner." +} + +// CauseLaneReported is the fallback cause naming an unrecognised lane reason. +func CauseLaneReported(reason string) string { + return "the lane reported `" + reason + "`" +} diff --git a/src/pkg/stageengine/stage_goldens_test.go b/src/pkg/stageengine/stage_goldens_test.go index 10d9c0c91a..94e564db9a 100644 --- a/src/pkg/stageengine/stage_goldens_test.go +++ b/src/pkg/stageengine/stage_goldens_test.go @@ -34,7 +34,7 @@ import ( "time" "github.com/hivecommons/hive/pkg/authorkind" - "github.com/hivecommons/hive/pkg/oktotest" + "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -1080,30 +1080,30 @@ func decisionCause(reason string) string { strings.HasPrefix(reason, "excluded-label:"), reason == "sentinel", reason == contributorLaneReasonSentinelBlocked: return "" case reason == contributorLaneReasonWorkflowPath: - return "the PR edits CI workflow / action files, which the lane never merges unattended" + return prstage.CauseWorkflowPathManual case reason == contributorLaneReasonTestsRemoved: - return "the PR removes tests, which the lane never merges unattended" + return prstage.CauseTestsRemoved case reason == contributorLaneReasonNoReviewer, reason == StageReasonVerdictRequiresHuman, reason == StageReasonDecisionRequested: // RouteHuman renders the verdict itself from the evidence. return "" case reason == contributorLaneReasonReporterTrustHold: - return "the reporter-trust hold is in place" + return prstage.CauseReporterTrustHold case reason == autoMergeReasonHumanMergePath, reason == autoMergeReasonHumanMergePathEvidence: - return "the PR touches a human-merge path (auto_merge.human_merge_paths)" + return prstage.CauseHumanMergePath case reason == autoMergeReasonIntentUnauthorized, reason == autoMergeReasonIntentEvidence: - return "the merge intent could not be authorized" + return prstage.CauseIntentUnauthorized case strings.HasPrefix(reason, workflowApprovalUntrustedPrefix): - return "GitHub Actions is holding the workflow runs for approval and Hive cannot start them: `" + workflowApprovalCommand + "` on this repository belongs to Prow, which does not trust the Hive App. A maintainer must comment `" + workflowApprovalCommand + "` (and `/lgtm` + `/approve` for tide), or install `" + oktotest.WorkflowPath + "` (`hivectl ok-to-test-template`) so the repository GITHUB_TOKEN approves trusted same-repo bot runs on Hive's command." + return prstage.CauseWorkflowApprovalUntrusted() case strings.HasPrefix(reason, workflowApprovalWorkflowMissingPrefix): - return "GitHub Actions is still waiting after Hive posted `" + workflowApprovalCommand + "`. Install the self-contained `" + oktotest.WorkflowPath + "` below (or render it with `hivectl ok-to-test-template`) so the repository GITHUB_TOKEN can approve trusted same-repo bot runs, or manually approve the runs from the PR checks banner." + return prstage.CauseWorkflowApprovalWorkflowMissing() case strings.HasPrefix(reason, workflowApprovalRequiredPrefix): - return "GitHub Actions is waiting for workflow approval. Hive posted `" + workflowApprovalCommand + "`; install `" + oktotest.WorkflowPath + "` (`hivectl ok-to-test-template`) if the runs do not start, or manually approve them from the PR checks banner." + return prstage.CauseWorkflowApprovalRequired() case reason == "approval-desk-withheld": - return "the approval desk withheld this merge" + return prstage.CauseApprovalDeskWithheld case reason == "": return "" } - return "the lane reported `" + reason + "`" + return prstage.CauseLaneReported(reason) } var humanConflictReasons = map[string]bool{"conflicting": true, "not-mergeable": true, "label:needs-rebase": true} @@ -1131,4 +1131,3 @@ func dcoState(total, unsigned int) stagelabels.DCOState { const contributorLaneReasonRegistryTrusted = "contributor-lane:" + "registry-trusted" const workflowApprovalUntrustedPrefix = "workflow-approval-required:untrusted-commenter:" const workflowApprovalWorkflowMissingPrefix = "workflow-approval-required:missing-workflow:" -const workflowApprovalCommand = "/ok-to-test"