/
msg_server_task.go
103 lines (85 loc) · 3.19 KB
/
msg_server_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package keeper
import (
"context"
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gitopia/gitopia/v2/x/gitopia/types"
)
func (k msgServer) CreateTask(goCtx context.Context, msg *types.MsgCreateTask) (*types.MsgCreateTaskResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
var task = types.Task{
Creator: msg.Creator,
Type: msg.TaskType,
Provider: msg.Provider,
State: types.StatePending,
}
id := k.AppendTask(
ctx,
task,
)
return &types.MsgCreateTaskResponse{
Id: id,
}, nil
}
func (k msgServer) UpdateTask(goCtx context.Context, msg *types.MsgUpdateTask) (*types.MsgUpdateTaskResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Checks that the element exists
task, found := k.GetTask(ctx, msg.Id)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}
if task.State != types.StatePending {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "only pending state can be updated")
}
// Checks if the msg creator is the same as the current owner
if msg.Creator != task.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
task.State = msg.State
task.Message = msg.Message
k.SetTask(ctx, task)
if task.State == types.StateFailure {
switch task.Type {
case types.TypeForkRepository:
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyAction, types.ForkRepositoryEventKey),
sdk.NewAttribute(types.EventAttributeCreatorKey, msg.Creator),
sdk.NewAttribute(types.EventAttributeTaskIdKey, strconv.FormatUint(task.Id, 10)),
sdk.NewAttribute(types.EventAttributeTaskStateKey, task.State.String()),
sdk.NewAttribute(types.EventAttributeMessageKey, task.Message),
),
)
case types.TypeSetPullRequestState:
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyAction, types.SetPullRequestStateEventKey),
sdk.NewAttribute(types.EventAttributeCreatorKey, msg.Creator),
sdk.NewAttribute(types.EventAttributePullRequestIdKey, strconv.FormatUint(msg.Id, 10)),
sdk.NewAttribute(types.EventAttributeTaskIdKey, strconv.FormatUint(task.Id, 10)),
sdk.NewAttribute(types.EventAttributeTaskStateKey, task.State.String()),
sdk.NewAttribute(types.EventAttributeMessageKey, task.Message),
),
)
}
}
return &types.MsgUpdateTaskResponse{}, nil
}
func (k msgServer) DeleteTask(goCtx context.Context, msg *types.MsgDeleteTask) (*types.MsgDeleteTaskResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Checks that the element exists
val, found := k.GetTask(ctx, msg.Id)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}
// Checks if the msg creator is the same as the current owner
if msg.Creator != val.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
k.RemoveTask(ctx, msg.Id)
return &types.MsgDeleteTaskResponse{}, nil
}