diff options
author | 6543 <6543@obermui.de> | 2020-12-25 09:59:32 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-25 11:59:32 +0200 |
commit | a19447aed128ecadfcd938d6a80cd4951af1f4ce (patch) | |
tree | 6312bf946d601aab29731645a4777feeaea66036 /models | |
parent | 04ae0f2f3f4556c6d0b4adc5f2cffd0cc7d25151 (diff) | |
download | gitea-a19447aed128ecadfcd938d6a80cd4951af1f4ce.tar.gz gitea-a19447aed128ecadfcd938d6a80cd4951af1f4ce.zip |
migrate from com.* to alternatives (#14103)
* remove github.com/unknwon/com from models
* dont use "com.ToStr()"
* replace "com.ToStr" with "fmt.Sprint" where its easy to do
* more refactor
* fix test
* just "proxy" Copy func for now
* as per @lunny
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 5 | ||||
-rw-r--r-- | models/admin.go | 4 | ||||
-rw-r--r-- | models/branches.go | 3 | ||||
-rw-r--r-- | models/issue.go | 6 | ||||
-rw-r--r-- | models/issue_comment.go | 3 | ||||
-rw-r--r-- | models/issue_xref.go | 3 | ||||
-rw-r--r-- | models/login_source.go | 11 | ||||
-rw-r--r-- | models/oauth2_application.go | 4 | ||||
-rw-r--r-- | models/repo.go | 15 | ||||
-rw-r--r-- | models/repo_unit.go | 4 | ||||
-rw-r--r-- | models/ssh_key.go | 12 | ||||
-rw-r--r-- | models/unit_tests.go | 7 | ||||
-rw-r--r-- | models/user.go | 7 |
13 files changed, 41 insertions, 43 deletions
diff --git a/models/action.go b/models/action.go index c39fdc397a..ccf161192e 100644 --- a/models/action.go +++ b/models/action.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" - "github.com/unknwon/com" "xorm.io/builder" ) @@ -266,7 +265,7 @@ func (a *Action) GetIssueInfos() []string { // GetIssueTitle returns the title of first issue associated // with the action. func (a *Action) GetIssueTitle() string { - index := com.StrTo(a.GetIssueInfos()[0]).MustInt64() + index, _ := strconv.ParseInt(a.GetIssueInfos()[0], 10, 64) issue, err := GetIssueByIndex(a.RepoID, index) if err != nil { log.Error("GetIssueByIndex: %v", err) @@ -278,7 +277,7 @@ func (a *Action) GetIssueTitle() string { // GetIssueContent returns the content of first issue associated with // this action. func (a *Action) GetIssueContent() string { - index := com.StrTo(a.GetIssueInfos()[0]).MustInt64() + index, _ := strconv.ParseInt(a.GetIssueInfos()[0], 10, 64) issue, err := GetIssueByIndex(a.RepoID, index) if err != nil { log.Error("GetIssueByIndex: %v", err) diff --git a/models/admin.go b/models/admin.go index 903e35b0c9..420adbcda9 100644 --- a/models/admin.go +++ b/models/admin.go @@ -12,8 +12,6 @@ import ( "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - - "github.com/unknwon/com" ) //NoticeType describes the notice type @@ -36,7 +34,7 @@ type Notice struct { // TrStr returns a translation format string. func (n *Notice) TrStr() string { - return "admin.notices.type_" + com.ToStr(n.Type) + return fmt.Sprintf("admin.notices.type_%d", n.Type) } // CreateNotice creates new system notice. diff --git a/models/branches.go b/models/branches.go index 144767ee95..80df8c433e 100644 --- a/models/branches.go +++ b/models/branches.go @@ -16,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/gobwas/glob" - "github.com/unknwon/com" ) // ProtectedBranch struct @@ -483,7 +482,7 @@ func updateTeamWhitelist(repo *Repository, currentWhitelist, newWhitelist []int6 whitelist = make([]int64, 0, len(teams)) for i := range teams { - if com.IsSliceContainsInt64(newWhitelist, teams[i].ID) { + if util.IsInt64InSlice(teams[i].ID, newWhitelist) { whitelist = append(whitelist, teams[i].ID) } } diff --git a/models/issue.go b/models/issue.go index 787d873f24..3161fd3e82 100644 --- a/models/issue.go +++ b/models/issue.go @@ -20,7 +20,6 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - "github.com/unknwon/com" "xorm.io/builder" "xorm.io/xorm" ) @@ -386,7 +385,7 @@ func (issue *Issue) State() api.StateType { // HashTag returns unique hash tag for issue. func (issue *Issue) HashTag() string { - return "issue-" + com.ToStr(issue.ID) + return fmt.Sprintf("issue-%d", issue.ID) } // IsPoster returns true if given user by ID is the poster. @@ -1374,7 +1373,8 @@ func parseCountResult(results []map[string][]byte) int64 { return 0 } for _, result := range results[0] { - return com.StrTo(string(result)).MustInt64() + c, _ := strconv.ParseInt(string(result), 10, 64) + return c } return 0 } diff --git a/models/issue_comment.go b/models/issue_comment.go index 7bcea40b93..a5cb91ac75 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -22,7 +22,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" - "github.com/unknwon/com" "xorm.io/builder" "xorm.io/xorm" ) @@ -367,7 +366,7 @@ func (c *Comment) HashTag() string { // EventTag returns unique event hash tag for comment. func (c *Comment) EventTag() string { - return "event-" + com.ToStr(c.ID) + return fmt.Sprintf("event-%d", c.ID) } // LoadLabel if comment.Type is CommentTypeLabel, then load Label diff --git a/models/issue_xref.go b/models/issue_xref.go index 104691fe93..7834b6ecc0 100644 --- a/models/issue_xref.go +++ b/models/issue_xref.go @@ -10,7 +10,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/references" - "github.com/unknwon/com" "xorm.io/xorm" ) @@ -324,7 +323,7 @@ func (comment *Comment) RefIssueIdent() string { return "" } // FIXME: check this name for cross-repository references (#7901 if it gets merged) - return "#" + com.ToStr(comment.RefIssue.Index) + return fmt.Sprintf("#%d", comment.RefIssue.Index) } // __________ .__ .__ __________ __ diff --git a/models/login_source.go b/models/login_source.go index 1de24a9cf7..4f577d6ca7 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -12,6 +12,7 @@ import ( "fmt" "net/smtp" "net/textproto" + "strconv" "strings" "code.gitea.io/gitea/modules/auth/ldap" @@ -20,8 +21,8 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" - "github.com/unknwon/com" "xorm.io/xorm" "xorm.io/xorm/convert" ) @@ -180,7 +181,9 @@ func Cell2Int64(val xorm.Cell) int64 { switch (*val).(type) { case []uint8: log.Trace("Cell2Int64 ([]uint8): %v", *val) - return com.StrTo(string((*val).([]uint8))).MustInt64() + + v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64) + return v } return (*val).(int64) } @@ -200,7 +203,7 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { case LoginSSPI: source.Cfg = new(SSPIConfig) default: - panic("unrecognized login source type: " + com.ToStr(*val)) + panic(fmt.Sprintf("unrecognized login source type: %v", *val)) } } } @@ -610,7 +613,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC idx := strings.Index(login, "@") if idx == -1 { return nil, ErrUserNotExist{0, login, 0} - } else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) { + } else if !util.IsStringInSlice(login[idx+1:], strings.Split(cfg.AllowedDomains, ","), true) { return nil, ErrUserNotExist{0, login, 0} } } diff --git a/models/oauth2_application.go b/models/oauth2_application.go index 1c7937685c..af4d280d0c 100644 --- a/models/oauth2_application.go +++ b/models/oauth2_application.go @@ -14,10 +14,10 @@ import ( "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "github.com/dgrijalva/jwt-go" uuid "github.com/google/uuid" - "github.com/unknwon/com" "golang.org/x/crypto/bcrypt" "xorm.io/xorm" ) @@ -62,7 +62,7 @@ func (app *OAuth2Application) LoadUser() (err error) { // ContainsRedirectURI checks if redirectURI is allowed for app func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool { - return com.IsSliceContainsStr(app.RedirectURIs, redirectURI) + return util.IsStringInSlice(redirectURI, app.RedirectURIs, true) } // GenerateClientSecret will generate the client secret and returns the plaintext and saves the hash at the database diff --git a/models/repo.go b/models/repo.go index d791b6cbd2..7dfe14c85b 100644 --- a/models/repo.go +++ b/models/repo.go @@ -34,7 +34,6 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - "github.com/unknwon/com" "xorm.io/builder" ) @@ -85,7 +84,7 @@ func loadRepoConfig() { } for _, f := range customFiles { - if !com.IsSliceContainsStr(files, f) { + if !util.IsStringInSlice(f, files, true) { files = append(files, f) } } @@ -115,12 +114,12 @@ func loadRepoConfig() { // Filter out invalid names and promote preferred licenses. sortedLicenses := make([]string, 0, len(Licenses)) for _, name := range setting.Repository.PreferredLicenses { - if com.IsSliceContainsStr(Licenses, name) { + if util.IsStringInSlice(name, Licenses, true) { sortedLicenses = append(sortedLicenses, name) } } for _, name := range Licenses { - if !com.IsSliceContainsStr(setting.Repository.PreferredLicenses, name) { + if !util.IsStringInSlice(name, setting.Repository.PreferredLicenses, true) { sortedLicenses = append(sortedLicenses, name) } } @@ -1933,7 +1932,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) { return } for _, result := range results { - id := com.StrTo(result["id"]).MustInt64() + id, _ := strconv.ParseInt(string(result["id"]), 10, 64) select { case <-ctx.Done(): log.Warn("CheckRepoStats: Cancelled before checking %s for Repo[%d]", checker.desc, id) @@ -2001,7 +2000,7 @@ func CheckRepoStats(ctx context.Context) error { log.Error("Select %s: %v", desc, err) } else { for _, result := range results { - id := com.StrTo(result["id"]).MustInt64() + id, _ := strconv.ParseInt(string(result["id"]), 10, 64) select { case <-ctx.Done(): log.Warn("CheckRepoStats: Cancelled during %s for repo ID %d", desc, id) @@ -2024,7 +2023,7 @@ func CheckRepoStats(ctx context.Context) error { log.Error("Select %s: %v", desc, err) } else { for _, result := range results { - id := com.StrTo(result["id"]).MustInt64() + id, _ := strconv.ParseInt(string(result["id"]), 10, 64) select { case <-ctx.Done(): log.Warn("CheckRepoStats: Cancelled") @@ -2047,7 +2046,7 @@ func CheckRepoStats(ctx context.Context) error { log.Error("Select repository count 'num_forks': %v", err) } else { for _, result := range results { - id := com.StrTo(result["id"]).MustInt64() + id, _ := strconv.ParseInt(string(result["id"]), 10, 64) select { case <-ctx.Done(): log.Warn("CheckRepoStats: Cancelled") diff --git a/models/repo_unit.go b/models/repo_unit.go index b208276d9d..8c2d458758 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -6,10 +6,10 @@ package models import ( "encoding/json" + "fmt" "code.gitea.io/gitea/modules/timeutil" - "github.com/unknwon/com" "xorm.io/xorm" "xorm.io/xorm/convert" ) @@ -147,7 +147,7 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) { case UnitTypeIssues: r.Config = new(IssuesConfig) default: - panic("unrecognized repo unit type: " + com.ToStr(*val)) + panic(fmt.Sprintf("unrecognized repo unit type: %v", *val)) } } } diff --git a/models/ssh_key.go b/models/ssh_key.go index f13fc61914..b2e4326559 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -20,6 +20,7 @@ import ( "math/big" "os" "path/filepath" + "strconv" "strings" "sync" "time" @@ -30,7 +31,6 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - "github.com/unknwon/com" "golang.org/x/crypto/ssh" "xorm.io/builder" "xorm.io/xorm" @@ -252,7 +252,11 @@ func SSHKeyGenParsePublicKey(key string) (string, int, error) { } keyType := strings.Trim(fields[len(fields)-1], "()\r\n") - return strings.ToLower(keyType), com.StrTo(fields[0]).MustInt(), nil + length, err := strconv.ParseInt(fields[0], 10, 32) + if err != nil { + return "", 0, err + } + return strings.ToLower(keyType), int(length), nil } // SSHNativeParsePublicKey extracts the key type and length using the golang SSH library. @@ -744,7 +748,7 @@ func rewriteAllPublicKeys(e Engine) error { } if isExist { bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) - if err = com.Copy(fPath, bakPath); err != nil { + if err = util.CopyFile(fPath, bakPath); err != nil { return err } } @@ -1226,7 +1230,7 @@ func rewriteAllPrincipalKeys(e Engine) error { } if isExist { bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) - if err = com.Copy(fPath, bakPath); err != nil { + if err = util.CopyFile(fPath, bakPath); err != nil { return err } } diff --git a/models/unit_tests.go b/models/unit_tests.go index 7254cbf66b..3299630634 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -19,7 +19,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" - "github.com/unknwon/com" "xorm.io/xorm" "xorm.io/xorm/names" ) @@ -82,8 +81,8 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { if err = util.RemoveAll(setting.RepoRootPath); err != nil { fatalTestError("util.RemoveAll: %v\n", err) } - if err = com.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { - fatalTestError("com.CopyDir: %v\n", err) + if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { + fatalTestError("util.CopyDir: %v\n", err) } exitStatus := m.Run() @@ -126,7 +125,7 @@ func PrepareTestEnv(t testing.TB) { assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta") - assert.NoError(t, com.CopyDir(metaPath, setting.RepoRootPath)) + assert.NoError(t, util.CopyDir(metaPath, setting.RepoRootPath)) base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set } diff --git a/models/user.go b/models/user.go index 4bf9e196a0..73178f256c 100644 --- a/models/user.go +++ b/models/user.go @@ -32,7 +32,6 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - "github.com/unknwon/com" "golang.org/x/crypto/argon2" "golang.org/x/crypto/bcrypt" "golang.org/x/crypto/pbkdf2" @@ -315,7 +314,7 @@ func (u *User) HTMLURL() string { // GenerateEmailActivateCode generates an activate code based on user information and given e-mail. func (u *User) GenerateEmailActivateCode(email string) string { code := base.CreateTimeLimitCode( - com.ToStr(u.ID)+email+u.LowerName+u.Passwd+u.Rands, + fmt.Sprintf("%d%s%s%s%s", u.ID, email, u.LowerName, u.Passwd, u.Rands), setting.Service.ActiveCodeLives, nil) // Add tail hex username @@ -880,7 +879,7 @@ func VerifyUserActiveCode(code string) (user *User) { if user = getVerifyUser(code); user != nil { // time limit code prefix := code[:base.TimeLimitCodeLength] - data := com.ToStr(user.ID) + user.Email + user.LowerName + user.Passwd + user.Rands + data := fmt.Sprintf("%d%s%s%s%s", user.ID, user.Email, user.LowerName, user.Passwd, user.Rands) if base.VerifyTimeLimitCode(data, minutes, prefix) { return user @@ -896,7 +895,7 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress { if user := getVerifyUser(code); user != nil { // time limit code prefix := code[:base.TimeLimitCodeLength] - data := com.ToStr(user.ID) + email + user.LowerName + user.Passwd + user.Rands + data := fmt.Sprintf("%d%s%s%s%s", user.ID, email, user.LowerName, user.Passwd, user.Rands) if base.VerifyTimeLimitCode(data, minutes, prefix) { emailAddress := &EmailAddress{UID: user.ID, Email: email} |