Skip to content

Commit

Permalink
migrate from com.* to alternatives (go-gitea#14103)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
6543 authored Dec 25, 2020
1 parent 04ae0f2 commit a19447a
Show file tree
Hide file tree
Showing 46 changed files with 231 additions and 221 deletions.
6 changes: 4 additions & 2 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/dgrijalva/jwt-go"
"github.com/kballard/go-shellquote"
"github.com/unknwon/com"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -105,7 +104,10 @@ func runServ(c *cli.Context) error {
if len(keys) != 2 || keys[0] != "key" {
fail("Key ID format error", "Invalid key argument: %s", c.Args()[0])
}
keyID := com.StrTo(keys[1]).MustInt64()
keyID, err := strconv.ParseInt(keys[1], 10, 64)
if err != nil {
fail("Key ID format error", "Invalid key argument: %s", c.Args()[1])
}

cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
if len(cmd) == 0 {
Expand Down
3 changes: 1 addition & 2 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
context2 "github.com/gorilla/context"
"github.com/unknwon/com"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -111,7 +110,7 @@ func runPR() {
models.LoadFixtures()
util.RemoveAll(setting.RepoRootPath)
util.RemoveAll(models.LocalCopyPath())
com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)

log.Printf("[PR] Setting up router\n")
//routers.GlobalInit()
Expand Down
9 changes: 6 additions & 3 deletions integrations/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"code.gitea.io/gitea/modules/util"

"github.com/stretchr/testify/assert"
"github.com/unknwon/com"
)

func withKeyFile(t *testing.T, keyname string, callback func(string)) {
Expand Down Expand Up @@ -112,7 +111,9 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
return func(t *testing.T) {
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
assert.NoError(t, err)
assert.True(t, exist)
}
}

Expand All @@ -122,7 +123,9 @@ func doGitCloneFail(u *url.URL) func(*testing.T) {
assert.NoError(t, err)
defer util.RemoveAll(tmpDir)
assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
assert.False(t, com.IsExist(filepath.Join(tmpDir, "README.md")))
exist, err := util.IsExist(filepath.Join(tmpDir, "README.md"))
assert.NoError(t, err)
assert.False(t, exist)
}
}

Expand Down
7 changes: 2 additions & 5 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/go-chi/chi"
"github.com/stretchr/testify/assert"
"github.com/unknwon/com"
)

var c chi.Router
Expand Down Expand Up @@ -231,8 +230,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
assert.NoError(t, models.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))

assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
setting.RepoRootPath))
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
return deferFn
}

Expand Down Expand Up @@ -473,6 +471,5 @@ func resetFixtures(t *testing.T) {
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
assert.NoError(t, models.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
setting.RepoRootPath))
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
}
3 changes: 1 addition & 2 deletions integrations/migration-test/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"code.gitea.io/gitea/modules/util"

"github.com/stretchr/testify/assert"
"github.com/unknwon/com"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -60,7 +59,7 @@ func initMigrationTest(t *testing.T) func() {

assert.True(t, len(setting.RepoRootPath) != 0)
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))

setting.CheckLFSVersion()
setting.InitDBConfig()
Expand Down
5 changes: 2 additions & 3 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"

"github.com/unknwon/com"
"xorm.io/builder"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions models/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/util"

"github.com/gobwas/glob"
"github.com/unknwon/com"
)

// ProtectedBranch struct
Expand Down Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions models/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/references"

"github.com/unknwon/com"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -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)
}

// __________ .__ .__ __________ __
Expand Down
11 changes: 7 additions & 4 deletions models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"net/smtp"
"net/textproto"
"strconv"
"strings"

"code.gitea.io/gitea/modules/auth/ldap"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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))
}
}
}
Expand Down Expand Up @@ -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}
}
}
Expand Down
4 changes: 2 additions & 2 deletions models/oauth2_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"github.com/unknwon/com"
"xorm.io/builder"
)

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
}
}
}
Expand Down
Loading

0 comments on commit a19447a

Please sign in to comment.