forked from cvbarros/go-teamcity
/
trigger_vcs_options.go
159 lines (137 loc) · 4.66 KB
/
trigger_vcs_options.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package teamcity
import (
"fmt"
"strconv"
)
// VcsTriggerQuietPeriodMode specifies if the VCS Trigger will delay the start of a build after detecting a VCS change, used by TriggerVcsOptions.
type VcsTriggerQuietPeriodMode int
const (
// QuietPeriodDoNotUse disables QuietPeriod on VCS Trigger
QuietPeriodDoNotUse VcsTriggerQuietPeriodMode = 0
// QuietPeriodUseDefault instructs the VCS Trigger to respect the server-wide quiet period
QuietPeriodUseDefault VcsTriggerQuietPeriodMode = 1
// QuietPeriodCustom allows specifying a period in seconds via TriggerVcsOptions.QuietPeriodInSeconds. When using Custom, TriggerVcsOptions.QuietPeriodInSeconds is mandatory.
QuietPeriodCustom VcsTriggerQuietPeriodMode = 2
)
// TriggerVcsOptions represents optional settings for a VCS Trigger type.
type TriggerVcsOptions struct {
enableQueueOptimization bool
perCheckinTriggering bool `prop:"branch"`
GroupUserCheckins bool
QuietPeriodMode VcsTriggerQuietPeriodMode
QuietPeriodInSeconds int
}
// NewTriggerVcsOptions initialize a TriggerVcsOptions instance with same defaults as TeamCity UI
//
// Defaults:
// - GroupCheckins = false
// - EnableQueueOptimization = false
func NewTriggerVcsOptions(mode VcsTriggerQuietPeriodMode, seconds int) (*TriggerVcsOptions, error) {
quietPeriodInSeconds := 0
if mode == QuietPeriodCustom {
if seconds <= 0 {
return nil, fmt.Errorf("invalid valid %d for QuietPeriodInSeconds. Must be greater than zero when QuietPeriodModeCustom is used", quietPeriodInSeconds)
}
quietPeriodInSeconds = seconds
}
return &TriggerVcsOptions{
perCheckinTriggering: false,
enableQueueOptimization: true,
GroupUserCheckins: false,
QuietPeriodMode: mode,
QuietPeriodInSeconds: quietPeriodInSeconds,
}, nil
}
//QueueOptimization gets the value of enableQueueOptimization property
func (o *TriggerVcsOptions) QueueOptimization() bool {
return o.enableQueueOptimization
}
//SetQueueOptimization toggles allowing the server to replace an already started build or a more recently queued one if new changes are detected. If set to true, PerCheckinTriggering will be disabled.
func (o *TriggerVcsOptions) SetQueueOptimization(enable bool) {
o.enableQueueOptimization = enable
if enable {
o.perCheckinTriggering = false
}
}
//PerCheckinTriggering gets the value of perCheckinTriggering property
func (o *TriggerVcsOptions) PerCheckinTriggering() bool {
return o.perCheckinTriggering
}
// SetPerCheckinTriggering specifies if VCS Trigger will fire a different build per checkin or commit for different committers. If set to true, enableQueueOptimization will be disabled.
func (o *TriggerVcsOptions) SetPerCheckinTriggering(enable bool) {
o.perCheckinTriggering = enable
if enable {
o.enableQueueOptimization = false
}
}
var quietPeriodModePropertyMap = map[VcsTriggerQuietPeriodMode]string{
QuietPeriodDoNotUse: "DO_NOT_USE",
QuietPeriodUseDefault: "USE_DEFAULT",
QuietPeriodCustom: "USE_CUSTOM",
}
var propertyToQuietPeriodModeMap = map[string]VcsTriggerQuietPeriodMode{
"DO_NOT_USE": QuietPeriodDoNotUse,
"USE_DEFAULT": QuietPeriodUseDefault,
"USE_CUSTOM": QuietPeriodCustom,
}
func (o *TriggerVcsOptions) properties() *Properties {
var props []*Property
p := NewProperty("quietPeriodMode", quietPeriodModePropertyMap[o.QuietPeriodMode])
props = append(props, p)
if o.enableQueueOptimization {
p := NewProperty("enableQueueOptimization", "true")
props = append(props, p)
}
if o.perCheckinTriggering {
p := NewProperty("perCheckinTriggering", "true")
props = append(props, p)
}
if o.GroupUserCheckins {
p := NewProperty("groupCheckinsByCommitter", "true")
props = append(props, p)
}
if o.QuietPeriodInSeconds > 0 {
p := NewProperty("quietPeriod", strconv.Itoa(o.QuietPeriodInSeconds))
props = append(props, p)
}
return NewProperties(props...)
}
func (p *Properties) triggerVcsOptions() (*TriggerVcsOptions, error) {
var out TriggerVcsOptions
if v, ok := p.GetOk("quietPeriodMode"); ok {
out.QuietPeriodMode = propertyToQuietPeriodModeMap[v]
}
if v, ok := p.GetOk("quietPeriod"); ok {
v2, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
out.QuietPeriodInSeconds = v2
}
if v, ok := p.GetOk("groupCheckinsByCommitter"); ok {
v2, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
out.GroupUserCheckins = v2
}
if v, ok := p.GetOk("perCheckinTriggering"); ok {
v2, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
if v2 {
out.SetPerCheckinTriggering(v2)
}
}
if v, ok := p.GetOk("enableQueueOptimization"); ok {
v2, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
if v2 {
out.SetQueueOptimization(v2)
}
}
return &out, nil
}