diff --git a/changelog.d/fixed-delegated-capability-inference.md b/changelog.d/fixed-delegated-capability-inference.md new file mode 100644 index 0000000000..6b9b4c4122 --- /dev/null +++ b/changelog.d/fixed-delegated-capability-inference.md @@ -0,0 +1 @@ +- Delegated issue decisions now use the blocker ledger's shared capability inference, keeping repository-write, branch-protection, secret, and workflow permission gaps parked instead of approving ineffective directions, while retaining the park for other missing App permissions (#15217). diff --git a/src/pkg/delegatedecision/policy.go b/src/pkg/delegatedecision/policy.go index 254ef418a7..a09f50fe39 100644 --- a/src/pkg/delegatedecision/policy.go +++ b/src/pkg/delegatedecision/policy.go @@ -27,6 +27,8 @@ import ( "sort" "strconv" "strings" + + "github.com/hivecommons/hive/pkg/blocker" ) // PolicyName is the policy identifier the audit marker carries. @@ -247,9 +249,9 @@ var ( ratchetRE = regexp.MustCompile(`(?i)\b(actionlint|ratchet|duplication (?:gate|threshold|check)|link[- ]?checker|allow-?list|lint gate|hygiene gate|shellcheck|yamllint|markdownlint)\b`) proposalRE = regexp.MustCompile(`(?i)\bacceptance criteria\b`) hivePolicyRE = regexp.MustCompile(`(?i)\b(hive\.ya?ml|governance|security posture|hive (?:settings?|config(?:uration)?|policy|policies)|branch protection|CODEOWNERS|required checks?|autonomy level|acmm level)\b`) - // capabilityGapRE matches a blocker no direction decision can clear: the - // GitHub App lacks a permission (for example `workflows`). - capabilityGapRE = regexp.MustCompile(`(?i)\b(?:app(?: token)?|installation|github app)\b[^\n]{0,80}\b(?:cannot|can't|lacks?|no|without|missing)\b[^\n]{0,60}\b(?:workflows?|permissions?)\b|\bno [\x60]?workflows[\x60]? permission\b`) + // appPermissionRE preserves the park for App permissions that have no + // specific capability kind in the blocker ledger. + appPermissionRE = regexp.MustCompile(`(?i)\b(?:app(?: token)?|installation|github app)\b[^\n]{0,80}\b(?:cannot|can't|lacks?|missing|without|no)\b[^\n]{0,60}\bpermissions?\b`) duplicateOfRE = regexp.MustCompile(`(?i)\bduplicate of #(\d+)`) blockedOnPRRE = regexp.MustCompile(`(?i)\b(review backlog|blocked on (?:its|their|the) PR)\b`) ) @@ -322,10 +324,16 @@ func HivePolicyChange(in Input) bool { } // CapabilityGap reports a needs-human whose blocker is a missing GitHub App -// capability rather than an open question. +// capability rather than an open question. Shared ledger inference handles +// known capability kinds; a generic App-permission guard keeps other missing +// permissions parked. Human-only blockers are handled by the ledger. func CapabilityGap(in Input) bool { - return capabilityGapRE.MatchString(in.headline()) || - in.Kind == KindIssue && capabilityGapRE.MatchString(firstLines(in.Body, 12)) + isCapability := func(text string) bool { + k, ok := blocker.Infer(text) + return ok && k.IsCapability() || appPermissionRE.MatchString(text) + } + return isCapability(in.headline()) || + in.Kind == KindIssue && isCapability(firstLines(in.Body, 12)) } // OwnerOnly reports a secrets / PAT / org-permission item. diff --git a/src/pkg/delegatedecision/policy_test.go b/src/pkg/delegatedecision/policy_test.go index 23130f907d..ddbc5ac4b0 100644 --- a/src/pkg/delegatedecision/policy_test.go +++ b/src/pkg/delegatedecision/policy_test.go @@ -25,6 +25,52 @@ func sameKinds(a, b []ActionKind) bool { return true } +func TestCapabilityGapSharedInference(t *testing.T) { + for _, report := range []string{ + "The App cannot push changes to workflow files.", + "The App cannot push to the upstream repository.", + "The App cannot update the protected branch.", + "The App cannot set the repository secret.", + "The GitHub App lacks the `issues: write` permission.", + "App lacks the `administration` permission to edit rulesets", + "The installation lacks the required permissions; needs a human.", + "The App token is missing the `checks: write` permission.", + "The installation has no permission to manage checks.", + "The App is without the required permission.", + "The App cannot obtain the required permission.", + "The App can't obtain the required permission.", + } { + t.Run(report, func(t *testing.T) { + for _, in := range []Input{ + {Kind: KindIssue, Title: report}, + {Kind: KindIssue, Question: report}, + {Kind: KindIssue, Reasons: []string{report}}, + {Kind: KindIssue, Body: report}, + {Kind: KindIssue, Body: strings.Repeat("context\n", 11) + report}, + } { + d := Run(in) + if !CapabilityGap(in) || d.Rule != RuleCapabilityGap || d.Verdict != VerdictRecommendOnly || !d.KeepsPark || !sameKinds(kinds(d), []ActionKind{ActionRecommendOnly}) { + t.Fatalf("capability report must keep the park: input=%+v decision=%+v", in, d) + } + } + }) + } + for _, in := range []Input{ + {Kind: KindIssue, Title: "The contributor must sign the CLA."}, + {Kind: KindIssue, Title: "Which workflow retry strategy should we use?"}, + {Kind: KindIssue, Title: "Which permissions should the GitHub App request?"}, + {Kind: KindIssue, Title: "The GitHub App has the required permissions."}, + {Kind: KindIssue, Body: strings.Repeat("context\n", 12) + "The GitHub App lacks the `issues: write` permission."}, + {Kind: KindPR, Body: "The GitHub App lacks the `issues: write` permission."}, + {Kind: KindIssue, Body: strings.Repeat("context\n", 12) + "The App cannot push to the repository."}, + {Kind: KindPR, Body: "The App cannot push to the repository."}, + } { + if CapabilityGap(in) { + t.Errorf("unexpected capability gap: %+v", in) + } + } +} + func TestDecideIssueRules(t *testing.T) { t.Parallel() cases := []struct {