Skip to content

Commit 65f17bf

Browse files
authored
Refactor legacy unknwon/com package, improve golangci lint (#19284)
The main purpose is to refactor the legacy `unknwon/com` package. 1. Remove most imports of `unknwon/com`, only `util/legacy.go` imports the legacy `unknwon/com` 2. Use golangci's depguard to process denied packages 3. Fix some incorrect values in golangci.yml, eg, the version should be quoted string `"1.18"` 4. Use correctly escaped content for `go-import` and `go-source` meta tags 5. Refactor `com.Expand` to our stable (and the same fast) `vars.Expand`, our `vars.Expand` can still return partially rendered content even if the template is not good (eg: key mistach).
1 parent 5b74660 commit 65f17bf

File tree

22 files changed

+397
-81
lines changed

22 files changed

+397
-81
lines changed

.golangci.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ linters:
1818
- ineffassign
1919
- revive
2020
- gofumpt
21+
- depguard
2122
enable-all: false
2223
disable-all: true
2324
fast: false
@@ -65,7 +66,15 @@ linters-settings:
6566
- name: modifies-value-receiver
6667
gofumpt:
6768
extra-rules: true
68-
lang-version: 1.18
69+
lang-version: "1.18"
70+
depguard:
71+
# TODO: use depguard to replace import checks in gitea-vet
72+
list-type: denylist
73+
# Check the list against standard lib.
74+
include-go-root: true
75+
packages-with-error-message:
76+
- encoding/json: "use gitea's modules/json instead of encoding/json"
77+
- github.com/unknwon/com: "use gitea's util and replacements"
6978

7079
issues:
7180
exclude-rules:
@@ -153,5 +162,6 @@ issues:
153162
- path: models/user/openid.go
154163
linters:
155164
- golint
156-
- linters: staticcheck
165+
- linters:
166+
- staticcheck
157167
text: "strings.Title is deprecated: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead."

integrations/goget_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ func TestGoGet(t *testing.T) {
2929
<body>
3030
go get --insecure %[1]s:%[2]s/blah/glah
3131
</body>
32-
</html>
33-
`, setting.Domain, setting.HTTPPort, setting.AppURL)
32+
</html>`, setting.Domain, setting.HTTPPort, setting.AppURL)
3433

3534
assert.Equal(t, expected, resp.Body.String())
3635
}

models/migrations/migrations_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"code.gitea.io/gitea/modules/util"
2525

2626
"github.com/stretchr/testify/assert"
27-
"github.com/unknwon/com"
2827
"xorm.io/xorm"
2928
"xorm.io/xorm/names"
3029
)
@@ -204,7 +203,7 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
204203
deferFn := PrintCurrentTest(t, ourSkip)
205204
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
206205

207-
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
206+
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
208207
setting.RepoRootPath))
209208
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
210209
if err != nil {

modules/cache/cache_redis.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010

1111
"code.gitea.io/gitea/modules/graceful"
1212
"code.gitea.io/gitea/modules/nosql"
13+
"code.gitea.io/gitea/modules/util"
1314

1415
"gitea.com/go-chi/cache"
1516
"github.com/go-redis/redis/v8"
16-
"github.com/unknwon/com"
1717
)
1818

