Skip to content

Commit 465f940

Browse files
committed
more refactor
1 parent f93c66c commit 465f940

File tree

9 files changed

+45
-41
lines changed

9 files changed

+45
-41
lines changed

contrib/pr/checkout.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/go-git/go-git/v5/config"
3838
"github.com/go-git/go-git/v5/plumbing"
3939
context2 "github.com/gorilla/context"
40-
"github.com/unknwon/com"
4140
"xorm.io/xorm"
4241
)
4342

@@ -111,7 +110,7 @@ func runPR() {
111110
models.LoadFixtures()
112111
util.RemoveAll(setting.RepoRootPath)
113112
util.RemoveAll(models.LocalCopyPath())
114-
com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
113+
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
115114

116115
log.Printf("[PR] Setting up router\n")
117116
//routers.GlobalInit()

integrations/git_helper_for_declarative_test.go

Lines changed: 6 additions & 3 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
)
2928

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

@@ -122,7 +123,9 @@ func doGitCloneFail(u *url.URL) func(*testing.T) {
122123
assert.NoError(t, err)
123124
defer util.RemoveAll(tmpDir)
124125
assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
125-
assert.False(t, com.IsExist(filepath.Join(tmpDir, "README.md")))
126+
exist, err := util.IsExist(filepath.Join(tmpDir, "README.md"))
127+
assert.NoError(t, err)
128+
assert.False(t, exist)
126129
}
127130
}
128131

integrations/integration_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/PuerkitoBio/goquery"
3838
"github.com/go-chi/chi"
3939
"github.com/stretchr/testify/assert"
40-
"github.com/unknwon/com"
4140
)
4241

4342
var c chi.Router
@@ -231,7 +230,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
231230
assert.NoError(t, models.LoadFixtures())
232231
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
233232

234-
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
233+
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
235234
setting.RepoRootPath))
236235
return deferFn
237236
}
@@ -473,6 +472,6 @@ func resetFixtures(t *testing.T) {
473472
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
474473
assert.NoError(t, models.LoadFixtures())
475474
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
476-
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
475+
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
477476
setting.RepoRootPath))
478477
}

integrations/migration-test/migration_test.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/util"
2828

2929
"github.com/stretchr/testify/assert"
30-
"github.com/unknwon/com"
3130
"xorm.io/xorm"
3231
)
3332

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

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

6564
setting.CheckLFSVersion()
6665
setting.InitDBConfig()

routers/api/v1/utils/hook.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414
"code.gitea.io/gitea/modules/context"
1515
"code.gitea.io/gitea/modules/convert"
1616
api "code.gitea.io/gitea/modules/structs"
17+
"code.gitea.io/gitea/modules/util"
1718
"code.gitea.io/gitea/routers/utils"
1819
"code.gitea.io/gitea/services/webhook"
19-
20-
"github.com/unknwon/com"
2120
)
2221

2322
// GetOrgHook get an organization's webhook. If there is an error, write to
@@ -89,11 +88,11 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
8988
}
9089

9190
func issuesHook(events []string, event string) bool {
92-
return com.IsSliceContainsStr(events, event) || com.IsSliceContainsStr(events, string(models.HookEventIssues))
91+
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(models.HookEventIssues), events, true)
9392
}
9493

9594
func pullHook(events []string, event string) bool {
96-
return com.IsSliceContainsStr(events, event) || com.IsSliceContainsStr(events, string(models.HookEventPullRequest))
95+
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(models.HookEventPullRequest), events, true)
9796
}
9897

