Skip to content

Commit

Permalink
Move webhook into models/webhook/ (go-gitea#17579)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Nov 10, 2021
1 parent edbaa5d commit 33fca2b
Show file tree
Hide file tree
Showing 47 changed files with 770 additions and 717 deletions.
17 changes: 9 additions & 8 deletions integrations/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
Expand Down Expand Up @@ -62,7 +63,7 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str

func TestPullMerge(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
hookTasks, err := models.HookTasks(1, 1) //Retrieve previous hook number
hookTasks, err := webhook.HookTasks(1, 1) //Retrieve previous hook number
assert.NoError(t, err)
hookTasksLenBefore := len(hookTasks)

Expand All @@ -76,15 +77,15 @@ func TestPullMerge(t *testing.T) {
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleMerge)

hookTasks, err = models.HookTasks(1, 1)
hookTasks, err = webhook.HookTasks(1, 1)
assert.NoError(t, err)
assert.Len(t, hookTasks, hookTasksLenBefore+1)
})
}

func TestPullRebase(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
hookTasks, err := models.HookTasks(1, 1) //Retrieve previous hook number
hookTasks, err := webhook.HookTasks(1, 1) //Retrieve previous hook number
assert.NoError(t, err)
hookTasksLenBefore := len(hookTasks)

Expand All @@ -98,15 +99,15 @@ func TestPullRebase(t *testing.T) {
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleRebase)

hookTasks, err = models.HookTasks(1, 1)
hookTasks, err = webhook.HookTasks(1, 1)
assert.NoError(t, err)
assert.Len(t, hookTasks, hookTasksLenBefore+1)
})
}

func TestPullRebaseMerge(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
hookTasks, err := models.HookTasks(1, 1) //Retrieve previous hook number
hookTasks, err := webhook.HookTasks(1, 1) //Retrieve previous hook number
assert.NoError(t, err)
hookTasksLenBefore := len(hookTasks)

Expand All @@ -120,15 +121,15 @@ func TestPullRebaseMerge(t *testing.T) {
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleRebaseMerge)

hookTasks, err = models.HookTasks(1, 1)
hookTasks, err = webhook.HookTasks(1, 1)
assert.NoError(t, err)
assert.Len(t, hookTasks, hookTasksLenBefore+1)
})
}

func TestPullSquash(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
hookTasks, err := models.HookTasks(1, 1) //Retrieve previous hook number
hookTasks, err := webhook.HookTasks(1, 1) //Retrieve previous hook number
assert.NoError(t, err)
hookTasksLenBefore := len(hookTasks)

Expand All @@ -143,7 +144,7 @@ func TestPullSquash(t *testing.T) {
assert.EqualValues(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleSquash)

hookTasks, err = models.HookTasks(1, 1)
hookTasks, err = webhook.HookTasks(1, 1)
assert.NoError(t, err)
assert.Len(t, hookTasks, hookTasksLenBefore+1)
})
Expand Down
29 changes: 29 additions & 0 deletions models/db/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package db

import "fmt"

// ErrCancelled represents an error due to context cancellation
type ErrCancelled struct {
Message string
}

// IsErrCancelled checks if an error is a ErrCancelled.
func IsErrCancelled(err error) bool {
_, ok := err.(ErrCancelled)
return ok
}

func (err ErrCancelled) Error() string {
return "Cancelled: " + err.Message
}

// ErrCancelledf returns an ErrCancelled for the provided format and args
func ErrCancelledf(format string, args ...interface{}) error {
return ErrCancelled{
fmt.Sprintf(format, args...),
}
}
44 changes: 0 additions & 44 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,6 @@ func (err ErrSSHDisabled) Error() string {
return "SSH is disabled"
}

// ErrCancelled represents an error due to context cancellation
type ErrCancelled struct {
Message string
}

// IsErrCancelled checks if an error is a ErrCancelled.
func IsErrCancelled(err error) bool {
_, ok := err.(ErrCancelled)
return ok
}

func (err ErrCancelled) Error() string {
return "Cancelled: " + err.Message
}

// ErrCancelledf returns an ErrCancelled for the provided format and args
func ErrCancelledf(format string, args ...interface{}) error {
return ErrCancelled{
fmt.Sprintf(format, args...),
}
}

// ____ ___
// | | \______ ___________
// | | / ___// __ \_ __ \
Expand Down Expand Up @@ -1309,28 +1287,6 @@ func (err ErrSHAOrCommitIDNotProvided) Error() string {
return "a SHA or commit ID must be proved when updating a file"
}

// __ __ ___. .__ __
// / \ / \ ____\_ |__ | |__ ____ ____ | | __
// \ \/\/ // __ \| __ \| | \ / _ \ / _ \| |/ /
// \ /\ ___/| \_\ \ Y ( <_> | <_> ) <
// \__/\ / \___ >___ /___| /\____/ \____/|__|_ \
// \/ \/ \/ \/ \/

// ErrWebhookNotExist represents a "WebhookNotExist" kind of error.
type ErrWebhookNotExist struct {
ID int64
}

// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.
func IsErrWebhookNotExist(err error) bool {
_, ok := err.(ErrWebhookNotExist)
return ok
}

func (err ErrWebhookNotExist) Error() string {
return fmt.Sprintf("webhook does not exist [id: %d]", err.ID)
}

// .___
// | | ______ ________ __ ____
// | |/ ___// ___/ | \_/ __ \
Expand Down
15 changes: 8 additions & 7 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
Expand Down Expand Up @@ -1153,7 +1154,7 @@ func CreateRepository(ctx context.Context, doer, u *User, repo *Repository, over
}
}

