-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook_agent.go
More file actions
181 lines (155 loc) · 4.94 KB
/
hook_agent.go
File metadata and controls
181 lines (155 loc) · 4.94 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"github.com/gogap/logs"
"strings"
)
// HookAgent defines a webhook agent interface. Struct of a webhook agent should satisfies the following interface.
type HookAgent interface {
Name() string
Parse(b []byte) error
CanTriggerEvent() bool
HookBranch() string
HookProject() string
Environment() string
}
// PullRequestHookAgent is the agent for pull request transfer.
type PullRequestHookAgent struct {
prHook PullRequestHook
isParsed bool
}
// Name is the agent name implementation.
func (agent *PullRequestHookAgent) Name() string {
return "PullRequestHookAgent"
}
// Parse unmarshal given bytes to agent.
func (agent *PullRequestHookAgent) Parse(b []byte) error {
var e error
agent.isParsed = false
if e = json.Unmarshal(b, &agent.prHook); e == nil {
agent.isParsed = true
logs.Debug("PR:", agent.prHook.PullRequest.Title, "/", agent.prHook.PullRequest.Base.Repo.Name,
"/", agent.prHook.PullRequest.Base.Ref, "/", agent.prHook.PullRequest.State)
}
return e
}
// CanTriggerEvent determines whether an agent can trigger following events.
// The pull request webhook can trigger CD events only if the state is "merged".
func (agent *PullRequestHookAgent) CanTriggerEvent() bool {
return agent.prHook.PullRequest.State == "merged"
}
// HookBranch returns the branch name of a pull request.
func (agent *PullRequestHookAgent) HookBranch() string {
if !agent.isParsed {
return ""
}
return agent.prHook.PullRequest.Base.Ref
}
// HookProject returns the project name of a pull request.
func (agent *PullRequestHookAgent) HookProject() string {
if !agent.isParsed {
return ""
}
return agent.prHook.PullRequest.Base.Repo.Name
}
// Environment returns "debug" or "production" based on the branch and project.
func (agent *PullRequestHookAgent) Environment() string {
return hookBranchEnvironment(agent.HookBranch())
}
// PushTagHookAgent is the agent for pull request transfer.
type PushTagHookAgent struct {
pushHook PushTagHook
isParsed bool
}
// Name is the agent name implementation.
func (agent *PushTagHookAgent) Name() string {
return "PushTagHookAgent"
}
// Parse unmarshal given bytes to agent.
func (agent *PushTagHookAgent) Parse(b []byte) error {
var e error
agent.isParsed = false
if e = json.Unmarshal(b, &agent.pushHook); e == nil {
agent.isParsed = true
logs.Debug("TagPush:", agent.pushHook.Ref, "/", agent.pushHook.Project.Name, "/", agent.pushHook.Project.FullName)
}
return e
}
// CanTriggerEvent determines whether an agent can trigger following events.
// The push tag hook cannot trigger any events.
func (agent *PushTagHookAgent) CanTriggerEvent() bool {
return false
}
// HookBranch returns the branch name of a push or the tag name.
func (agent *PushTagHookAgent) HookBranch() string {
if !agent.isParsed {
return ""
}
return agent.pushHook.Ref
}
// HookProject returns the project name of a push or tag.
func (agent *PushTagHookAgent) HookProject() string {
if !agent.isParsed {
return ""
}
return agent.pushHook.Project.Name
}
// Environment returns "debug" or "production" based on the branch and project.
func (agent *PushTagHookAgent) Environment() string {
return hookBranchEnvironment(agent.HookBranch())
}
// DefaultHookAgent is a fake agent struct, a default agent cannot trigger any following events.
type DefaultHookAgent struct {
isParsed bool
}
// Name is the agent name implementation.
func (agent *DefaultHookAgent) Name() string {
return "DefaultHookAgent"
}
// Parse unmarshal given bytes to agent.
func (agent *DefaultHookAgent) Parse(_ []byte) error {
return nil
}
// CanTriggerEvent determines whether an agent can trigger following events.
// The default agent cannot trigger any events.
func (agent *DefaultHookAgent) CanTriggerEvent() bool {
return false
}
// HookBranch always returns "unknown" for a default agent.
func (agent *DefaultHookAgent) HookBranch() string {
return "unknown"
}
// HookProject always returns "unknown" for a default agent.
func (agent *DefaultHookAgent) HookProject() string {
return "unknown"
}
// Environment always returns "unknown" for a default agent.
func (agent *DefaultHookAgent) Environment() string {
return "unknown"
}
func hookBranchEnvironment(branch string) string {
if strings.HasPrefix(branch, "master") || strings.HasPrefix(branch, "release") {
return "production"
}
return "debug"
}
func createHookAgentByName(name string) HookAgent {
if name == "merge_request_hooks" {
return &PullRequestHookAgent{}
}
if name == "tag_push_hooks" || name == "push_hooks" {
return &PushTagHookAgent{}
}
return &DefaultHookAgent{}
}
func createNotifierByAgent(agent HookAgent) *JenkinsNotifier {
project := matchJenkinsProject(agent.Environment(), agent.HookProject(), agent.HookBranch())
notifier := JenkinsNotifier{
JenkinsHost: settings.jenkinsHost,
JenkinsUrl: settings.jenkinsNotifyUrl,
JenkinsProject: project,
UserName: settings.jenkinsUserName,
UserApiToken: settings.jenkinsUserApiToken,
}
return ¬ifier
}