forked from cvbarros/go-teamcity
/
step.go
115 lines (101 loc) · 3.24 KB
/
step.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
package teamcity
import (
"encoding/json"
"fmt"
)
// BuildStepType represents most common step types for build steps
type BuildStepType = string
const (
//StepTypePowershell step type
StepTypePowershell BuildStepType = "jetbrains_powershell"
//StepTypeDotnetCli step type
StepTypeDotnetCli BuildStepType = "dotnet.cli"
//StepTypeCommandLine (shell/cmd) step type
StepTypeCommandLine BuildStepType = "simpleRunner"
StepTypeOctopusPushPackage BuildStepType = "octopus.push.package"
StepTypeOctopusCreateRelease BuildStepType = "octopus.create.release"
)
//StepExecuteMode represents how a build configuration step will execute regarding others.
type StepExecuteMode = string
const (
//StepExecuteModeDefault executes the step only if all previous steps finished successfully.
StepExecuteModeDefault = "default"
//StepExecuteModeOnlyIfBuildIsSuccessful executes the step only if the whole build is successful.
StepExecuteModeOnlyIfBuildIsSuccessful = "execute_if_success"
//StepExecuteModeEvenWhenFailed executes the step even if previous steps failed.
StepExecuteModeEvenWhenFailed = "execute_if_failed"
//StepExecuteAlways executes even if build stop command was issued.
StepExecuteAlways = "execute_always"
)
// Step interface represents a a build configuration/template build step. To interact with concrete step types, see the Step* types.
type Step interface {
GetID() string
GetName() string
Type() string
serializable() *stepJSON
}
type stepJSON struct {
Disabled *bool `json:"disabled,omitempty" xml:"disabled"`
Href string `json:"href,omitempty" xml:"href"`
ID string `json:"id,omitempty" xml:"id"`
Inherited *bool `json:"inherited,omitempty" xml:"inherited"`
Name string `json:"name,omitempty" xml:"name"`
Properties *Properties `json:"properties,omitempty"`
Type string `json:"type,omitempty" xml:"type"`
}
type stepsJSON struct {
Count int32 `json:"count,omitempty" xml:"count"`
Items []*stepJSON `json:"step"`
}
var stepsReadingFunc = func(dt []byte, out interface{}) error {
var payload stepsJSON
if err := json.Unmarshal(dt, &payload); err != nil {
return err
}
var steps = make([]Step, payload.Count)
for i := 0; i < int(payload.Count); i++ {
sdt, err := json.Marshal(payload.Items[i])
if err != nil {
return err
}
err = stepReadingFunc(sdt, &steps[i])
if err != nil {
return err
}
}
replaceValue(out, &steps)
return nil
}
var stepReadingFunc = func(dt []byte, out interface{}) error {
var payload stepJSON
if err := json.Unmarshal(dt, &payload); err != nil {
return err
}
var step Step
var err error
switch payload.Type {
case string(StepTypePowershell):
var ps StepPowershell
err = ps.UnmarshalJSON(dt)
step = &ps
case string(StepTypeCommandLine):
var cmd StepCommandLine
err = cmd.UnmarshalJSON(dt)
step = &cmd
case string(StepTypeOctopusPushPackage):
var opp StepOctopusPushPackage
err = opp.UnmarshalJSON(dt)
step = &opp
case string(StepTypeOctopusCreateRelease):
var ocr StepOctopusCreateRelease
err = ocr.UnmarshalJSON(dt)
step = &ocr
default:
return fmt.Errorf("Unsupported step type: '%s' (id:'%s')", payload.Type, payload.ID)
}
if err != nil {
return err
}
replaceValue(out, &step)
return nil
}