Skip to content

Commit

Permalink
fix webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
holdno committed Sep 17, 2023
1 parent 55359a6 commit db7ccb2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@
<h1 align="center">GopherCron</h1>
开箱即用的分布式可视化crontab

可以通过配置文件指定某个节点所受理的业务线,从而做到业务统一管理但隔离调度
[使用文档](https://gophercron.ojbk.io/)


### V2.2.0 2023-07-25

增加边缘与中心间的鉴权,配置文件会有调整,等后面稍微有空时我会整理一个使用文档出来。

### Discussions
### Discussions

[关于“为系统增加内置环境变量”的讨论](https://github.com/holdno/gopherCron/discussions/21)


### V2

全新 V2 版本支持 workflow,重写任务调度方式,移除 client 对 etcd 的依赖
Expand Down Expand Up @@ -103,4 +97,5 @@ client 配置文件中的 project 配置需要用户先部署 service
需要将项目 ID 填写在 client 的配置中该 client 才会调度这个项目的任务

### Chat & QA
- [Discord](https://discord.gg/TCybDnu8)

- [Discord](https://discord.gg/TCybDnu8)
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ func (a *app) CreateUser(u common.User) error {

func (a *app) GetProjectRelevanceUsers(pid int64) ([]*common.ProjectRelevance, error) {
opt := selection.NewSelector(selection.NewRequirement("project_id", selection.Equals, pid))
opt.AddOrder("create_time DESC")
res, err := a.store.ProjectRelevance().GetList(opt)
if err != nil && err != gorm.ErrRecordNotFound {
errObj := errors.ErrInternalError
Expand Down
12 changes: 10 additions & 2 deletions app/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ import (
)

func (a *app) CreateWebHook(projectID int64, types, callbackUrl string) error {
err := a.store.WebHook().Create(common.WebHook{
hook, err := a.GetWebHook(projectID, types)
if err != nil {
return err
}

if hook != nil {
return errors.NewError(http.StatusForbidden, "当前webhook类型已存在")
}

err = a.store.WebHook().Create(common.WebHook{
CallbackURL: callbackUrl,
ProjectID: projectID,
Type: types,
Secret: utils.RandomStr(32),
CreateTime: time.Now().Unix(),
})

Expand Down
14 changes: 14 additions & 0 deletions cmd/service/controller/user_func/select.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package user_func

import (
"sort"

"github.com/holdno/gopherCron/app"
"github.com/holdno/gopherCron/cmd/service/response"
"github.com/holdno/gopherCron/common"
Expand Down Expand Up @@ -141,6 +143,18 @@ func GetUsersUnderTheProject(c *gin.Context) {
}
}

sort.Slice(res, func(i, j int) bool {
ic, exist := usersMap.Get(res[i].ID)
if !exist {
return false
}
jc, exist := usersMap.Get(res[j].ID)
if !exist {
return true
}
return ic.CreateTime > jc.CreateTime
})

response.APISuccess(c, &gin.H{
"list": utils.TernaryOperation(res != nil, res, []struct{}{}),
})
Expand Down
12 changes: 6 additions & 6 deletions cmd/service/controller/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type CreateWebHookRequest struct {
ProjectID int64 `json:"project_id" form:"project_id" binding:"required"`
CallBackURL string `json:"call_back_url" form:"call_back_url" binding:"required"`
Types string `json:"types" form:"types"`
Type string `json:"type" form:"type" binding:"required"`
}

func CreateWebHook(c *gin.Context) {
Expand All @@ -31,7 +31,7 @@ func CreateWebHook(c *gin.Context) {
return
}

if err = srv.CreateWebHook(req.ProjectID, req.Types, req.CallBackURL); err != nil {
if err = srv.CreateWebHook(req.ProjectID, req.Type, req.CallBackURL); err != nil {
response.APIError(c, err)
return
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func GetWebHookList(c *gin.Context) {

type GetWebHookRequest struct {
ProjectID int64 `json:"project_id" form:"project_id" binding:"required"`
Types string `json:"types" form:"types"`
Type string `json:"type" form:"type"`
}

func GetWebHook(c *gin.Context) {
Expand All @@ -93,7 +93,7 @@ func GetWebHook(c *gin.Context) {
return
}

webhook, err := srv.GetWebHook(req.ProjectID, req.Types)
webhook, err := srv.GetWebHook(req.ProjectID, req.Type)
if err != nil {
response.APIError(c, err)
return
Expand All @@ -104,7 +104,7 @@ func GetWebHook(c *gin.Context) {

type DeleteWebHookRequest struct {
ProjectID int64 `json:"project_id" form:"project_id" binding:"required"`
Types string `json:"types" form:"types" binding:"required"`
Type string `json:"type" form:"type" binding:"required"`
}

func DeleteWebHook(c *gin.Context) {
Expand All @@ -125,7 +125,7 @@ func DeleteWebHook(c *gin.Context) {
return
}

if err = srv.DeleteWebHook(nil, req.ProjectID, req.Types); err != nil {
if err = srv.DeleteWebHook(nil, req.ProjectID, req.Type); err != nil {
response.APIError(c, err)
return
}
Expand Down
1 change: 0 additions & 1 deletion common/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ type WebHook struct {
CallbackURL string `json:"callback_url" gorm:"column:callback_url;type:varchar(255);not null;comment:'回调地址'"`
ProjectID int64 `json:"project_id" gorm:"column:project_id;type:int(11);index:project_id;not null;comment:'关联项目id'"`
Type string `json:"type" gorm:"column:type;type:varchar(30);not null;index:type;comment:'webhook类型'"`
Secret string `json:"secret" gorm:"column:secret;type:varchar(32);not null;comment:'secret'"`
CreateTime int64 `json:"create_time" gorm:"column:create_time;type:int(11);not null;comment:'创建时间'"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/sqlStore/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *webHookStore) GetList(projectID int64) ([]*common.WebHook, error) {
res []*common.WebHook
)

err = s.GetReplica().Table(s.GetTable()).Where("project_id = ?", projectID).Find(&res).Error
err = s.GetReplica().Table(s.GetTable()).Where("project_id = ?", projectID).Order("create_time DESC").Find(&res).Error
if err != nil {
return nil, err
}
Expand Down

0 comments on commit db7ccb2

Please sign in to comment.