Browse Source

#1249

update
pull/2933/head
chenyifan01 2 years ago
parent
commit
e4890a9bfa
7 changed files with 92 additions and 55 deletions
  1. +7
    -16
      models/action.go
  2. +3
    -4
      models/reward_operate_record.go
  3. +64
    -5
      models/task_config.go
  4. +0
    -16
      modules/notification/action/action.go
  5. +2
    -2
      routers/reward/point/point.go
  6. +2
    -2
      routers/task/config.go
  7. +14
    -10
      services/task/task.go

+ 7
- 16
models/action.go View File

@@ -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


+ 3
- 4
models/reward_operate_record.go View File

@@ -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})


+ 64
- 5
models/task_config.go View File

@@ -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


+ 0
- 16
modules/notification/action/action.go View File

@@ -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)
}
}

+ 2
- 2
routers/reward/point/point.go View File

@@ -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,


+ 2
- 2
routers/task/config.go View File

@@ -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)


+ 14
- 10
services/task/task.go View File

@@ -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,


Loading…
Cancel
Save