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
4
 
4
 
5
 import (
5
 import (
6
 	"context"
6
 	"context"
7
-	"path/filepath"
8
 
7
 
9
 	"github.com/jackc/pgx/v5/pgtype"
8
 	"github.com/jackc/pgx/v5/pgtype"
10
 	"github.com/jackc/pgx/v5/pgxpool"
9
 	"github.com/jackc/pgx/v5/pgxpool"
11
 
10
 
12
 	pullsdb "github.com/tenseleyFlow/shithub/internal/pulls/sqlc"
11
 	pullsdb "github.com/tenseleyFlow/shithub/internal/pulls/sqlc"
12
+	"github.com/tenseleyFlow/shithub/internal/repos/protection"
13
 	reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc"
13
 	reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc"
14
 )
14
 )
15
 
15
 
@@ -58,7 +58,7 @@ func Evaluate(ctx context.Context, pool *pgxpool.Pool, in GateInputs, prAuthorUs
58
 	if err != nil {
58
 	if err != nil {
59
 		return GateResult{}, err
59
 		return GateResult{}, err
60
 	}
60
 	}
61
-	rule, hasRule := matchRule(rules, in.BaseRef)
61
+	rule, hasRule := protection.MatchLongestRule(rules, in.BaseRef)
62
 
62
 
63
 	// If there's no rule, approve count + request_changes still gate
63
 	// If there's no rule, approve count + request_changes still gate
64
 	// the merge per spec ("no unresolved request_changes reviews").
64
 	// the merge per spec ("no unresolved request_changes reviews").
@@ -97,25 +97,7 @@ func loadProtectionRules(ctx context.Context, pool *pgxpool.Pool, repoID int64)
97
 }
97
 }
98
 
98
 
99
 // matchRule duplicates the longest-pattern-wins algorithm from
99
 // matchRule duplicates the longest-pattern-wins algorithm from
100
-// internal/repos/protection so this package doesn't pull that import
100
+// (matchRule extracted to protection.MatchLongestRule — see audit fix.)
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
-}
119
 
101
 
120
 // latestPerAuthor reduces reviews to a per-author "winning" state.
102
 // latestPerAuthor reduces reviews to a per-author "winning" state.
121
 // approve and request_changes update the author's tally; comment-state
103
 // approve and request_changes update the author's tally; comment-state