Skip to content

Commit eba0476

Browse files
committed
remove some unnecessary wrap functions
1 parent 0733aca commit eba0476

File tree

15 files changed

+37
-59
lines changed

15 files changed

+37
-59
lines changed

models/auth/oauth2.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ func (app *OAuth2Application) ValidateClientSecret(secret []byte) bool {
9292
}
9393

9494
// GetGrantByUserID returns a OAuth2Grant by its user and application ID
95-
func (app *OAuth2Application) GetGrantByUserID(userID int64) (*OAuth2Grant, error) {
96-
return app.getGrantByUserID(db.DefaultContext, userID)
97-
}
98-
99-
func (app *OAuth2Application) getGrantByUserID(ctx context.Context, userID int64) (grant *OAuth2Grant, err error) {
95+
func (app *OAuth2Application) GetGrantByUserID(ctx context.Context, userID int64) (grant *OAuth2Grant, err error) {
10096
grant = new(OAuth2Grant)
10197
if has, err := db.GetEngine(ctx).Where("user_id = ? AND application_id = ?", userID, app.ID).Get(grant); err != nil {
10298
return nil, err
@@ -107,11 +103,7 @@ func (app *OAuth2Application) getGrantByUserID(ctx context.Context, userID int64
107103
}
108104

109105
// CreateGrant generates a grant for an user
110-
func (app *OAuth2Application) CreateGrant(userID int64, scope string) (*OAuth2Grant, error) {
111-
return app.createGrant(db.DefaultContext, userID, scope)
112-
}
113-
114-
func (app *OAuth2Application) createGrant(ctx context.Context, userID int64, scope string) (*OAuth2Grant, error) {
106+
func (app *OAuth2Application) CreateGrant(ctx context.Context, userID int64, scope string) (*OAuth2Grant, error) {
115107
grant := &OAuth2Grant{
116108
ApplicationID: app.ID,
117109
UserID: userID,

models/auth/oauth2_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ func TestOAuth2Application_TableName(t *testing.T) {
7878
func TestOAuth2Application_GetGrantByUserID(t *testing.T) {
7979
assert.NoError(t, unittest.PrepareTestDatabase())
8080
app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
81-
grant, err := app.GetGrantByUserID(1)
81+
grant, err := app.GetGrantByUserID(db.DefaultContext, 1)
8282
assert.NoError(t, err)
8383
assert.Equal(t, int64(1), grant.UserID)
8484

85-
grant, err = app.GetGrantByUserID(34923458)
85+
grant, err = app.GetGrantByUserID(db.DefaultContext, 34923458)
8686
assert.NoError(t, err)
8787
assert.Nil(t, grant)
8888
}
8989

9090
func TestOAuth2Application_CreateGrant(t *testing.T) {
9191
assert.NoError(t, unittest.PrepareTestDatabase())
9292
app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
93-
grant, err := app.CreateGrant(2, "")
93+
grant, err := app.CreateGrant(db.DefaultContext, 2, "")
9494
assert.NoError(t, err)
9595
assert.NotNil(t, grant)
9696
assert.Equal(t, int64(2), grant.UserID)

models/repo/mirror.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (m *Mirror) GetRepository() *Repository {
6060
return m.Repo
6161
}
6262
var err error
63-
m.Repo, err = getRepositoryByID(db.DefaultContext, m.RepoID)
63+
m.Repo, err = GetRepositoryByIDCtx(db.DefaultContext, m.RepoID)
6464
if err != nil {
6565
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
6666
}

models/repo/pushmirror.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (m *PushMirror) GetRepository() *Repository {
3939
return m.Repo
4040
}
4141
var err error
42-
m.Repo, err = getRepositoryByID(db.DefaultContext, m.RepoID)
42+
m.Repo, err = GetRepositoryByIDCtx(db.DefaultContext, m.RepoID)
4343
if err != nil {
4444
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
4545
}

models/repo/repo.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ func (repo *Repository) getBaseRepo(ctx context.Context) (err error) {
462462
return nil
463463
}
464464

465-
repo.BaseRepo, err = getRepositoryByID(ctx, repo.ForkID)
465+
repo.BaseRepo, err = GetRepositoryByIDCtx(ctx, repo.ForkID)
466466
return err
467467
}
468468

@@ -659,7 +659,8 @@ func GetRepositoryByName(ownerID int64, name string) (*Repository, error) {
659659
return repo, err
660660
}
661661

662-
func getRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
662+
// GetRepositoryByIDCtx returns the repository by given id if exists.
663+
func GetRepositoryByIDCtx(ctx context.Context, id int64) (*Repository, error) {
663664
repo := new(Repository)
664665
has, err := db.GetEngine(ctx).ID(id).Get(repo)
665666
if err != nil {
@@ -672,12 +673,7 @@ func getRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
672673

673674
// GetRepositoryByID returns the repository by given id if exists.
674675
func GetRepositoryByID(id int64) (*Repository, error) {
675-
return getRepositoryByID(db.DefaultContext, id)
676-
}
677-
678-
// GetRepositoryByIDCtx returns the repository by given id if exists.
679-
func GetRepositoryByIDCtx(ctx context.Context, id int64) (*Repository, error) {
680-
return getRepositoryByID(ctx, id)
676+
return GetRepositoryByIDCtx(db.DefaultContext, id)
681677
}
682678

683679
// GetRepositoriesMapByIDs returns the repositories by given id slice.
@@ -707,7 +703,7 @@ func GetTemplateRepo(ctx context.Context, repo *Repository) (*Repository, error)
707703
return nil, nil
708704
}
709705

710-
return getRepositoryByID(ctx, repo.TemplateID)
706+
return GetRepositoryByIDCtx(ctx, repo.TemplateID)
711707
}
712708

713709
// TemplateRepo returns the repository, which is template of this repository

models/repo_generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (gt GiteaTemplate) Globs() []glob.Glob {
7070

7171
// GenerateWebhooks generates webhooks from a template repository
7272
func GenerateWebhooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
73-
templateWebhooks, err := webhook.ListWebhooksByOpts(&webhook.ListWebhookOptions{RepoID: templateRepo.ID})
73+
templateWebhooks, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{RepoID: templateRepo.ID})
7474
if err != nil {
7575
return err
7676
}

models/webhook/webhook.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ func (opts *ListWebhookOptions) toCond() builder.Cond {
454454
return cond
455455
}
456456

457-
func listWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) ([]*Webhook, error) {
457+
// ListWebhooksByOpts return webhooks based on options
458+
func ListWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) ([]*Webhook, error) {
458459
sess := db.GetEngine(ctx).Where(opts.toCond())
459460

460461
if opts.Page != 0 {
@@ -469,22 +470,13 @@ func listWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) ([]*Webho
469470
return webhooks, err
470471
}
471472

472-
// ListWebhooksByOpts return webhooks based on options
473-
func ListWebhooksByOpts(opts *ListWebhookOptions) ([]*Webhook, error) {
474-
return listWebhooksByOpts(db.DefaultContext, opts)
475-
}
476-
477473
// CountWebhooksByOpts count webhooks based on options and ignore pagination
478474
func CountWebhooksByOpts(opts *ListWebhookOptions) (int64, error) {
479475
return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&Webhook{})
480476
}
481477

482478
// GetDefaultWebhooks returns all admin-default webhooks.
483-
func GetDefaultWebhooks() ([]*Webhook, error) {
484-
return getDefaultWebhooks(db.DefaultContext)
485-
}
486-
487-
func getDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
479+
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
488480
webhooks := make([]*Webhook, 0, 5)
489481
return webhooks, db.GetEngine(ctx).
490482
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
@@ -506,11 +498,7 @@ func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
506498
}
507499

508500
// GetSystemWebhooks returns all admin system webhooks.
509-
func GetSystemWebhooks(isActive util.OptionalBool) ([]*Webhook, error) {
510-
return getSystemWebhooks(db.DefaultContext, isActive)
511-
}
512-
513-
func getSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
501+
func GetSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
514502
webhooks := make([]*Webhook, 0, 5)
515503
if isActive.IsNone() {
516504
return webhooks, db.GetEngine(ctx).
@@ -596,7 +584,7 @@ func DeleteDefaultSystemWebhook(id int64) error {
596584

597585
// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
598586
func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
599-
ws, err := getDefaultWebhooks(ctx)
587+
ws, err := GetDefaultWebhooks(ctx)
600588
if err != nil {
601589
return fmt.Errorf("GetDefaultWebhooks: %v", err)
602590
}

models/webhook/webhook_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestGetWebhookByOrgID(t *testing.T) {
122122

123123
func TestGetActiveWebhooksByRepoID(t *testing.T) {
124124
assert.NoError(t, unittest.PrepareTestDatabase())
125-
hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1, IsActive: util.OptionalBoolTrue})
125+
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{RepoID: 1, IsActive: util.OptionalBoolTrue})
126126
assert.NoError(t, err)
127127
if assert.Len(t, hooks, 1) {
128128
assert.Equal(t, int64(1), hooks[0].ID)
@@ -132,7 +132,7 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) {
132132

133133
func TestGetWebhooksByRepoID(t *testing.T) {
134134
assert.NoError(t, unittest.PrepareTestDatabase())
135-
hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1})
135+
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{RepoID: 1})
136136
assert.NoError(t, err)
137137
if assert.Len(t, hooks, 2) {
138138
assert.Equal(t, int64(1), hooks[0].ID)
@@ -142,7 +142,7 @@ func TestGetWebhooksByRepoID(t *testing.T) {
142142

143143
func TestGetActiveWebhooksByOrgID(t *testing.T) {
144144
assert.NoError(t, unittest.PrepareTestDatabase())
145-
hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3, IsActive: util.OptionalBoolTrue})
145+
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{OrgID: 3, IsActive: util.OptionalBoolTrue})
146146
assert.NoError(t, err)
147147
if assert.Len(t, hooks, 1) {
148148
assert.Equal(t, int64(3), hooks[0].ID)
@@ -152,7 +152,7 @@ func TestGetActiveWebhooksByOrgID(t *testing.T) {
152152

153153
func TestGetWebhooksByOrgID(t *testing.T) {
154154
assert.NoError(t, unittest.PrepareTestDatabase())
155-
hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3})
155+
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{OrgID: 3})
156156
assert.NoError(t, err)
157157
if assert.Len(t, hooks, 1) {
158158
assert.Equal(t, int64(3), hooks[0].ID)

routers/api/v1/org/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func ListHooks(ctx *context.APIContext) {
5151
return
5252
}
5353

54-
orgHooks, err := webhook.ListWebhooksByOpts(opts)
54+
orgHooks, err := webhook.ListWebhooksByOpts(ctx, opts)
5555
if err != nil {
5656
ctx.InternalServerError(err)
5757
return

routers/api/v1/repo/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func ListHooks(ctx *context.APIContext) {
6060
return
6161
}
6262

63-
hooks, err := webhook.ListWebhooksByOpts(opts)
63+
hooks, err := webhook.ListWebhooksByOpts(ctx, opts)
6464
if err != nil {
6565
ctx.InternalServerError(err)
6666
return

routers/web/admin/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func DefaultOrSystemWebhooks(ctx *context.Context) {
3535

3636
sys["Title"] = ctx.Tr("admin.systemhooks")
3737
sys["Description"] = ctx.Tr("admin.systemhooks.desc")
38-
sys["Webhooks"], err = webhook.GetSystemWebhooks(util.OptionalBoolNone)
38+
sys["Webhooks"], err = webhook.GetSystemWebhooks(ctx, util.OptionalBoolNone)
3939
sys["BaseLink"] = setting.AppSubURL + "/admin/hooks"
4040
sys["BaseLinkNew"] = setting.AppSubURL + "/admin/system-hooks"
4141
if err != nil {
@@ -45,7 +45,7 @@ func DefaultOrSystemWebhooks(ctx *context.Context) {
4545

4646
def["Title"] = ctx.Tr("admin.defaulthooks")
4747
def["Description"] = ctx.Tr("admin.defaulthooks.desc")
48-
def["Webhooks"], err = webhook.GetDefaultWebhooks()
48+
def["Webhooks"], err = webhook.GetDefaultWebhooks(ctx)
4949
def["BaseLink"] = setting.AppSubURL + "/admin/hooks"
5050
def["BaseLinkNew"] = setting.AppSubURL + "/admin/default-hooks"
5151
if err != nil {

routers/web/auth/oauth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func AuthorizeOAuth(ctx *context.Context) {
439439
return
440440
}
441441

442-
grant, err := app.GetGrantByUserID(ctx.Doer.ID)
442+
grant, err := app.GetGrantByUserID(ctx, ctx.Doer.ID)
443443
if err != nil {
444444
handleServerError(ctx, form.State, form.RedirectURI)
445445
return
@@ -516,7 +516,7 @@ func GrantApplicationOAuth(ctx *context.Context) {
516516
ctx.ServerError("GetOAuth2ApplicationByClientID", err)
517517
return
518518
}
519-
grant, err := app.CreateGrant(ctx.Doer.ID, form.Scope)
519+
grant, err := app.CreateGrant(ctx, ctx.Doer.ID, form.Scope)
520520
if err != nil {
521521
handleAuthorizeError(ctx, AuthorizeError{
522522
State: form.State,

routers/web/org/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func Webhooks(ctx *context.Context) {
207207
ctx.Data["BaseLinkNew"] = ctx.Org.OrgLink + "/settings/hooks"
208208
ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
209209

210-
ws, err := webhook.ListWebhooksByOpts(&webhook.ListWebhookOptions{OrgID: ctx.Org.Organization.ID})
210+
ws, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{OrgID: ctx.Org.Organization.ID})
211211
if err != nil {
212212
ctx.ServerError("GetWebhooksByOrgId", err)
213213
return

routers/web/repo/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func Webhooks(ctx *context.Context) {
4444
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
4545
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
4646

47-
ws, err := webhook.ListWebhooksByOpts(&webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
47+
ws, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
4848
if err != nil {
4949
ctx.ServerError("GetWebhooksByRepoID", err)
5050
return

services/webhook/webhook.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package webhook
66

77
import (
8+
"context"
89
"fmt"
910
"strconv"
1011
"strings"
1112

13+
"code.gitea.io/gitea/models/db"
1214
repo_model "code.gitea.io/gitea/models/repo"
1315
webhook_model "code.gitea.io/gitea/models/webhook"
1416
"code.gitea.io/gitea/modules/git"
@@ -218,15 +220,15 @@ func prepareWebhook(w *webhook_model.Webhook, repo *repo_model.Repository, event
218220

219221
// PrepareWebhooks adds new webhooks to task queue for given payload.
220222
func PrepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventType, p api.Payloader) error {
221-
if err := prepareWebhooks(repo, event, p); err != nil {
223+
if err := prepareWebhooks(db.DefaultContext, repo, event, p); err != nil {
222224
return err
223225
}
224226

225227
return addToTask(repo.ID)
226228
}
227229

228-
func prepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventType, p api.Payloader) error {
229-
ws, err := webhook_model.ListWebhooksByOpts(&webhook_model.ListWebhookOptions{
230+
func prepareWebhooks(ctx context.Context, repo *repo_model.Repository, event webhook_model.HookEventType, p api.Payloader) error {
231+
ws, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{
230232
RepoID: repo.ID,
231233
IsActive: util.OptionalBoolTrue,
232234
})
@@ -237,7 +239,7 @@ func prepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventT
237239
// check if repo belongs to org and append additional webhooks
238240
if repo.MustOwner().IsOrganization() {
239241
// get hooks for org
240-
orgHooks, err := webhook_model.ListWebhooksByOpts(&webhook_model.ListWebhookOptions{
242+
orgHooks, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{
241243
OrgID: repo.OwnerID,
242244
IsActive: util.OptionalBoolTrue,
243245
})
@@ -248,7 +250,7 @@ func prepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventT
248250
}
249251

250252
// Add any admin-defined system webhooks
251-
systemHooks, err := webhook_model.GetSystemWebhooks(util.OptionalBoolTrue)
253+
systemHooks, err := webhook_model.GetSystemWebhooks(ctx, util.OptionalBoolTrue)
252254
if err != nil {
253255
return fmt.Errorf("GetSystemWebhooks: %v", err)
254256
}

0 commit comments

Comments
 (0)