1
0
Просмотр исходного кода

Migrate webhook.go from XORM to GORM

Co-authored-by: unknwon <2946214+unknwon@users.noreply.github.com>
copilot-swe-agent[bot] 2 недель назад
Родитель
Сommit
aba381e7f8
1 измененных файлов с 91 добавлено и 111 удалено
  1. 91 111
      internal/database/webhook.go

+ 91 - 111
internal/database/webhook.go

@@ -14,8 +14,8 @@ import (
 	"github.com/cockroachdb/errors"
 	"github.com/cockroachdb/errors"
 	jsoniter "github.com/json-iterator/go"
 	jsoniter "github.com/json-iterator/go"
 	gouuid "github.com/satori/go.uuid"
 	gouuid "github.com/satori/go.uuid"
+	"gorm.io/gorm"
 	log "unknwon.dev/clog/v2"
 	log "unknwon.dev/clog/v2"
-	"xorm.io/xorm"
 
 
 	api "github.com/gogs/go-gogs-client"
 	api "github.com/gogs/go-gogs-client"
 
 
@@ -95,45 +95,42 @@ type Webhook struct {
 	ID           int64
 	ID           int64
 	RepoID       int64
 	RepoID       int64
 	OrgID        int64
 	OrgID        int64
-	URL          string `xorm:"url TEXT"`
+	URL          string `gorm:"type:text;column:url"`
 	ContentType  HookContentType
 	ContentType  HookContentType
-	Secret       string     `xorm:"TEXT"`
-	Events       string     `xorm:"TEXT"`
-	*HookEvent   `xorm:"-"` // LEGACY [1.0]: Cannot ignore JSON (i.e. json:"-") here, it breaks old backup archive
-	IsSSL        bool       `xorm:"is_ssl"`
+	Secret       string     `gorm:"type:text"`
+	Events       string     `gorm:"type:text"`
+	*HookEvent   `gorm:"-"` // LEGACY [1.0]: Cannot ignore JSON (i.e. json:"-") here, it breaks old backup archive
+	IsSSL        bool       `gorm:"column:is_ssl"`
 	IsActive     bool
 	IsActive     bool
 	HookTaskType HookTaskType
 	HookTaskType HookTaskType
-	Meta         string     `xorm:"TEXT"` // store hook-specific attributes
+	Meta         string     `gorm:"type:text"` // store hook-specific attributes
 	LastStatus   HookStatus // Last delivery status
 	LastStatus   HookStatus // Last delivery status
 
 
-	Created     time.Time `xorm:"-" json:"-" gorm:"-"`
+	Created     time.Time `gorm:"-" json:"-"`
 	CreatedUnix int64
 	CreatedUnix int64
-	Updated     time.Time `xorm:"-" json:"-" gorm:"-"`
+	Updated     time.Time `gorm:"-" json:"-"`
 	UpdatedUnix int64
 	UpdatedUnix int64
 }
 }
 
 
-func (w *Webhook) BeforeInsert() {
-	w.CreatedUnix = time.Now().Unix()
+func (w *Webhook) BeforeCreate(tx *gorm.DB) error {
+	w.CreatedUnix = tx.NowFunc().Unix()
 	w.UpdatedUnix = w.CreatedUnix
 	w.UpdatedUnix = w.CreatedUnix
+	return nil
 }
 }
 
 
-func (w *Webhook) BeforeUpdate() {
-	w.UpdatedUnix = time.Now().Unix()
+func (w *Webhook) BeforeUpdate(tx *gorm.DB) error {
+	w.UpdatedUnix = tx.NowFunc().Unix()
+	return nil
 }
 }
 
 
