Skip to content

Commit 6033c67

Browse files
authored
Refactor some trivial problems (#34959)
1. make our "route group pattern match" also update chi's RoutePattern 2. fix incorrect "NotFound" call in conda package 3. make ".flex-item .flex-item-main" has a general gap, then no need to use `tw` tricks 4. improve the "test webhook" UI
1 parent 5557359 commit 6033c67

File tree

8 files changed

+39
-20
lines changed

8 files changed

+39
-20
lines changed

modules/web/router_path.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (g *RouterPathGroup) ServeHTTP(resp http.ResponseWriter, req *http.Request)
2626
path := chiCtx.URLParam(g.pathParam)
2727
for _, m := range g.matchers {
2828
if m.matchPath(chiCtx, path) {
29+
chiCtx.RoutePatterns = append(chiCtx.RoutePatterns, m.pattern)
2930
handler := m.handlerFunc
3031
for i := len(m.middlewares) - 1; i >= 0; i-- {
3132
handler = m.middlewares[i](handler).ServeHTTP
@@ -38,6 +39,7 @@ func (g *RouterPathGroup) ServeHTTP(resp http.ResponseWriter, req *http.Request)
3839
}
3940

4041
type RouterPathGroupPattern struct {
42+
pattern string
4143
re *regexp.Regexp
4244
params []routerPathParam
4345
middlewares []any
@@ -62,6 +64,7 @@ type routerPathParam struct {
6264

6365
type routerPathMatcher struct {
6466
methods container.Set[string]
67+
pattern string
6568
re *regexp.Regexp
6669
params []routerPathParam
6770
middlewares []func(http.Handler) http.Handler
@@ -117,7 +120,7 @@ func newRouterPathMatcher(methods string, patternRegexp *RouterPathGroupPattern,
117120
}
118121
p.methods.Add(method)
119122
}
120-
p.re, p.params = patternRegexp.re, patternRegexp.params
123+
p.pattern, p.re, p.params = patternRegexp.pattern, patternRegexp.re, patternRegexp.params
121124
return p
122125
}
123126

@@ -157,7 +160,7 @@ func patternRegexp(pattern string, h ...any) *RouterPathGroupPattern {
157160
p.params = append(p.params, param)
158161
}
159162
re = append(re, '$')
160-
p.re = regexp.MustCompile(string(re))
163+
p.pattern, p.re = pattern, regexp.MustCompile(string(re))
161164
return p
162165
}
163166

modules/web/router_test.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,20 @@ func TestRouter(t *testing.T) {
5656
recorder.Body = buff
5757

5858
type resultStruct struct {
59-
method string
60-
pathParams map[string]string
61-
handlerMarks []string
59+
method string
60+
pathParams map[string]string
61+
handlerMarks []string
62+
chiRoutePattern *string
6263
}
6364

6465
var res resultStruct
6566
h := func(optMark ...string) func(resp http.ResponseWriter, req *http.Request) {
6667
mark := util.OptionalArg(optMark, "")
6768
return func(resp http.ResponseWriter, req *http.Request) {
69+
chiCtx := chi.RouteContext(req.Context())
6870
res.method = req.Method
69-
res.pathParams = chiURLParamsToMap(chi.RouteContext(req.Context()))
71+
res.pathParams = chiURLParamsToMap(chiCtx)
72+
res.chiRoutePattern = util.ToPointer(chiCtx.RoutePattern())
7073
if mark != "" {
7174
res.handlerMarks = append(res.handlerMarks, mark)
7275
}
@@ -125,21 +128,29 @@ func TestRouter(t *testing.T) {
125128
req, err := http.NewRequest(methodPathFields[0], methodPathFields[1], nil)
126129
assert.NoError(t, err)
127130
r.ServeHTTP(recorder, req)
131+
if expected.chiRoutePattern == nil {
132+
res.chiRoutePattern = nil
133+
}
128134
assert.Equal(t, expected, res)
129135
})
130136
}
131137

132138
t.Run("RootRouter", func(t *testing.T) {
133-
testRoute(t, "GET /the-user/the-repo/other", resultStruct{method: "GET", handlerMarks: []string{"not-found:/"}})
139+
testRoute(t, "GET /the-user/the-repo/other", resultStruct{
140+
method: "GET",
141+
handlerMarks: []string{"not-found:/"},
142+
chiRoutePattern: util.ToPointer(""),
143+
})
134144
testRoute(t, "GET /the-user/the-repo/pulls", resultStruct{
135145
method: "GET",
136146
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "pulls"},
137147
handlerMarks: []string{"list-issues-b"},
138148
})
139149
testRoute(t, "GET /the-user/the-repo/issues/123", resultStruct{
140-
method: "GET",
141-
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "issues", "index": "123"},
142-
handlerMarks: []string{"view-issue"},
150+
method: "GET",
151+
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "issues", "index": "123"},
152+
handlerMarks: []string{"view-issue"},
153+
chiRoutePattern: util.ToPointer("/{username}/{reponame}/{type:issues|pulls}/{index}"),
143154
})
144155
testRoute(t, "GET /the-user/the-repo/issues/123?stop=hijack", resultStruct{
145156
method: "GET",
@@ -154,7 +165,10 @@ func TestRouter(t *testing.T) {
154165
})
155166

156167
t.Run("Sub Router", func(t *testing.T) {
157-
testRoute(t, "GET /api/v1/other", resultStruct{method: "GET", handlerMarks: []string{"not-found:/api/v1"}})
168+
testRoute(t, "GET /api/v1/other", resultStruct{
169+
method: "GET",
170+
handlerMarks: []string{"not-found:/api/v1"},
171+
})
158172
testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches", resultStruct{
159173
method: "GET",
160174
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
@@ -211,9 +225,10 @@ func TestRouter(t *testing.T) {
211225
})
212226

213227
testRoute(t, "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s3", resultStruct{
214-
method: "GET",
215-
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1/d2/fn", "dir": "d1/d2", "file": "fn"},
216-
handlerMarks: []string{"s1", "s2", "s3"},
228+
method: "GET",
229+
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "*": "d1/d2/fn", "dir": "d1/d2", "file": "fn"},
230+
handlerMarks: []string{"s1", "s2", "s3"},
231+
chiRoutePattern: util.ToPointer("/api/v1/repos/{username}/{reponame}/branches/<dir:*>/<file:[a-z]{1,2}>"),
217232
})
218233
})
219234
}

options/locale/locale_en-US.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,8 +2334,8 @@ settings.hooks_desc = Webhooks automatically make HTTP POST requests to a server
23342334
settings.webhook_deletion = Remove Webhook
23352335
settings.webhook_deletion_desc = Removing a webhook deletes its settings and delivery history. Continue?
23362336
settings.webhook_deletion_success = The webhook has been removed.
2337-
settings.webhook.test_delivery = Test Delivery
2338-
settings.webhook.test_delivery_desc = Test this webhook with a fake event.
2337+
settings.webhook.test_delivery = Test Push Event
2338+
settings.webhook.test_delivery_desc = Test this webhook with a fake push event.
23392339
settings.webhook.test_delivery_desc_disabled = To test this webhook with a fake event, activate it.
23402340
settings.webhook.request = Request
23412341
settings.webhook.response = Response

routers/api/packages/conda/conda.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func ListOrGetPackages(ctx *context.Context) {
5151
DownloadPackageFile(ctx)
5252
return
5353
}
54-
ctx.NotFound(nil)
54+
http.NotFound(ctx.Resp, ctx.Req)
5555
}
5656

