forked from cvbarros/go-teamcity
/
trigger_vcs.go
135 lines (110 loc) · 3.34 KB
/
trigger_vcs.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
123
124
125
126
127
128
129
130
131
132
133
134
135
package teamcity
import (
"encoding/json"
"fmt"
"strings"
)
//TriggerVcs represents a build trigger on VCS changes
type TriggerVcs struct {
triggerJSON *triggerJSON
buildTypeID string
BranchFilter []string
Rules []string
Options *TriggerVcsOptions
}
//ID for this entity
func (t *TriggerVcs) ID() string {
return t.triggerJSON.ID
}
//Type returns TriggerTypes.Vcs ("vcsTrigger")
func (t *TriggerVcs) Type() string {
return TriggerTypes.Vcs
}
//SetDisabled controls whether this trigger is disabled or not
func (t *TriggerVcs) SetDisabled(disabled bool) {
t.triggerJSON.Disabled = NewBool(disabled)
}
//Disabled gets the disabled status for this trigger
func (t *TriggerVcs) Disabled() bool {
return *t.triggerJSON.Disabled
}
//BuildTypeID gets the build type identifier
func (t *TriggerVcs) BuildTypeID() string {
return t.buildTypeID
}
//SetBuildTypeID sets the build type identifier
func (t *TriggerVcs) SetBuildTypeID(id string) {
t.buildTypeID = id
}
// NewTriggerVcs returns a VCS trigger type with the triggerRules specified. triggerRules is required, but branchFilter can be optional if the VCS root uses multiple branches.
func NewTriggerVcs(triggerRules []string, branchFilter []string) (*TriggerVcs, error) {
opt, err := NewTriggerVcsOptions(QuietPeriodDoNotUse, 0)
if err != nil {
return nil, err
}
i, _ := NewTriggerVcsWithOptions(triggerRules, branchFilter, opt)
return i, nil
}
// NewTriggerVcsWithOptions returns a VCS trigger type with TriggerVcsOptions. See also NewTriggerVcs for other parameters.
func NewTriggerVcsWithOptions(triggerRules []string, branchFilter []string, opt *TriggerVcsOptions) (*TriggerVcs, error) {
if opt == nil {
return nil, fmt.Errorf("opt parameter must be valid TriggerVcsOptions, not nil")
}
newTriggerVcs := &TriggerVcs{
triggerJSON: &triggerJSON{
Disabled: NewFalse(),
Type: TriggerTypes.Vcs,
},
BranchFilter: branchFilter,
Rules: triggerRules,
Options: opt,
}
newTriggerVcs.triggerJSON.Properties = newTriggerVcs.properties()
return newTriggerVcs, nil
}
func (t *TriggerVcs) properties() *Properties {
props := t.Options.properties()
if len(t.BranchFilter) > 0 {
props.AddOrReplaceValue("branchFilter", strings.Join(t.BranchFilter, "\\n"))
}
if len(t.Rules) > 0 {
props.AddOrReplaceValue("triggerRules", strings.Join(t.Rules, "\\n"))
}
return props
}
//MarshalJSON implements JSON serialization for TriggerVcs
func (t *TriggerVcs) MarshalJSON() ([]byte, error) {
out := &triggerJSON{
ID: t.ID(),
Type: t.Type(),
Disabled: NewBool(t.Disabled()),
Properties: t.properties(),
}
return json.Marshal(out)
}
//UnmarshalJSON implements JSON deserialization for TriggerVcs
func (t *TriggerVcs) UnmarshalJSON(data []byte) error {
var aux triggerJSON
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if aux.Type != TriggerTypes.Vcs {
return fmt.Errorf("invalid type %s trying to deserialize into TriggerVcs entity", aux.Type)
}
if aux.Disabled != nil {
t.SetDisabled(*aux.Disabled)
}
t.triggerJSON = &aux
if v, ok := aux.Properties.GetOk("branchFilter"); ok {
t.BranchFilter = strings.Split(v, "\\n")
}
if v, ok := aux.Properties.GetOk("triggerRules"); ok {
t.Rules = strings.Split(v, "\\n")
}
opt, err := aux.Properties.triggerVcsOptions()
if err != nil {
return err
}
t.Options = opt
return nil
}