1919
// RedisCacher represents a redis cache adapter implementation.
@@ -29,15 +29,15 @@ type RedisCacher struct {
2929
func (c *RedisCacher) Put(key string, val interface{}, expire int64) error {
3030
key = c.prefix + key
3131
if expire == 0 {
32-
if err := c.c.Set(graceful.GetManager().HammerContext(), key, com.ToStr(val), 0).Err(); err != nil {
32+
if err := c.c.Set(graceful.GetManager().HammerContext(), key, util.ToStr(val), 0).Err(); err != nil {
3333
return err
3434
}
3535
} else {
36-
dur, err := time.ParseDuration(com.ToStr(expire) + "s")
36+
dur, err := time.ParseDuration(util.ToStr(expire) + "s")
3737
if err != nil {
3838
return err
3939
}
40-
if err = c.c.Set(graceful.GetManager().HammerContext(), key, com.ToStr(val), dur).Err(); err != nil {
40+
if err = c.c.Set(graceful.GetManager().HammerContext(), key, util.ToStr(val), dur).Err(); err != nil {
4141
return err
4242
}
4343
}

modules/context/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ import (
3131
"code.gitea.io/gitea/modules/setting"
3232
"code.gitea.io/gitea/modules/templates"
3333
"code.gitea.io/gitea/modules/translation"
34+
"code.gitea.io/gitea/modules/util"
3435
"code.gitea.io/gitea/modules/web/middleware"
3536
"code.gitea.io/gitea/services/auth"
3637

3738
"gitea.com/go-chi/cache"
3839
"gitea.com/go-chi/session"
3940
chi "github.com/go-chi/chi/v5"
40-
"github.com/unknwon/com"
4141
"github.com/unrolled/render"
4242
"golang.org/x/crypto/pbkdf2"
4343
)
@@ -475,7 +475,7 @@ func (ctx *Context) CookieDecrypt(secret, val string) (string, bool) {
475475
}
476476

477477
key := pbkdf2.Key([]byte(secret), []byte(secret), 1000, 16, sha256.New)
478-
text, err = com.AESGCMDecrypt(key, text)
478+
text, err = util.AESGCMDecrypt(key, text)
479479
return string(text), err == nil
480480
}
481481

@@ -489,7 +489,7 @@ func (ctx *Context) SetSuperSecureCookie(secret, name, value string, expiry int)
489489
// CookieEncrypt encrypts a given value using the provided secret
490490
func (ctx *Context) CookieEncrypt(secret, value string) string {
491491
key := pbkdf2.Key([]byte(secret), []byte(secret), 1000, 16, sha256.New)
492-
text, err := com.AESGCMEncrypt(key, []byte(value))
492+
text, err := util.AESGCMEncrypt(key, []byte(value))
493493
if err != nil {
494494
panic("error encrypting cookie: " + err.Error())
495495
}

modules/context/csrf.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
package context
2020

2121
import (
22+
"encoding/base32"
23+
"fmt"
2224
"net/http"
2325
"time"
2426

2527
"code.gitea.io/gitea/modules/setting"
28+
"code.gitea.io/gitea/modules/util"
2629
"code.gitea.io/gitea/modules/web/middleware"
27-
28-
"github.com/unknwon/com"
2930
)
3031

3132
// CSRF represents a CSRF service and is used to get the current token and validate a suspect token.
@@ -162,7 +163,12 @@ func prepareOptions(options []CsrfOptions) CsrfOptions {
162163

163164
// Defaults.
164165
if len(opt.Secret) == 0 {
165-
opt.Secret = string(com.RandomCreateBytes(10))
166+
randBytes, err := util.CryptoRandomBytes(8)
167+
if err != nil {
168+
// this panic can be handled by the recover() in http handlers
169+
panic(fmt.Errorf("failed to generate random bytes: %w", err))
170+
}
171+
opt.Secret = base32.StdEncoding.EncodeToString(randBytes)
166172
}
167173
if len(opt.Header) == 0 {
168174
opt.Header = "X-CSRFToken"
@@ -211,7 +217,7 @@ func Csrfer(opt CsrfOptions, ctx *Context) CSRF {
211217
x.ID = "0"
212218
uid := ctx.Session.Get(opt.SessionKey)
213219
if uid != nil {
214-
x.ID = com.ToStr(uid)
220+
x.ID = util.ToStr(uid)
215221
}
216222

217223
needsNew := false

modules/context/repo.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package context
88
import (
99
"context"
1010
"fmt"
11+
"html"
1112
"io"
1213
"net/http"
1314
"net/url"
@@ -29,7 +30,6 @@ import (
2930
asymkey_service "code.gitea.io/gitea/services/asymkey"
3031

3132
"github.com/editorconfig/editorconfig-core-go/v2"
32-
"github.com/unknwon/com"
3333
)
3434

3535
// IssueTemplateDirCandidates issue templates directory
@@ -308,11 +308,9 @@ func EarlyResponseForGoGetMeta(ctx *Context) {
308308
ctx.PlainText(http.StatusBadRequest, "invalid repository path")
309309
return
310310
}
311-
ctx.PlainText(http.StatusOK, com.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`,
312-
map[string]string{
313-
"GoGetImport": ComposeGoGetImport(username, reponame),
314-
"CloneLink": repo_model.ComposeHTTPSCloneURL(username, reponame),
315-
}))
311+
goImportContent := fmt.Sprintf("%s git %s", ComposeGoGetImport(username, reponame), repo_model.ComposeHTTPSCloneURL(username, reponame))
312+
htmlMeta := fmt.Sprintf(`<meta name="go-import" content="%s">`, html.EscapeString(goImportContent))
313+
ctx.PlainText(http.StatusOK, htmlMeta)
316314
}
317315

318316
// RedirectToRepo redirect to a differently-named repository

modules/json/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package json
88
import (
99
"bytes"
1010
"encoding/binary"
11-
"encoding/json"
11+
"encoding/json" //nolint:depguard
1212
"io"
1313

1414
jsoniter "github.com/json-iterator/go"

modules/markup/html.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"code.gitea.io/gitea/modules/markup/common"
2222
"code.gitea.io/gitea/modules/references"
2323
"code.gitea.io/gitea/modules/setting"
24+
"code.gitea.io/gitea/modules/templates/vars"
2425
"code.gitea.io/gitea/modules/util"
2526

26-
"github.com/unknwon/com"
2727
"golang.org/x/net/html"
2828
"golang.org/x/net/html/atom"
2929
"mvdan.cc/xurls/v2"
@@ -838,7 +838,14 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
838838
reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End]
839839
if exttrack && !ref.IsPull {
840840
ctx.Metas["index"] = ref.Issue
841-
link = createLink(com.Expand(ctx.Metas["format"], ctx.Metas), reftext, "ref-issue ref-external-issue")
841+
842+
res, err := vars.Expand(ctx.Metas["format"], ctx.Metas)
843+
if err != nil {
844+
// here we could just log the error and continue the rendering
845+
log.Error("unable to expand template vars for ref %s, err: %v", ref.Issue, err)
846+
}
847+
848+
link = createLink(res, reftext, "ref-issue ref-external-issue")
842849
} else {
843850
// Path determines the type of link that will be rendered. It's unknown at this point whether
844851
// the linked item is actually a PR or an issue. Luckily it's of no real consequence because

modules/repository/init.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import (
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/options"
2424
"code.gitea.io/gitea/modules/setting"
25+
"code.gitea.io/gitea/modules/templates/vars"
2526
"code.gitea.io/gitea/modules/util"
2627
asymkey_service "code.gitea.io/gitea/services/asymkey"
27-
28-
"github.com/unknwon/com"
2928
)
3029

3130
var (
@@ -250,8 +249,13 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
250249
"CloneURL.HTTPS": cloneLink.HTTPS,
251250
"OwnerName": repo.OwnerName,
252251
}
252+
res, err := vars.Expand(string(data), match)
253+
if err != nil {
254+
// here we could just log the error and continue the rendering
255+
log.Error("unable to expand template vars for repo README: %s, err: %v", opts.Readme, err)
256+
}
253257
if err = os.WriteFile(filepath.Join(tmpDir, "README.md"),
254-
[]byte(com.Expand(string(data), match)), 0o644); err != nil {
258+
[]byte(res), 0o644); err != nil {
255259
return fmt.Errorf("write README.md: %v", err)
256260
}
257261

modules/setting/setting.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"code.gitea.io/gitea/modules/user"
2828
"code.gitea.io/gitea/modules/util"
2929

30-
"github.com/unknwon/com"
3130
gossh "golang.org/x/crypto/ssh"
3231
ini "gopkg.in/ini.v1"
3332
)
@@ -612,7 +611,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
612611

613612
Cfg.NameMapper = ini.SnackCase
614613

615-
homeDir, err := com.HomeDir()
614+
homeDir, err := util.HomeDir()
616615
if err != nil {
617616
log.Fatal("Failed to get home directory: %v", err)
618617
}

modules/sync/unique_queue.go

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

66
package sync
77

8-
import (
9-
"github.com/unknwon/com"
10-
)
8+
import "code.gitea.io/gitea/modules/util"
119

1210
// UniqueQueue is a queue which guarantees only one instance of same
1311
// identity is in the line. Instances with same identity will be
@@ -73,13 +71,13 @@ func (q *UniqueQueue) Queue() <-chan string {
7371
// Exist returns true if there is an instance with given identity
7472
// exists in the queue.
7573
func (q *UniqueQueue) Exist(id interface{}) bool {
76-
return q.table.IsRunning(com.ToStr(id))
74+
return q.table.IsRunning(util.ToStr(id))
7775
}
7876

7977
// AddFunc adds new instance to the queue with a custom runnable function,
8078
// the queue is blocked until the function exits.
8179
func (q *UniqueQueue) AddFunc(id interface{}, fn func()) {
82-
idStr := com.ToStr(id)
80+
idStr := util.ToStr(id)
8381
q.table.lock.Lock()
8482
if _, ok := q.table.pool[idStr]; ok {
8583
q.table.lock.Unlock()
@@ -105,5 +103,5 @@ func (q *UniqueQueue) Add(id interface{}) {
105103

106104
// Remove removes instance from the queue.
107105
func (q *UniqueQueue) Remove(id interface{}) {
108-
q.table.Stop(com.ToStr(id))
106+
q.table.Stop(util.ToStr(id))
109107
}

0 commit comments

Comments
 (0)