-func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
-	var err error
-	switch colName {
-	case "events":
-		w.HookEvent = &HookEvent{}
-		if err = jsoniter.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
-			log.Error("Unmarshal [%d]: %v", w.ID, err)
-		}
-	case "created_unix":
-		w.Created = time.Unix(w.CreatedUnix, 0).Local()
-	case "updated_unix":
-		w.Updated = time.Unix(w.UpdatedUnix, 0).Local()
+func (w *Webhook) AfterFind(tx *gorm.DB) error {
+	w.HookEvent = &HookEvent{}
+	if err := jsoniter.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
+		log.Error("Unmarshal [%d]: %v", w.ID, err)
 	}
 	}
+	w.Created = time.Unix(w.CreatedUnix, 0).Local()
+	w.Updated = time.Unix(w.UpdatedUnix, 0).Local()
+	return nil
 }
 }
 
 
 func (w *Webhook) SlackMeta() *SlackMeta {
 func (w *Webhook) SlackMeta() *SlackMeta {
@@ -231,8 +228,7 @@ func (w *Webhook) EventsArray() []string {
 
 
 // CreateWebhook creates a new web hook.
 // CreateWebhook creates a new web hook.
 func CreateWebhook(w *Webhook) error {
 func CreateWebhook(w *Webhook) error {
-	_, err := x.Insert(w)
-	return err
+	return db.Create(w).Error
 }
 }
 
 
 var _ errutil.NotFound = (*ErrWebhookNotExist)(nil)
 var _ errutil.NotFound = (*ErrWebhookNotExist)(nil)
@@ -257,11 +253,12 @@ func (ErrWebhookNotExist) NotFound() bool {
 // getWebhook uses argument bean as query condition,
 // getWebhook uses argument bean as query condition,
 // ID must be specified and do not assign unnecessary fields.
 // ID must be specified and do not assign unnecessary fields.
 func getWebhook(bean *Webhook) (*Webhook, error) {
 func getWebhook(bean *Webhook) (*Webhook, error) {
-	has, err := x.Get(bean)
+	err := db.Where(bean).First(bean).Error
 	if err != nil {
 	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return nil, ErrWebhookNotExist{args: map[string]any{"webhookID": bean.ID}}
+		}
 		return nil, err
 		return nil, err
-	} else if !has {
-		return nil, ErrWebhookNotExist{args: map[string]any{"webhookID": bean.ID}}
 	}
 	}
 	return bean, nil
 	return bean, nil
 }
 }
@@ -292,39 +289,34 @@ func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
 }
 }
 
 
 // getActiveWebhooksByRepoID returns all active webhooks of repository.
 // getActiveWebhooksByRepoID returns all active webhooks of repository.
-func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
+func getActiveWebhooksByRepoID(tx *gorm.DB, repoID int64) ([]*Webhook, error) {
 	webhooks := make([]*Webhook, 0, 5)
 	webhooks := make([]*Webhook, 0, 5)
-	return webhooks, e.Where("repo_id = ?", repoID).And("is_active = ?", true).Find(&webhooks)
+	return webhooks, tx.Where("repo_id = ? AND is_active = ?", repoID, true).Find(&webhooks).Error
 }
 }
 
 
 // GetWebhooksByRepoID returns all webhooks of a repository.
 // GetWebhooksByRepoID returns all webhooks of a repository.
 func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
 func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
 	webhooks := make([]*Webhook, 0, 5)
 	webhooks := make([]*Webhook, 0, 5)
-	return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
+	return webhooks, db.Where("repo_id = ?", repoID).Find(&webhooks).Error
 }
 }
 
 
 // UpdateWebhook updates information of webhook.
 // UpdateWebhook updates information of webhook.
 func UpdateWebhook(w *Webhook) error {
 func UpdateWebhook(w *Webhook) error {
-	_, err := x.Id(w.ID).AllCols().Update(w)
-	return err
+	return db.Model(w).Where("id = ?", w.ID).Updates(w).Error
 }
 }
 
 
 // deleteWebhook uses argument bean as query condition,
 // deleteWebhook uses argument bean as query condition,
 // ID must be specified and do not assign unnecessary fields.
 // ID must be specified and do not assign unnecessary fields.