9998
// addHook add the hook specified by `form`, `orgID` and `repoID`. If there is
@@ -112,24 +111,24 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
112111
HookEvent: &models.HookEvent{
113112
ChooseEvents: true,
114113
HookEvents: models.HookEvents{
115-
Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
116-
Delete: com.IsSliceContainsStr(form.Events, string(models.HookEventDelete)),
117-
Fork: com.IsSliceContainsStr(form.Events, string(models.HookEventFork)),
114+
Create: util.IsStringInSlice(string(models.HookEventCreate), form.Events, true),
115+
Delete: util.IsStringInSlice(string(models.HookEventDelete), form.Events, true),
116+
Fork: util.IsStringInSlice(string(models.HookEventFork), form.Events, true),
118117
Issues: issuesHook(form.Events, "issues_only"),
119118
IssueAssign: issuesHook(form.Events, string(models.HookEventIssueAssign)),
120119
IssueLabel: issuesHook(form.Events, string(models.HookEventIssueLabel)),
121120
IssueMilestone: issuesHook(form.Events, string(models.HookEventIssueMilestone)),
122121
IssueComment: issuesHook(form.Events, string(models.HookEventIssueComment)),
123-
Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
122+
Push: util.IsStringInSlice(string(models.HookEventPush), form.Events, true),
124123
PullRequest: pullHook(form.Events, "pull_request_only"),
125124
PullRequestAssign: pullHook(form.Events, string(models.HookEventPullRequestAssign)),
126125
PullRequestLabel: pullHook(form.Events, string(models.HookEventPullRequestLabel)),
127126
PullRequestMilestone: pullHook(form.Events, string(models.HookEventPullRequestMilestone)),
128127
PullRequestComment: pullHook(form.Events, string(models.HookEventPullRequestComment)),
129128
PullRequestReview: pullHook(form.Events, "pull_request_review"),
130129
PullRequestSync: pullHook(form.Events, string(models.HookEventPullRequestSync)),
131-
Repository: com.IsSliceContainsStr(form.Events, string(models.HookEventRepository)),
132-
Release: com.IsSliceContainsStr(form.Events, string(models.HookEventRelease)),
130+
Repository: util.IsStringInSlice(string(models.HookEventRepository), form.Events, true),
131+
Release: util.IsStringInSlice(string(models.HookEventRelease), form.Events, true),
133132
},
134133
BranchFilter: form.BranchFilter,
135134
},
@@ -244,18 +243,18 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
244243
w.PushOnly = false
245244
w.SendEverything = false
246245
w.ChooseEvents = true
247-
w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
248-
w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
249-
w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
250-
w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
251-
w.Delete = com.IsSliceContainsStr(form.Events, string(models.HookEventDelete))
252-
w.Fork = com.IsSliceContainsStr(form.Events, string(models.HookEventFork))
253-
w.Issues = com.IsSliceContainsStr(form.Events, string(models.HookEventIssues))
254-
w.IssueComment = com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment))
255-
w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
256-
w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
257-
w.Repository = com.IsSliceContainsStr(form.Events, string(models.HookEventRepository))
258-
w.Release = com.IsSliceContainsStr(form.Events, string(models.HookEventRelease))
246+
w.Create = util.IsStringInSlice(string(models.HookEventCreate), form.Events, true)
247+
w.Push = util.IsStringInSlice(string(models.HookEventPush), form.Events, true)
248+
w.PullRequest = util.IsStringInSlice(string(models.HookEventPullRequest), form.Events, true)
249+
w.Create = util.IsStringInSlice(string(models.HookEventCreate), form.Events, true)
250+
w.Delete = util.IsStringInSlice(string(models.HookEventDelete), form.Events, true)
251+
w.Fork = util.IsStringInSlice(string(models.HookEventFork), form.Events, true)
252+
w.Issues = util.IsStringInSlice(string(models.HookEventIssues), form.Events, true)
253+
w.IssueComment = util.IsStringInSlice(string(models.HookEventIssueComment), form.Events, true)
254+
w.Push = util.IsStringInSlice(string(models.HookEventPush), form.Events, true)
255+
w.PullRequest = util.IsStringInSlice(string(models.HookEventPullRequest), form.Events, true)
256+
w.Repository = util.IsStringInSlice(string(models.HookEventRepository), form.Events, true)
257+
w.Release = util.IsStringInSlice(string(models.HookEventRelease), form.Events, true)
259258
w.BranchFilter = form.BranchFilter
260259

261260
if err := w.UpdateEvent(); err != nil {

routers/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
114114
viewType := ctx.Query("type")
115115
sortType := ctx.Query("sort")
116116
types := []string{"all", "your_repositories", "assigned", "created_by", "mentioned"}
117-
if !com.IsSliceContainsStr(types, viewType) {
117+
if !util.IsStringInSlice(viewType, types, true) {
118118
viewType = "all"
119119
}
120120

routers/repo/webhook.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import (
2020
"code.gitea.io/gitea/modules/git"
2121
"code.gitea.io/gitea/modules/setting"
2222
api "code.gitea.io/gitea/modules/structs"
23+
"code.gitea.io/gitea/modules/util"
2324
"code.gitea.io/gitea/services/webhook"
24-
25-
"github.com/unknwon/com"
2625
)
2726

2827
const (
@@ -100,7 +99,7 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
10099

101100
func checkHookType(ctx *context.Context) string {
102101
hookType := strings.ToLower(ctx.Params(":type"))
103-
if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
102+
if !util.IsStringInSlice(hookType, setting.Webhook.Types, true) {
104103
ctx.NotFound("checkHookType", nil)
105104
return ""
106105
}

services/archiver/archiver_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/test"
15+
"code.gitea.io/gitea/modules/util"
1516

1617
"github.com/stretchr/testify/assert"
17-
"github.com/unknwon/com"
1818
)
1919

2020
var queueMutex sync.Mutex
@@ -144,7 +144,9 @@ func TestArchive_Basic(t *testing.T) {
144144

145145
for _, req := range inFlight {
146146
assert.True(t, req.IsComplete())
147-
assert.True(t, com.IsExist(req.GetArchivePath()))
147+
exist, err := util.IsExist(req.GetArchivePath())
148+
assert.NoError(t, err)
149+
assert.True(t, exist)
148150
}
149151

150152
arbitraryReq := inFlight[0]

services/repository/transfer_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/notification"
1313
"code.gitea.io/gitea/modules/notification/action"
14+
"code.gitea.io/gitea/modules/util"
1415

1516
"github.com/stretchr/testify/assert"
16-
"github.com/unknwon/com"
1717
)
1818

1919
var notifySync sync.Once
@@ -37,8 +37,12 @@ func TestTransferOwnership(t *testing.T) {
3737
transferredRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
3838
assert.EqualValues(t, 2, transferredRepo.OwnerID)
3939

40-
assert.False(t, com.IsExist(models.RepoPath("user3", "repo3")))
41-
assert.True(t, com.IsExist(models.RepoPath("user2", "repo3")))
40+
exist, err := util.IsExist(models.RepoPath("user3", "repo3"))
41+
assert.NoError(t, err)
42+
assert.False(t, exist)
43+
exist, err = util.IsExist(models.RepoPath("user2", "repo3"))
44+
assert.NoError(t, err)
45+
assert.True(t, exist)
4246
models.AssertExistsAndLoadBean(t, &models.Action{
4347
OpType: models.ActionTransferRepo,
4448
ActUserID: 2,

0 commit comments

Comments
 (0)