Skip to content

Commit 5bf1438

Browse files
committed
fix
1 parent 241f799 commit 5bf1438

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

models/perm/access/repo_permission.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (p *Permission) ReadableUnitTypes() []unit.Type {
152152
}
153153

154154
func (p *Permission) LogString() string {
155-
format := "<Permission AccessMode=%s, %d Units, %d UnitsMode(s): [ "
155+
format := "<Permission AccessMode=%s, %d Units, %d UnitsMode(s): ["
156156
args := []any{p.AccessMode.ToString(), len(p.units), len(p.unitsMode)}
157157

158158
for i, u := range p.units {
@@ -164,14 +164,16 @@ func (p *Permission) LogString() string {
164164
config = err.Error()
165165
}
166166
}
167-
format += "\nUnits[%d]: ID: %d RepoID: %d Type: %s Config: %s"
167+
format += "\n\tunits[%d]: ID=%d RepoID=%d Type=%s Config=%s"
168168
args = append(args, i, u.ID, u.RepoID, u.Type.LogString(), config)
169169
}
170170
for key, value := range p.unitsMode {
171-
format += "\nUnitMode[%-v]: %-v"
171+
format += "\n\tunitsMode[%-v]: %-v"
172172
args = append(args, key.LogString(), value.LogString())
173173
}
174-
format += " ]>"
174+
format += "\n\teveryoneAccessMode: %-v"
175+
args = append(args, p.everyoneAccessMode)
176+
format += "\n\t]>"
175177
return fmt.Sprintf(format, args...)
176178
}
177179

modules/log/logger_impl.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package log
55