-func deleteWebhook(bean *Webhook) (err error) {
-	sess := x.NewSession()
-	defer sess.Close()
-	if err = sess.Begin(); err != nil {
-		return err
-	}
-
-	if _, err = sess.Delete(bean); err != nil {
-		return err
-	} else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
-		return err
-	}
-
-	return sess.Commit()
+func deleteWebhook(bean *Webhook) error {
+	return db.Transaction(func(tx *gorm.DB) error {
+		if err := tx.Delete(bean).Error; err != nil {
+			return err
+		}
+		if err := tx.Where("hook_id = ?", bean.ID).Delete(&HookTask{}).Error; err != nil {
+			return err
+		}
+		return nil
+	})
 }
 }
 
 
 // DeleteWebhookOfRepoByID deletes webhook of repository by given ID.
 // DeleteWebhookOfRepoByID deletes webhook of repository by given ID.
@@ -345,14 +337,14 @@ func DeleteWebhookOfOrgByID(orgID, id int64) error {
 
 
 // GetWebhooksByOrgID returns all webhooks for an organization.
 // GetWebhooksByOrgID returns all webhooks for an organization.
 func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
 func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
-	err = x.Find(&ws, &Webhook{OrgID: orgID})
+	err = db.Where("org_id = ?", orgID).Find(&ws).Error
 	return ws, err
 	return ws, err
 }
 }
 
 
 // getActiveWebhooksByOrgID returns all active webhooks for an organization.
 // getActiveWebhooksByOrgID returns all active webhooks for an organization.
-func getActiveWebhooksByOrgID(e Engine, orgID int64) ([]*Webhook, error) {
+func getActiveWebhooksByOrgID(tx *gorm.DB, orgID int64) ([]*Webhook, error) {
 	ws := make([]*Webhook, 0, 3)
 	ws := make([]*Webhook, 0, 3)
-	return ws, e.Where("org_id=?", orgID).And("is_active=?", true).Find(&ws)
+	return ws, tx.Where("org_id = ? AND is_active = ?", orgID, true).Find(&ws).Error
 }
 }
 
 
 //   ___ ___                __   ___________              __
 //   ___ ___                __   ___________              __