5757
func EnumeratePackages(ctx *context.Context) {

templates/devtest/flex-list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<a class="text primary" href="{{$.Link}}">
6969
gitea-org / gitea
7070
</a>
71-
<span data-tooltip-content="{{ctx.Locale.Tr "repo.fork"}}">{{svg "octicon-repo-forked"}}</span>
71+
<span class="flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.fork"}}">{{svg "octicon-repo-forked"}}</span>
7272
</div>
7373
<div class="flex-item-trailing">
7474
<a class="muted" href="{{$.Link}}">

templates/repo/settings/webhook/history.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{{else}}
2727
<span class="text red">{{svg "octicon-alert"}}</span>
2828
{{end}}
29-
<a class="ui primary sha label toggle button show-panel" data-panel="#info-{{.ID}}">{{.UUID}}</a>
29+
<button class="btn interact-bg tw-p-2 toggle show-panel" data-panel="#info-{{.ID}}">{{.UUID}}</button>
3030
</div>
3131
<span class="text grey">
3232
{{DateUtils.TimeSince .Delivered}}

templates/shared/issuelist.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</div>
3636
{{end}}
3737
</div>
38-
<div class="flex-item-body tw-mt-1">
38+
<div class="flex-item-body">
3939
<a class="index" href="{{if .Link}}{{.Link}}{{else}}{{$.Link}}/{{.Index}}{{end}}">
4040
{{if eq $.listType "dashboard"}}
4141
{{.Repo.FullName}}#{{.Index}}

web_src/css/shared/flex-list.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.flex-item .flex-item-main {
1818
display: flex;
1919
flex-direction: column;
20+
gap: 0.25em;
2021
flex-grow: 1;
2122
flex-basis: 60%; /* avoid wrapping the "flex-item-trailing" too aggressively */
2223
min-width: 0; /* make the "text truncate" work, otherwise the flex axis is not limited and the text just overflows */

0 commit comments

Comments
 (0)