-
Notifications
You must be signed in to change notification settings - Fork 7
/
intent.go
94 lines (80 loc) · 2.23 KB
/
intent.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
package intent
import "fmt"
const (
TypeReleaseArtifact = "ReleaseArtifact"
TypeReleaseBranch = "ReleaseBranch"
TypePromote = "Promote"
TypeRollback = "Rollback"
TypeAutoRelease = "AutoRelease"
)
type Intent struct {
Type string `json:"type,omitempty"`
ReleaseBranch ReleaseBranchIntent `json:"releaseBranch,omitempty"`
Promote PromoteIntent `json:"promote,omitempty"`
Rollback RollbackIntent `json:"rollback,omitempty"`
}
type ReleaseBranchIntent struct {
Branch string `json:"branch,omitempty"`
}
type PromoteIntent struct {
FromEnvironment string `json:"fromEnvironment,omitempty"`
}
type RollbackIntent struct {
PreviousArtifactID string `json:"previousArtifactId,omitempty"`
}
func NewReleaseArtifact() Intent {
return Intent{
Type: TypeReleaseArtifact,
}
}
func NewReleaseBranch(branch string) Intent {
return Intent{
Type: TypeReleaseBranch,
ReleaseBranch: ReleaseBranchIntent{
Branch: branch,
},
}
}
func NewPromoteEnvironment(fromEnvironment string) Intent {
return Intent{
Type: TypePromote,
Promote: PromoteIntent{
FromEnvironment: fromEnvironment,
},
}
}
func NewAutoRelease() Intent {
return Intent{
Type: TypeAutoRelease,
}
}
func NewRollback(previousArtifactID string) Intent {
return Intent{
Type: TypeRollback,
Rollback: RollbackIntent{
PreviousArtifactID: previousArtifactID,
},
}
}
func (intent *Intent) Valid() bool {
return !intent.Empty()
}
func (intent *Intent) Empty() bool {
return intent.Type == ""
}
func (intent *Intent) AsArtifactWithIntent(artifactID string) string {
switch intent.Type {
case TypeReleaseBranch:
return fmt.Sprintf("branch '%s' with artifact '%s'", intent.ReleaseBranch.Branch, artifactID)
case TypeReleaseArtifact:
return fmt.Sprintf("artifact '%s'", artifactID)
case TypePromote:
return fmt.Sprintf("promotion from '%s' with artifact '%s'", intent.Promote.FromEnvironment, artifactID)
case TypeRollback:
return fmt.Sprintf("rollback to artifact '%s' from artifact '%s'", artifactID, intent.Rollback.PreviousArtifactID)
case TypeAutoRelease:
return fmt.Sprintf("autorelease artifact '%s'", artifactID)
default:
return fmt.Sprintf("invalid intent with artifact '%s'", artifactID)
}
}