-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathhooks_handler.go
122 lines (100 loc) · 3.76 KB
/
hooks_handler.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package graveler
import (
"context"
"time"
"github.com/rs/xid"
)
type EventType string
const (
EventTypePreCommit EventType = "pre-commit"
EventTypePostCommit EventType = "post-commit"
EventTypePreMerge EventType = "pre-merge"
EventTypePostMerge EventType = "post-merge"
EventTypePreCreateTag EventType = "pre-create-tag"
EventTypePostCreateTag EventType = "post-create-tag"
EventTypePreDeleteTag EventType = "pre-delete-tag"
EventTypePostDeleteTag EventType = "post-delete-tag"
EventTypePreCreateBranch EventType = "pre-create-branch"
EventTypePostCreateBranch EventType = "post-create-branch"
EventTypePreDeleteBranch EventType = "pre-delete-branch"
EventTypePostDeleteBranch EventType = "post-delete-branch"
RunIDTimeLayout = "20060102150405"
UnixYear3000 = 32500915200
)
// HookRecord is an aggregation of all necessary fields for all event types
type HookRecord struct {
// Required fields for all event types:
RunID string
EventType EventType
RepositoryID RepositoryID
StorageNamespace StorageNamespace
// The reference which the actions files are read from
SourceRef Ref
// Event specific fields:
// Relevant for all event types except tags. For merge events this will be the ID of the destination branch
BranchID BranchID
// Relevant only for commit and merge events. In both it will contain the new commit data created from the operation
Commit Commit
// Not relevant in delete branch. In commit and merge will not exist in pre-action. In post actions will contain the new commit ID
CommitID CommitID
// Exists only in post actions. Contains the ID of the pre-action associated with this post-action
PreRunID string
// Exists only in tag actions.
TagID TagID
}
type HooksHandler interface {
PreCommitHook(ctx context.Context, record HookRecord) error
PostCommitHook(ctx context.Context, record HookRecord) error
PreMergeHook(ctx context.Context, record HookRecord) error
PostMergeHook(ctx context.Context, record HookRecord) error
PreCreateTagHook(ctx context.Context, record HookRecord) error
PostCreateTagHook(ctx context.Context, record HookRecord)
PreDeleteTagHook(ctx context.Context, record HookRecord) error
PostDeleteTagHook(ctx context.Context, record HookRecord)
PreCreateBranchHook(ctx context.Context, record HookRecord) error
PostCreateBranchHook(ctx context.Context, record HookRecord)
PreDeleteBranchHook(ctx context.Context, record HookRecord) error
PostDeleteBranchHook(ctx context.Context, record HookRecord)
// NewRunID TODO (niro): WA for now until KV feature complete
NewRunID() string
}
type HooksNoOp struct{}
func (h *HooksNoOp) PreCommitHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PostCommitHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PreMergeHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PostMergeHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PreCreateTagHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PostCreateTagHook(context.Context, HookRecord) {
}
func (h *HooksNoOp) PreDeleteTagHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PostDeleteTagHook(context.Context, HookRecord) {
}
func (h *HooksNoOp) PreCreateBranchHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PostCreateBranchHook(context.Context, HookRecord) {
}
func (h *HooksNoOp) PreDeleteBranchHook(context.Context, HookRecord) error {
return nil
}
func (h *HooksNoOp) PostDeleteBranchHook(context.Context, HookRecord) {
}
func (h *HooksNoOp) NewRunID() string {
return NewRunID()
}
func NewRunID() string {
tm := time.Unix(UnixYear3000-time.Now().Unix(), 0).UTC()
return xid.NewWithTime(tm).String()
}