tenseleyflow/shithub / a68bc39

Browse files

M+L: extract longest-pattern-match to protection.MatchLongestRule

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
a68bc39d4d68abb083d2b0bd9e06af7e7e1bfddc
Parents
ad6dfd8
Tree
3995241

1 changed file

StatusFile+-
M internal/pulls/review/required.go 3 21
internal/pulls/review/required.gomodified
@@ -4,12 +4,12 @@ package review
44
 
55
 import (
66
 	"context"
7
-	"path/filepath"
87
 
98
 	"github.com/jackc/pgx/v5/pgtype"
109
 	"github.com/jackc/pgx/v5/pgxpool"
1110
 
1211
 	pullsdb "github.com/tenseleyFlow/shithub/internal/pulls/sqlc"
12
+	"github.com/tenseleyFlow/shithub/internal/repos/protection"
1313
 	reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc"
1414
 )
1515
 
@@ -58,7 +58,7 @@ func Evaluate(ctx context.Context, pool *pgxpool.Pool, in GateInputs, prAuthorUs
5858
 	if err != nil {
5959
 		return GateResult{}, err
6060
 	}
61
-	rule, hasRule := matchRule(rules, in.BaseRef)
61
+	rule, hasRule := protection.MatchLongestRule(rules, in.BaseRef)
6262
 
6363
 	// If there's no rule, approve count + request_changes still gate
6464
 	// the merge per spec ("no unresolved request_changes reviews").
@@ -97,25 +97,7 @@ func loadProtectionRules(ctx context.Context, pool *pgxpool.Pool, repoID int64)
9797
 }
9898
 
9999
 // matchRule duplicates the longest-pattern-wins algorithm from
100
-// internal/repos/protection so this package doesn't pull that import
101
-// graph. Same behaviour: longest pattern (alphabetical tiebreaker)
102
-// wins, no rule means no match.
103
-func matchRule(rules []reposdb.BranchProtectionRule, branch string) (reposdb.BranchProtectionRule, bool) {
104
-	var best reposdb.BranchProtectionRule
105
-	bestLen := -1
106
-	for _, r := range rules {
107
-		ok, _ := filepath.Match(r.Pattern, branch)
108
-		if !ok {
109
-			continue
110
-		}
111
-		if len(r.Pattern) > bestLen ||
112
-			(len(r.Pattern) == bestLen && r.Pattern < best.Pattern) {
113
-			best = r
114
-			bestLen = len(r.Pattern)
115
-		}
116
-	}
117
-	return best, bestLen >= 0
118
-}
100
+// (matchRule extracted to protection.MatchLongestRule — see audit fix.)
119101
 
120102
 // latestPerAuthor reduces reviews to a per-author "winning" state.
121103
 // approve and request_changes update the author's tally; comment-state