66
import (
77
"context"
8+
"reflect"
89
"runtime"
910
"strings"
1011
"sync"
@@ -175,6 +176,20 @@ func (l *LoggerImpl) IsEnabled() bool {
175176
return l.level.Load() < int32(FATAL) && len(l.eventWriters) > 0
176177
}
177178

179+
func asLogStringer(v any) LogStringer {
180+
if s, ok := v.(LogStringer); ok {
181+
return s
182+
} else if a := reflect.ValueOf(v); a.Kind() == reflect.Struct {
183+
// in case the receiver is a pointer, but the value is a struct
184+
vp := reflect.New(a.Type())
185+
vp.Elem().Set(a)
186+
if s, ok := vp.Interface().(LogStringer); ok {
187+
return s
188+
}
189+
}
190+
return nil
191+
}
192+
178193
// Log prepares the log event, if the level matches, the event will be sent to the writers
179194
func (l *LoggerImpl) Log(skip int, level Level, format string, logArgs ...any) {
180195
if Level(l.level.Load()) > level {
@@ -207,11 +222,11 @@ func (l *LoggerImpl) Log(skip int, level Level, format string, logArgs ...any) {
207222
// handle LogStringer values
208223
for i, v := range msgArgs {
209224
if cv, ok := v.(*ColoredValue); ok {
210-
if s, ok := cv.v.(LogStringer); ok {
211-
cv.v = logStringFormatter{v: s}
225+
if ls := asLogStringer(cv.v); ls != nil {
226+
cv.v = logStringFormatter{v: ls}
212227
}
213-
} else if s, ok := v.(LogStringer); ok {
214-
msgArgs[i] = logStringFormatter{v: s}
228+
} else if ls := asLogStringer(v); ls != nil {
229+
msgArgs[i] = logStringFormatter{v: ls}
215230
}
216231
}
217232

modules/log/logger_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ func (t testLogString) LogString() string {
116116
return "log-string"
117117
}
118118

119+
type testLogStringPtrReceiver struct {
120+
Field string
121+
}
122+
123+
func (t *testLogStringPtrReceiver) LogString() string {
124+
return "log-string-ptr-receiver"
125+
}
126+
119127
func TestLoggerLogString(t *testing.T) {
120128
logger := NewLoggerWithWriters(context.Background(), "test")
121129

@@ -124,9 +132,13 @@ func TestLoggerLogString(t *testing.T) {
124132
logger.AddWriters(w1)
125133

126134
logger.Info("%s %s %#v %v", testLogString{}, &testLogString{}, testLogString{Field: "detail"}, NewColoredValue(testLogString{}, FgRed))
135+
logger.Info("%s %s %#v %v", testLogStringPtrReceiver{}, &testLogStringPtrReceiver{}, testLogStringPtrReceiver{Field: "detail"}, NewColoredValue(testLogStringPtrReceiver{}, FgRed))
127136
logger.Close()
128137

129-
assert.Equal(t, []string{"log-string log-string log.testLogString{Field:\"detail\"} \x1b[31mlog-string\x1b[0m\n"}, w1.GetLogs())
138+
assert.Equal(t, []string{
139+
"log-string log-string log.testLogString{Field:\"detail\"} \x1b[31mlog-string\x1b[0m\n",
140+
"log-string-ptr-receiver log-string-ptr-receiver &log.testLogStringPtrReceiver{Field:\"detail\"} \x1b[31mlog-string-ptr-receiver\x1b[0m\n",
141+
}, w1.GetLogs())
130142
}
131143

132144
func TestLoggerExpressionFilter(t *testing.T) {

routers/web/web.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,16 +1196,17 @@ func registerRoutes(m *web.Router) {
11961196
})
11971197
})
11981198
}
1199+
// FIXME: many "pulls" requests are sent to "issues" endpoints correctly, so the issue endpoints have to tolerate pull request permissions at the moment
1200+
m.Group("/{username}/{reponame}/{type:issues}", addIssuesPullsViewRoutes, optSignIn, context.RepoAssignment, context.RequireUnitReader(unit.TypeIssues, unit.TypePullRequests))
1201+
m.Group("/{username}/{reponame}/{type:pulls}", addIssuesPullsViewRoutes, optSignIn, context.RepoAssignment, reqUnitPullsReader)
1202+
11991203
m.Group("/{username}/{reponame}", func() {
12001204
m.Get("/comments/{id}/attachments", repo.GetCommentAttachments)
12011205
m.Get("/labels", repo.RetrieveLabelsForList, repo.Labels)
12021206
m.Get("/milestones", repo.Milestones)
12031207
m.Get("/milestone/{id}", context.RepoRef(), repo.MilestoneIssuesAndPulls)
12041208
m.Get("/issues/suggestions", repo.IssueSuggestions)
12051209
}, optSignIn, context.RepoAssignment, reqRepoIssuesOrPullsReader) // issue/pull attachments, labels, milestones
1206-
1207-
m.Group("/{username}/{reponame}/{type:issues}", addIssuesPullsViewRoutes, optSignIn, context.RepoAssignment, reqUnitIssuesReader)
1208-
m.Group("/{username}/{reponame}/{type:pulls}", addIssuesPullsViewRoutes, optSignIn, context.RepoAssignment, reqUnitPullsReader)
12091210
// end "/{username}/{reponame}": view milestone, label, issue, pull, etc
12101211

12111212
m.Group("/{username}/{reponame}/{type:issues}", func() {
@@ -1224,7 +1225,7 @@ func registerRoutes(m *web.Router) {
12241225
m.Get("/search", repo.SearchRepoIssuesJSON)
12251226
}, reqUnitIssuesReader)
12261227

1227-
addIssuesPullsRoutes := func() {
1228+
addIssuesPullsUpdateRoutes := func() {
12281229
// for "/{username}/{reponame}/issues" or "/{username}/{reponame}/pulls"
12291230
m.Group("/{index}", func() {
12301231
m.Post("/title", repo.UpdateIssueTitle)
@@ -1267,8 +1268,9 @@ func registerRoutes(m *web.Router) {
12671268
m.Delete("/unpin/{index}", reqRepoAdmin, repo.IssueUnpin)
12681269
m.Post("/move_pin", reqRepoAdmin, repo.IssuePinMove)
12691270
}
1270-
m.Group("/{type:issues}", addIssuesPullsRoutes, reqUnitIssuesReader, context.RepoMustNotBeArchived())
1271-
m.Group("/{type:pulls}", addIssuesPullsRoutes, reqUnitPullsReader, context.RepoMustNotBeArchived())
1271+
// FIXME: many "pulls" requests are sent to "issues" endpoints correctly, so the issue endpoints have to tolerate pull request permissions at the moment
1272+
m.Group("/{type:issues}", addIssuesPullsUpdateRoutes, context.RequireUnitReader(unit.TypeIssues, unit.TypePullRequests), context.RepoMustNotBeArchived())
1273+
m.Group("/{type:pulls}", addIssuesPullsUpdateRoutes, reqUnitPullsReader, context.RepoMustNotBeArchived())
12721274

12731275
m.Group("/comments/{id}", func() {
12741276
m.Post("", repo.UpdateCommentContent)
@@ -1292,7 +1294,7 @@ func registerRoutes(m *web.Router) {
12921294
m.Post("/delete", repo.DeleteMilestone)
12931295
}, reqRepoIssuesOrPullsWriter, context.RepoRef())
12941296

1295-
// FIXME: need to move these routes to the proper place
1297+
// FIXME: many "pulls" requests are sent to "issues" endpoints incorrectly, need to move these routes to the proper place
12961298
m.Group("/issues", func() {
12971299
m.Post("/request_review", repo.UpdatePullReviewRequest)
12981300
m.Post("/dismiss_review", reqRepoAdmin, web.Bind(forms.DismissReviewForm{}), repo.DismissReview)

0 commit comments

Comments
 (0)