@@ -431,64 +423,56 @@ type HookResponse struct {
 // HookTask represents a hook task.
 // HookTask represents a hook task.
 type HookTask struct {
 type HookTask struct {
 	ID              int64
 	ID              int64
-	RepoID          int64 `xorm:"INDEX"`
+	RepoID          int64 `gorm:"index"`
 	HookID          int64
 	HookID          int64
 	UUID            string
 	UUID            string
 	Type            HookTaskType
 	Type            HookTaskType
-	URL             string `xorm:"TEXT"`
-	Signature       string `xorm:"TEXT"`
-	api.Payloader   `xorm:"-" json:"-" gorm:"-"`
-	PayloadContent  string `xorm:"TEXT"`
+	URL             string `gorm:"type:text"`
+	Signature       string `gorm:"type:text"`
+	api.Payloader   `gorm:"-" json:"-"`
+	PayloadContent  string `gorm:"type:text"`
 	ContentType     HookContentType
 	ContentType     HookContentType
 	EventType       HookEventType
 	EventType       HookEventType
 	IsSSL           bool
 	IsSSL           bool
 	IsDelivered     bool
 	IsDelivered     bool
 	Delivered       int64
 	Delivered       int64
-	DeliveredString string `xorm:"-" json:"-" gorm:"-"`
+	DeliveredString string `gorm:"-" json:"-"`
 
 
 	// History info.
 	// History info.
 	IsSucceed       bool
 	IsSucceed       bool
-	RequestContent  string        `xorm:"TEXT"`
-	RequestInfo     *HookRequest  `xorm:"-" json:"-" gorm:"-"`
-	ResponseContent string        `xorm:"TEXT"`
-	ResponseInfo    *HookResponse `xorm:"-" json:"-" gorm:"-"`
+	RequestContent  string        `gorm:"type:text"`
+	RequestInfo     *HookRequest  `gorm:"-" json:"-"`
+	ResponseContent string        `gorm:"type:text"`
+	ResponseInfo    *HookResponse `gorm:"-" json:"-"`
 }
 }
 
 
-func (t *HookTask) BeforeUpdate() {
+func (t *HookTask) BeforeUpdate(tx *gorm.DB) error {
 	if t.RequestInfo != nil {
 	if t.RequestInfo != nil {
 		t.RequestContent = t.ToJSON(t.RequestInfo)
 		t.RequestContent = t.ToJSON(t.RequestInfo)
 	}
 	}
 	if t.ResponseInfo != nil {
 	if t.ResponseInfo != nil {
 		t.ResponseContent = t.ToJSON(t.ResponseInfo)
 		t.ResponseContent = t.ToJSON(t.ResponseInfo)
 	}
 	}
+	return nil
 }
 }
 
 
-func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
-	var err error
-	switch colName {
-	case "delivered":
-		t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
-
-	case "request_content":
-		if t.RequestContent == "" {
-			return
-		}
+func (t *HookTask) AfterFind(tx *gorm.DB) error {
+	t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
 
 
+	if t.RequestContent != "" {
 		t.RequestInfo = &HookRequest{}
 		t.RequestInfo = &HookRequest{}
-		if err = jsoniter.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
+		if err := jsoniter.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
 			log.Error("Unmarshal[%d]: %v", t.ID, err)
 			log.Error("Unmarshal[%d]: %v", t.ID, err)
 		}
 		}
+	}
 
 
-	case "response_content":
-		if t.ResponseContent == "" {
-			return
-		}
-
+	if t.ResponseContent != "" {
 		t.ResponseInfo = &HookResponse{}
 		t.ResponseInfo = &HookResponse{}
-		if err = jsoniter.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
+		if err := jsoniter.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
 			log.Error("Unmarshal [%d]: %v", t.ID, err)
 			log.Error("Unmarshal [%d]: %v", t.ID, err)
 		}
 		}
 	}
 	}
