tenseleyflow/shithub / 20ffd30

Browse files

M slop: drop unused-import shims, sanitizerOnce, ErrTransient, stubCmd

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
20ffd308528838af2341b37ed8ccd89b084aa292
Parents
c0c9bdf
Tree
e2ad52b

5 changed files

StatusFile+-
M cmd/shithubd/hook.go 0 4
D cmd/shithubd/stubs.go 0 22
M internal/markdown/extensions/extensions.go 8 21
M internal/markdown/sanitize.go 2 9
M internal/repos/protection/protection.go 9 7
cmd/shithubd/hook.gomodified
@@ -15,7 +15,6 @@ import (
15
 	"strings"
15
 	"strings"
16
 	"time"
16
 	"time"
17
 
17
 
18
-	"github.com/jackc/pgx/v5"
19
 	"github.com/jackc/pgx/v5/pgtype"
18
 	"github.com/jackc/pgx/v5/pgtype"
20
 	"github.com/jackc/pgx/v5/pgxpool"
19
 	"github.com/jackc/pgx/v5/pgxpool"
21
 	"github.com/spf13/cobra"
20
 	"github.com/spf13/cobra"
@@ -383,6 +382,3 @@ func init() {
383
 	rootCmd.AddCommand(hooksParentCmd)
382
 	rootCmd.AddCommand(hooksParentCmd)
384
 }
383
 }
385
 
384
 