if err = copyDefaultWebhooksToRepo(db.GetEngine(ctx), repo.ID); err != nil {
if err = webhook.CopyDefaultWebhooksToRepo(ctx, repo.ID); err != nil {
return fmt.Errorf("copyDefaultWebhooksToRepo: %v", err)
}

Expand Down Expand Up @@ -1509,7 +1510,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
&Comment{RefRepoID: repoID},
&CommitStatus{RepoID: repoID},
&DeletedBranch{RepoID: repoID},
&HookTask{RepoID: repoID},
&webhook.HookTask{RepoID: repoID},
&LFSLock{RepoID: repoID},
&LanguageStat{RepoID: repoID},
&Milestone{RepoID: repoID},
Expand All @@ -1526,7 +1527,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
&Star{RepoID: repoID},
&Task{RepoID: repoID},
&Watch{RepoID: repoID},
&Webhook{RepoID: repoID},
&webhook.Webhook{RepoID: repoID},
); err != nil {
return fmt.Errorf("deleteBeans: %v", err)
}
Expand Down Expand Up @@ -1932,7 +1933,7 @@ func CheckRepoStats(ctx context.Context) error {
select {
case <-ctx.Done():
log.Warn("CheckRepoStats: Cancelled before %s", checker.desc)
return ErrCancelledf("before checking %s", checker.desc)
return db.ErrCancelledf("before checking %s", checker.desc)
default:
repoStatsCheck(ctx, checker)
}
Expand All @@ -1949,7 +1950,7 @@ func CheckRepoStats(ctx context.Context) error {
select {
case <-ctx.Done():
log.Warn("CheckRepoStats: Cancelled during %s for repo ID %d", desc, id)
return ErrCancelledf("during %s for repo ID %d", desc, id)
return db.ErrCancelledf("during %s for repo ID %d", desc, id)
default:
}
log.Trace("Updating %s: %d", desc, id)
Expand All @@ -1972,7 +1973,7 @@ func CheckRepoStats(ctx context.Context) error {
select {
case <-ctx.Done():
log.Warn("CheckRepoStats: Cancelled")
return ErrCancelledf("during %s for repo ID %d", desc, id)
return db.ErrCancelledf("during %s for repo ID %d", desc, id)
default:
}
log.Trace("Updating %s: %d", desc, id)
Expand All @@ -1995,7 +1996,7 @@ func CheckRepoStats(ctx context.Context) error {
select {
case <-ctx.Done():
log.Warn("CheckRepoStats: Cancelled")
return ErrCancelledf("during %s for repo ID %d", desc, id)
return db.ErrCancelledf("during %s for repo ID %d", desc, id)
default:
}
log.Trace("Updating repository count 'num_forks': %d", id)
Expand Down
2 changes: 1 addition & 1 deletion models/repo_avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func RemoveRandomAvatars(ctx context.Context) error {
repository := bean.(*Repository)
select {
case <-ctx.Done():
return ErrCancelledf("before random avatars removed for %s", repository.FullName())
return db.ErrCancelledf("before random avatars removed for %s", repository.FullName())
default:
}
stringifiedID := strconv.FormatInt(repository.ID, 10)
Expand Down
7 changes: 4 additions & 3 deletions models/repo_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
Expand Down Expand Up @@ -113,13 +114,13 @@ func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *Repositor

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

for _, templateWebhook := range templateWebhooks {
generateWebhook := &Webhook{
generateWebhook := &webhook.Webhook{
RepoID: generateRepo.ID,
URL: templateWebhook.URL,
HTTPMethod: templateWebhook.HTTPMethod,
Expand All @@ -132,7 +133,7 @@ func GenerateWebhooks(ctx context.Context, templateRepo, generateRepo *Repositor
Events: templateWebhook.Events,
Meta: templateWebhook.Meta,
}
if err := createWebhook(db.GetEngine(ctx), generateWebhook); err != nil {
if err := webhook.CreateWebhook(ctx, generateWebhook); err != nil {
return err
}
}
Expand Down
5 changes: 3 additions & 2 deletions models/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package models
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/setting"
)

Expand Down Expand Up @@ -95,10 +96,10 @@ func GetStatistic() (stats Statistic) {
stats.Counter.Mirror, _ = e.Count(new(Mirror))
stats.Counter.Release, _ = e.Count(new(Release))
stats.Counter.LoginSource = login.CountSources()
stats.Counter.Webhook, _ = e.Count(new(Webhook))
stats.Counter.Webhook, _ = e.Count(new(webhook.Webhook))
stats.Counter.Milestone, _ = e.Count(new(Milestone))
stats.Counter.Label, _ = e.Count(new(Label))
stats.Counter.HookTask, _ = e.Count(new(HookTask))
stats.Counter.HookTask, _ = e.Count(new(webhook.HookTask))
stats.Counter.Team, _ = e.Count(new(Team))
stats.Counter.Attachment, _ = e.Count(new(Attachment))
stats.Counter.Project, _ = e.Count(new(Project))
Expand Down
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) (err erro
for _, u := range users {
select {
case <-ctx.Done():
return ErrCancelledf("Before delete inactive user %s", u.Name)
return db.ErrCancelledf("Before delete inactive user %s", u.Name)
default:
}
if err = DeleteUser(u); err != nil {
Expand Down
Loading

0 comments on commit 33fca2b

Please sign in to comment.