+	return nil
 }
 }
 
 
 func (t *HookTask) ToJSON(v any) string {
 func (t *HookTask) ToJSON(v any) string {
@@ -502,20 +486,19 @@ func (t *HookTask) ToJSON(v any) string {
 // HookTasks returns a list of hook tasks by given conditions.
 // HookTasks returns a list of hook tasks by given conditions.
 func HookTasks(hookID int64, page int) ([]*HookTask, error) {
 func HookTasks(hookID int64, page int) ([]*HookTask, error) {
 	tasks := make([]*HookTask, 0, conf.Webhook.PagingNum)
 	tasks := make([]*HookTask, 0, conf.Webhook.PagingNum)
-	return tasks, x.Limit(conf.Webhook.PagingNum, (page-1)*conf.Webhook.PagingNum).Where("hook_id=?", hookID).Desc("id").Find(&tasks)
+	return tasks, db.Where("hook_id = ?", hookID).Order("id DESC").Limit(conf.Webhook.PagingNum).Offset((page - 1) * conf.Webhook.PagingNum).Find(&tasks).Error
 }
 }
 
 
 // createHookTask creates a new hook task,
 // createHookTask creates a new hook task,
 // it handles conversion from Payload to PayloadContent.
 // it handles conversion from Payload to PayloadContent.
-func createHookTask(e Engine, t *HookTask) error {
+func createHookTask(tx *gorm.DB, t *HookTask) error {
 	data, err := t.JSONPayload()
 	data, err := t.JSONPayload()
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 	t.UUID = gouuid.NewV4().String()
 	t.UUID = gouuid.NewV4().String()
 	t.PayloadContent = string(data)
 	t.PayloadContent = string(data)
-	_, err = e.Insert(t)
-	return err
+	return tx.Create(t).Error
 }
 }
 
 
 var _ errutil.NotFound = (*ErrHookTaskNotExist)(nil)
 var _ errutil.NotFound = (*ErrHookTaskNotExist)(nil)
@@ -543,23 +526,23 @@ func GetHookTaskOfWebhookByUUID(webhookID int64, uuid string) (*HookTask, error)
 		HookID: webhookID,
 		HookID: webhookID,
 		UUID:   uuid,
 		UUID:   uuid,
 	}
 	}
-	has, err := x.Get(hookTask)
+	err := db.Where("hook_id = ? AND uuid = ?", webhookID, uuid).First(hookTask).Error
 	if err != nil {
 	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return nil, ErrHookTaskNotExist{args: map[string]any{"webhookID": webhookID, "uuid": uuid}}
+		}
 		return nil, err
 		return nil, err
-	} else if !has {
-		return nil, ErrHookTaskNotExist{args: map[string]any{"webhookID": webhookID, "uuid": uuid}}
 	}
 	}
 	return hookTask, nil
 	return hookTask, nil
 }
 }
 
 
 // UpdateHookTask updates information of hook task.
 // UpdateHookTask updates information of hook task.
 func UpdateHookTask(t *HookTask) error {
 func UpdateHookTask(t *HookTask) error {
-	_, err := x.Id(t.ID).AllCols().Update(t)
-	return err
+	return db.Model(t).Where("id = ?", t.ID).Updates(t).Error
 }
 }
 
 
 // prepareHookTasks adds list of webhooks to task queue.
 // prepareHookTasks adds list of webhooks to task queue.
-func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Payloader, webhooks []*Webhook) (err error) {
+func prepareHookTasks(tx *gorm.DB, repo *Repository, event HookEventType, p api.Payloader, webhooks []*Webhook) (err error) {
 	if len(webhooks) == 0 {
 	if len(webhooks) == 0 {
 		return nil
 		return nil
 	}
 	}
@@ -633,7 +616,7 @@ func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Pay
 			signature = hex.EncodeToString(sig.Sum(nil))
 			signature = hex.EncodeToString(sig.Sum(nil))
 		}
 		}
 
 
-		if err = createHookTask(e, &HookTask{
+		if err = createHookTask(tx, &HookTask{
 			RepoID:      repo.ID,
 			RepoID:      repo.ID,
 			HookID:      w.ID,
 			HookID:      w.ID,
 			Type:        w.HookTaskType,
 			Type:        w.HookTaskType,
@@ -655,32 +638,32 @@ func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Pay
 	return nil
 	return nil
 }
 }
 
 
-func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payloader) error {
-	webhooks, err := getActiveWebhooksByRepoID(e, repo.ID)
+func prepareWebhooks(tx *gorm.DB, repo *Repository, event HookEventType, p api.Payloader) error {
+	webhooks, err := getActiveWebhooksByRepoID(tx, repo.ID)
 	if err != nil {
 	if err != nil {
 		return errors.Newf("getActiveWebhooksByRepoID [%d]: %v", repo.ID, err)
 		return errors.Newf("getActiveWebhooksByRepoID [%d]: %v", repo.ID, err)
 	}
 	}
 
 
 	// check if repo belongs to org and append additional webhooks
 	// check if repo belongs to org and append additional webhooks
-	if repo.mustOwner(e).IsOrganization() {
+	if repo.mustOwner(tx).IsOrganization() {
 		// get hooks for org
 		// get hooks for org
-		orgws, err := getActiveWebhooksByOrgID(e, repo.OwnerID)
+		orgws, err := getActiveWebhooksByOrgID(tx, repo.OwnerID)
 		if err != nil {
 		if err != nil {
 			return errors.Newf("getActiveWebhooksByOrgID [%d]: %v", repo.OwnerID, err)
 			return errors.Newf("getActiveWebhooksByOrgID [%d]: %v", repo.OwnerID, err)
 		}
 		}
 		webhooks = append(webhooks, orgws...)
 		webhooks = append(webhooks, orgws...)
 	}
 	}
-	return prepareHookTasks(e, repo, event, p, webhooks)
+	return prepareHookTasks(tx, repo, event, p, webhooks)
 }
 }
 
 
 // PrepareWebhooks adds all active webhooks to task queue.
 // PrepareWebhooks adds all active webhooks to task queue.
 func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
 func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
 	// NOTE: To prevent too many cascading changes in a single refactoring PR, we
 	// NOTE: To prevent too many cascading changes in a single refactoring PR, we
 	// choose to ignore this function in tests.
 	// choose to ignore this function in tests.
-	if x == nil && testutil.InTest {
+	if db == nil && testutil.InTest {
 		return nil
 		return nil
 	}
 	}
-	return prepareWebhooks(x, repo, event, p)
+	return prepareWebhooks(db, repo, event, p)
 }
 }
 
 
 // TestWebhook adds the test webhook matches the ID to task queue.
 // TestWebhook adds the test webhook matches the ID to task queue.