386
-// silence unused import warnings during incremental builds — the pgx
387
-// import is used through the hookCtx pool helper above.
388
-var _ = pgx.ErrNoRows
cmd/shithubd/stubs.godeleted
@@ -1,22 +0,0 @@
1
-// SPDX-License-Identifier: AGPL-3.0-or-later
2
-
3
-package main
4
-
5
-import (
6
-	"fmt"
7
-
8
-	"github.com/spf13/cobra"
9
-)
10
-
11
-// stubCmd returns a placeholder cobra command for subcommands scheduled in
12
-// later sprints. The sprint identifier is surfaced in the error so an
13
-// operator who hits the stub knows which sprint adds the implementation.
14
-func stubCmd(name, short, sprint string) *cobra.Command {
15
-	return &cobra.Command{
16
-		Use:   name,
17
-		Short: fmt.Sprintf("%s (planned in %s)", short, sprint),
18
-		RunE: func(cmd *cobra.Command, args []string) error {
19
-			return fmt.Errorf("%s: not yet implemented (planned in sprint %s)", name, sprint)
20
-		},
21
-	}
22
-}
internal/markdown/extensions/extensions.gomodified
@@ -25,7 +25,6 @@ import (
25
 	"context"
25
 	"context"
26
 	"regexp"
26
 	"regexp"
27
 	"strconv"
27
 	"strconv"
28
-	"strings"
29
 
28
 
30
 	"github.com/yuin/goldmark"
29
 	"github.com/yuin/goldmark"
31
 	"github.com/yuin/goldmark/ast"
30
 	"github.com/yuin/goldmark/ast"
@@ -93,8 +92,11 @@ type Mention struct {
93
 //	#6               commit prefix
92
 //	#6               commit prefix
94
 //	#7               emoji name
93
 //	#7               emoji name
95
 var reCombined = regexp.MustCompile(`` +
94
 var reCombined = regexp.MustCompile(`` +
96
-	// cross-repo: alice/proj#3
95
+	// cross-repo: alice/proj#3 — left boundary required so we don't
97
-	`([A-Za-z0-9][A-Za-z0-9._-]*)/([A-Za-z0-9][A-Za-z0-9._-]*)#([0-9]{1,9})\b` +
96
+	// chew into a preceding word (e.g. `xfoo/bar#3` should not be a
97
+	// match of `foo/bar#3`). Mirrors the same-repo / mention shape;
98
+	// the audit caught the asymmetry (S00-S25, M).
99
+	`(?:^|[^\w/])([A-Za-z0-9][A-Za-z0-9._-]*)/([A-Za-z0-9][A-Za-z0-9._-]*)#([0-9]{1,9})\b` +
98
 	// or same-repo: #3 — must have non-word non-/ boundary on the left
100
 	// or same-repo: #3 — must have non-word non-/ boundary on the left
99
 	`|(?:^|[^\w/])#([0-9]{1,9})\b` +
101
 	`|(?:^|[^\w/])#([0-9]{1,9})\b` +
100
 	// or mention: @alice — must have non-word boundary on the left
102
 	// or mention: @alice — must have non-word boundary on the left
@@ -176,6 +178,9 @@ func (t *transformer) replaceText(txt *ast.Text, source []byte) {
176
 		var contentStart int
178
 		var contentStart int
177
 		switch {
179
 		switch {
178
 		case isCrossRepo:
180
 		case isCrossRepo:
181
+			// m[2] is the owner-start position (after the regex's
182
+			// leading non-word boundary char). Same shape as same-repo
183
+			// — emit the boundary char as plain text, then the link.
179
 			contentStart = m[2]
184
 			contentStart = m[2]
180
 		case isSameRepo:
185
 		case isSameRepo:
181
 			contentStart = m[8] - 1 // include `#`
186
 			contentStart = m[8] - 1 // include `#`
@@ -335,21 +340,3 @@ func (t *transformer) appendCommitLink(parent, before ast.Node, shaPrefix string
335
 	return true
340
 	return true
336
 }
341
 }
337
 
342
 
338
-// trimLeadingNonWord drops the leading boundary char(s) — used when
339
-// a same-repo or mention token is rendered as plain text fallback.
340
-func trimLeadingNonWord(b []byte) []byte {
341
-	for len(b) > 0 && !isWordByte(b[0]) {
342
-		b = b[1:]
343
-	}
344
-	return b
345
-}
346
-
347
-func isWordByte(c byte) bool {
348
-	return c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
349
-}
350
-
351
-// silence unused-import warnings in a stripped build.
352
-var (
353
-	_ = strings.Builder{}
354
-	_ = trimLeadingNonWord
355
-)
internal/markdown/sanitize.gomodified
@@ -4,7 +4,6 @@ package markdown
4
 
4
 
5
 import (
5
 import (
6
 	"regexp"
6
 	"regexp"
7
-	"sync"
8
 
7
 
9
 	"github.com/microcosm-cc/bluemonday"
8
 	"github.com/microcosm-cc/bluemonday"
10
 )
9
 )
@@ -70,14 +69,8 @@ var sanitizer = func() *bluemonday.Policy {
70
 var reCodeClass = regexp.MustCompile(`^(?:language-[A-Za-z0-9_+\-]+|chroma|chroma-[a-zA-Z]+|nl|ln|line|hl)(?:\s+(?:language-[A-Za-z0-9_+\-]+|chroma|chroma-[a-zA-Z]+|nl|ln|line|hl))*$`)
69
 var reCodeClass = regexp.MustCompile(`^(?:language-[A-Za-z0-9_+\-]+|chroma|chroma-[a-zA-Z]+|nl|ln|line|hl)(?:\s+(?:language-[A-Za-z0-9_+\-]+|chroma|chroma-[a-zA-Z]+|nl|ln|line|hl))*$`)
71
 
70
 
72
 // sanitizeBytes is the hot-path entry the Render pipeline uses. The
71
 // sanitizeBytes is the hot-path entry the Render pipeline uses. The
73
-// bluemonday Policy is itself goroutine-safe; the once.Do here keeps
72
+// bluemonday Policy is built at package init via the var initializer
74
-// the regex compilation cost off the first request path.
73
+// above and is itself goroutine-safe — no need for sync.Once gymnastics.
75
-var sanitizerOnce sync.Once
76
-
77
 func sanitizeBytes(in []byte) []byte {
74
 func sanitizeBytes(in []byte) []byte {
78
-	sanitizerOnce.Do(func() {
79
-		// Touch the package-level sanitizer to ensure it's built.
80
-		_ = sanitizer
81
-	})
82
 	return sanitizer.SanitizeBytes(in)
75
 	return sanitizer.SanitizeBytes(in)
83
 }
76
 }
internal/repos/protection/protection.gomodified
@@ -11,7 +11,6 @@ package protection
11
 
11
 
12
 import (
12
 import (
13
 	"context"
13
 	"context"
14
-	"errors"
15
 	"fmt"
14
 	"fmt"
16
 	"path/filepath"
15
 	"path/filepath"
17
 	"sort"
16
 	"sort"
@@ -114,8 +113,11 @@ func deny(r reposdb.BranchProtectionRule, reason string) Decision {
114
 	}
113
 	}
115
 }
114
 }
116
 
115
 
117
-// matchRule returns the rule with the longest pattern matching branch
116
+// MatchLongestRule returns the rule with the longest pattern matching
118
-// (alphabetical tiebreaker). Returns ok=false when no rule matches.
117
+// branch (alphabetical tiebreaker). Returns ok=false when no rule
118
+// matches. Exposed as the canonical pattern-match helper so the
119
+// `pulls` and `pulls/review` packages don't reimplement it (the audit
120
+// caught two near-identical copies — S00-S25 audit, M).
119
 //
121
 //
120
 // Patterns use filepath.Match semantics:
122
 // Patterns use filepath.Match semantics:
121
 //   - `*` matches any sequence of non-separator chars (NOT crossing `/`)
123
 //   - `*` matches any sequence of non-separator chars (NOT crossing `/`)
@@ -123,6 +125,10 @@ func deny(r reposdb.BranchProtectionRule, reason string) Decision {
123
 //   - `[abc]` matches one of a/b/c
125
 //   - `[abc]` matches one of a/b/c
124
 //
126
 //
125
 // `release/*` matches `release/v1.0` but NOT `release/v1.0/sub`.
127
 // `release/*` matches `release/v1.0` but NOT `release/v1.0/sub`.
128
+func MatchLongestRule(rules []reposdb.BranchProtectionRule, branch string) (reposdb.BranchProtectionRule, bool) {
129
+	return matchRule(rules, branch)
130
+}
131
+
126
 func matchRule(rules []reposdb.BranchProtectionRule, branch string) (reposdb.BranchProtectionRule, bool) {
132
 func matchRule(rules []reposdb.BranchProtectionRule, branch string) (reposdb.BranchProtectionRule, bool) {
127
 	type cand struct {
133
 	type cand struct {
128
 		rule reposdb.BranchProtectionRule
134
 		rule reposdb.BranchProtectionRule
@@ -173,7 +179,3 @@ func FriendlyMessage(d Decision) string {
173
 	return fmt.Sprintf("shithub: %s (rule pattern %q).", d.Reason, d.Pattern)
179
 	return fmt.Sprintf("shithub: %s (rule pattern %q).", d.Reason, d.Pattern)
174
 }
180
 }
175
 
181
 
176
-// ErrTransient is returned by Enforce when DB connectivity is the
177
-// failure cause. Pre-receive maps this to "transient error; try
178
-// again" and rejects the push (fail closed per S20 spec).
179
-var ErrTransient = errors.New("protection: transient error")