From e4890a9bfa2af590288e4884825f61becca5f2d6 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 21 Sep 2022 10:45:08 +0800 Subject: [PATCH] #1249 update --- models/action.go | 23 ++++-------- models/reward_operate_record.go | 7 ++-- models/task_config.go | 69 ++++++++++++++++++++++++++++++++--- modules/notification/action/action.go | 16 -------- routers/reward/point/point.go | 4 +- routers/task/config.go | 4 +- services/task/task.go | 24 +++++++----- 7 files changed, 92 insertions(+), 55 deletions(-) diff --git a/models/action.go b/models/action.go index db3114c95..d0a763e08 100755 --- a/models/action.go +++ b/models/action.go @@ -61,13 +61,10 @@ const ( ActionCreateGrampusNPUTrainTask //32 ActionCreateGrampusGPUTrainTask //33 ActionBindWechat //34 - ActionCreateCloudbrainTask //35 - ActionDatasetRecommended //36 - ActionCreateImage //37 - ActionImageRecommend //38 - ActionChangeUserAvatar //39 - ActionPushCommits //40 - ActionForkRepo //41 + ActionDatasetRecommended //35 + ActionCreateImage //36 + ActionImageRecommend //37 + ActionChangeUserAvatar //38 ) @@ -95,6 +92,7 @@ type Action struct { type ActionShow struct { OpType ActionType + TaskType TaskType RepoLink string ShortRepoFullDisplayName string Content string @@ -241,7 +239,8 @@ func (a *Action) GetRepoLink() string { func (a *Action) ToShow() *ActionShow { actionShow := &ActionShow{} - actionShow.OpType = GetTaskOptType(*a) + actionShow.OpType = a.OpType + actionShow.TaskType = GetTaskTypeFromAction(a.OpType) actionShow.Content = a.Content actionShow.RefName = a.RefName @@ -272,14 +271,6 @@ func (a *Action) ToShow() *ActionShow { return actionShow } -func GetTaskOptType(action Action) ActionType { - //Convert all types of cloudbrain tasks action into ActionCreateCloudbrainTask - if action.IsCloudbrainAction() { - return ActionCreateCloudbrainTask - } - return action.OpType -} - // GetRepositoryFromMatch returns a *Repository from a username and repo strings func GetRepositoryFromMatch(ownerName string, repoName string) (*Repository, error) { var err error diff --git a/models/reward_operate_record.go b/models/reward_operate_record.go index ad00cc05c..1992eccba 100644 --- a/models/reward_operate_record.go +++ b/models/reward_operate_record.go @@ -2,7 +2,6 @@ package models import ( "code.gitea.io/gitea/modules/timeutil" - "fmt" "strconv" "strings" "xorm.io/builder" @@ -355,7 +354,7 @@ type RewardRecordListOpts struct { OperateType RewardOperateType RewardType RewardType SourceType string - ActionType int + TaskType string SerialNo string OrderBy RewardOperateOrderBy IsAdmin bool @@ -381,8 +380,8 @@ func (opts *RewardRecordListOpts) toCond() builder.Cond { if opts.SourceType != "" { cond = cond.And(builder.Eq{"reward_operate_record.source_type": opts.SourceType}) } - if opts.ActionType > 0 { - cond = cond.And(builder.Eq{"reward_operate_record.source_template_id": fmt.Sprint(opts.ActionType)}) + if opts.TaskType != "" { + cond = cond.And(builder.Eq{"reward_operate_record.source_template_id": opts.TaskType}) } if opts.SerialNo != "" { cond = cond.And(builder.Like{"reward_operate_record.serial_no", opts.SerialNo}) diff --git a/models/task_config.go b/models/task_config.go index baa8c9adb..8650851be 100644 --- a/models/task_config.go +++ b/models/task_config.go @@ -2,7 +2,6 @@ package models import ( "code.gitea.io/gitea/modules/timeutil" - "fmt" "xorm.io/builder" ) @@ -11,6 +10,66 @@ const ( PeriodDaily = "DAILY" ) +type TaskType string + +const ( + TaskCreatePublicRepo TaskType = "CreatePublicRepo" + TaskCreateIssue TaskType = "CreateIssue" + TaskCreatePullRequest TaskType = "CreatePullRequest" + TaskCommentIssue TaskType = "CommentIssue" + TaskUploadAttachment TaskType = "UploadAttachment" + TaskCreateNewModelTask TaskType = "CreateNewModelTask" + TaskBindWechat TaskType = "BindWechat" + TaskCreateCloudbrainTask TaskType = "CreateCloudbrainTask" + TaskDatasetRecommended TaskType = "DatasetRecommended " + TaskCreateImage TaskType = "CreateImage" + TaskImageRecommend TaskType = "ImageRecommend" + TaskChangeUserAvatar TaskType = "ChangeUserAvatar" + TaskPushCommits TaskType = "PushCommits" +) + +func GetTaskTypeFromAction(a ActionType) TaskType { + switch a { + case ActionCreateDebugGPUTask, + ActionCreateDebugNPUTask, + ActionCreateTrainTask, + ActionCreateInferenceTask, + ActionCreateBenchMarkTask, + ActionCreateGPUTrainTask, + ActionCreateGrampusNPUTrainTask, + ActionCreateGrampusGPUTrainTask: + return TaskCreateCloudbrainTask + case ActionCreateRepo: + return TaskCreatePublicRepo + case ActionCreatePullRequest: + return TaskCreatePullRequest + case ActionCommentIssue: + return TaskCommentIssue + case ActionUploadAttachment: + return TaskUploadAttachment + case ActionCreateNewModelTask: + return TaskCreateNewModelTask + case ActionBindWechat: + return TaskBindWechat + case ActionDatasetRecommended: + return TaskDatasetRecommended + case ActionImageRecommend: + return TaskImageRecommend + case ActionCreateImage: + return TaskCreateImage + case ActionChangeUserAvatar: + return TaskChangeUserAvatar + case ActionCommitRepo, + ActionDeleteBranch, + ActionPushTag, + ActionDeleteTag: + return TaskPushCommits + case ActionCreateIssue: + return TaskCreateIssue + } + return "" +} + //PointTaskConfig Only add and delete are allowed, edit is not allowed //so if you want to edit config for some task code,please delete first and add new one type TaskConfig struct { @@ -124,8 +183,8 @@ func GetTaskConfigList() ([]*TaskConfig, error) { type GetTaskConfigOpts struct { ListOptions - Status int //1 normal 2 deleted - ActionType int + Status int //1 normal 2 deleted + TaskType string } func GetTaskConfigPageWithDeleted(opt GetTaskConfigOpts) ([]*TaskAndLimiterConfig, int64, error) { @@ -133,8 +192,8 @@ func GetTaskConfigPageWithDeleted(opt GetTaskConfigOpts) ([]*TaskAndLimiterConfi opt.Page = 1 } cond := builder.NewCond() - if opt.ActionType > 0 { - cond = cond.And(builder.Eq{"task_code": fmt.Sprint(opt.ActionType)}) + if opt.TaskType != "" { + cond = cond.And(builder.Eq{"task_code": opt.TaskType}) } var count int64 diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index bfe574328..84181c555 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -428,19 +428,3 @@ func (t *actionNotifier) NotifyChangeUserAvatar(user *models.User, form auth.Ava log.Error("notifyWatchers: %v", err) } } - -func (t *actionNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { - act := &models.Action{ - ActUserID: pusher.ID, - ActUser: pusher, - OpType: models.ActionPushCommits, - RepoID: repo.ID, - Repo: repo, - RefName: refName, - IsPrivate: repo.IsPrivate, - Content: fmt.Sprintf("%s|%s", oldCommitID, newCommitID), - } - if err := models.NotifyWatchers(act); err != nil { - log.Error("notifyWatchers: %v", err) - } -} diff --git a/routers/reward/point/point.go b/routers/reward/point/point.go index 88cd8b22a..c9ed207ad 100644 --- a/routers/reward/point/point.go +++ b/routers/reward/point/point.go @@ -141,7 +141,7 @@ func GetAdminRewardList(ctx *context.Context) { func buildAdminRewardRecordListOpts(ctx *context.Context) (*models.RewardRecordListOpts, error) { operateType := ctx.Query("operate") sourceType := ctx.Query("source") - actionType := ctx.QueryInt("action") + taskType := ctx.Query("action") serialNo := ctx.Query("serialNo") status := ctx.Query("status") @@ -161,7 +161,7 @@ func buildAdminRewardRecordListOpts(ctx *context.Context) (*models.RewardRecordL RewardType: models.RewardTypePoint, OrderBy: orderBy, SourceType: sourceType, - ActionType: actionType, + TaskType: taskType, SerialNo: serialNo, IsAdmin: true, Status: status, diff --git a/routers/task/config.go b/routers/task/config.go index 9740561d2..c8a994e39 100644 --- a/routers/task/config.go +++ b/routers/task/config.go @@ -13,11 +13,11 @@ import ( func GetTaskConfigList(ctx *context.Context) { page := ctx.QueryInt("Page") status := ctx.QueryInt("Status") - action := ctx.QueryInt("Action") + action := ctx.Query("Action") r, err := task.GetTaskConfigWithLimitList(models.GetTaskConfigOpts{ ListOptions: models.ListOptions{PageSize: 20, Page: page}, Status: status, - ActionType: action, + TaskType: action, }) if err != nil { log.Error("GetTaskConfigList error.%v", err) diff --git a/services/task/task.go b/services/task/task.go index 2ff2e13c4..5a2028572 100644 --- a/services/task/task.go +++ b/services/task/task.go @@ -15,19 +15,24 @@ func Accomplish(action models.Action) { log.Error("PANIC:%v", combinedErr) } }() - action.OpType = models.GetTaskOptType(action) - switch action.OpType { + taskType := models.GetTaskTypeFromAction(action.OpType) + if taskType == "" { + log.Info("Accomplish finished.taskType is not exist.action.ID=%d", action.ID) + return + } + + switch taskType { //only creating public repo can be rewarded - case models.ActionCreateRepo: + case models.TaskCreatePublicRepo: if action.Repo.IsPrivate { return } //only creating public image can be rewarded - case models.ActionCreateImage: + case models.TaskCreateImage: if action.IsPrivate { return } - case models.ActionBindWechat: + case models.TaskBindWechat: n, err := models.CountWechatBindLog(action.Content, models.WECHAT_BIND) if err != nil { log.Error("CountWechatBindLog error when accomplish task,err=%v", err) @@ -40,10 +45,10 @@ func Accomplish(action models.Action) { } } - go accomplish(action) + go accomplish(action, taskType) } -func accomplish(action models.Action) error { +func accomplish(action models.Action, taskType models.TaskType) error { defer func() { if err := recover(); err != nil { combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2)) @@ -51,10 +56,9 @@ func accomplish(action models.Action) error { } }() userId := action.ActUserID - taskType := fmt.Sprint(action.OpType) //get task config - config, err := GetTaskConfig(taskType) + config, err := GetTaskConfig(string(taskType)) if err != nil { log.Error("GetTaskConfig error,%v", err) return err @@ -86,7 +90,7 @@ func accomplish(action models.Action) error { reward.Operate(&models.RewardOperateContext{ SourceType: models.SourceTypeAccomplishTask, SourceId: fmt.Sprint(action.ID), - SourceTemplateId: fmt.Sprint(action.OpType), + SourceTemplateId: string(taskType), Title: config.Title, Reward: models.Reward{ Amount: config.AwardAmount,