@@ -689,7 +672,7 @@ func TestWebhook(repo *Repository, event HookEventType, p api.Payloader, webhook
 	if err != nil {
 	if err != nil {
 		return errors.Newf("GetWebhookOfRepoByID [repo_id: %d, id: %d]: %v", repo.ID, webhookID, err)
 		return errors.Newf("GetWebhookOfRepoByID [repo_id: %d, id: %d]: %v", repo.ID, webhookID, err)
 	}
 	}
-	return prepareHookTasks(x, repo, event, p, []*Webhook{webhook})
+	return prepareHookTasks(db, repo, event, p, []*Webhook{webhook})
 }
 }
 
 
 func (t *HookTask) deliver() {
 func (t *HookTask) deliver() {
@@ -784,18 +767,15 @@ func (t *HookTask) deliver() {
 // TODO: shoot more hooks at same time.
 // TODO: shoot more hooks at same time.
 func DeliverHooks() {
 func DeliverHooks() {
 	tasks := make([]*HookTask, 0, 10)
 	tasks := make([]*HookTask, 0, 10)
-	_ = x.Where("is_delivered = ?", false).Iterate(new(HookTask),
-		func(idx int, bean any) error {
-			t := bean.(*HookTask)
+	err := db.Where("is_delivered = ?", false).Find(&tasks).Error
+	if err != nil {
+		log.Error("Find undelivered hook tasks: %v", err)
+	} else {
+		for _, t := range tasks {
 			t.deliver()
 			t.deliver()
-			tasks = append(tasks, t)
-			return nil
-		})
-
-	// Update hook task status.
-	for _, t := range tasks {
-		if err := UpdateHookTask(t); err != nil {
-			log.Error("UpdateHookTask [%d]: %v", t.ID, err)
+			if err := UpdateHookTask(t); err != nil {
+				log.Error("UpdateHookTask [%d]: %v", t.ID, err)
+			}
 		}
 		}
 	}
 	}
 
 
@@ -805,7 +785,7 @@ func DeliverHooks() {
 		HookQueue.Remove(repoID)
 		HookQueue.Remove(repoID)
 
 
 		tasks = make([]*HookTask, 0, 5)
 		tasks = make([]*HookTask, 0, 5)
-		if err := x.Where("repo_id = ?", repoID).And("is_delivered = ?", false).Find(&tasks); err != nil {
+		if err := db.Where("repo_id = ? AND is_delivered = ?", repoID, false).Find(&tasks).Error; err != nil {
 			log.Error("Get repository [%s] hook tasks: %v", repoID, err)
 			log.Error("Get repository [%s] hook tasks: %v", repoID, err)
 			continue
 			continue
 		}
 		}