-
Notifications
You must be signed in to change notification settings - Fork 7
/
apply.go
129 lines (120 loc) · 4.26 KB
/
apply.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
package policy
import (
"errors"
"fmt"
"net/http"
"github.com/lunarway/release-manager/cmd/hamctl/command/completion"
"github.com/lunarway/release-manager/internal/git"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/spf13/cobra"
)
func NewApply(client *httpinternal.Client, service *string) *cobra.Command {
var command = &cobra.Command{
Use: "apply",
Short: "Apply a release policy for a service. See available commands for specific policies.",
// make sure that only valid args are applied and that at least one
// command is specified
Args: func(c *cobra.Command, args []string) error {
err := cobra.OnlyValidArgs(c, args)
if err != nil {
return err
}
if len(args) == 0 {
return errors.New("please specify a policy.")
}
return nil
},
ValidArgs: []string{"auto-release", "branch-restriction"},
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
},
}
command.AddCommand(autoRelease(client, service))
command.AddCommand(branchRestriction(client, service))
return command
}
func autoRelease(client *httpinternal.Client, service *string) *cobra.Command {
var branch, env string
var command = &cobra.Command{
Use: "auto-release",
Short: "Auto-release policy for releasing branch artifacts to an environment",
Args: cobra.ExactArgs(0),
RunE: func(c *cobra.Command, args []string) error {
committerName, committerEmail, err := git.CommitterDetails()
if err != nil {
return err
}
var resp httpinternal.ApplyPolicyResponse
path, err := client.URL(pathAutoRelease)
if err != nil {
return err
}
err = client.Do(http.MethodPatch, path, httpinternal.ApplyAutoReleasePolicyRequest{
Service: *service,
Branch: branch,
Environment: env,
CommitterEmail: committerEmail,
CommitterName: committerName,
}, &resp)
if err != nil {
return err
}
fmt.Printf("[✓] Applied auto-release policy '%s' to service '%s'\n", resp.ID, resp.Service)
return nil
},
}
command.Flags().StringVarP(&branch, "branch", "b", "", "Branch to auto-release artifacts from")
// errors are skipped here as the only case they can occour are if thee flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("branch")
completion.FlagAnnotation(command, "branch", "__hamctl_get_branches")
command.Flags().StringVarP(&env, "env", "e", "", "Environment to release artifacts to")
//nolint:errcheck
command.MarkFlagRequired("env")
completion.FlagAnnotation(command, "env", "__hamctl_get_environments")
return command
}
func branchRestriction(client *httpinternal.Client, service *string) *cobra.Command {
var branchRegex, env string
var command = &cobra.Command{
Use: "branch-restriction",
Short: "Branch restriction policy for limiting releases by their origin branch",
Long: "Branch restriction policy for limiting releases of artifacts by their origin branch to specific environments",
Args: cobra.ExactArgs(0),
RunE: func(c *cobra.Command, args []string) error {
committerName, committerEmail, err := git.CommitterDetails()
if err != nil {
return err
}
var resp httpinternal.ApplyBranchRestrictionPolicyResponse
path, err := client.URL(pathBranchRestrction)
if err != nil {
return err
}
err = client.Do(http.MethodPatch, path, httpinternal.ApplyBranchRestrictionPolicyRequest{
Service: *service,
BranchRegex: branchRegex,
Environment: env,
CommitterEmail: committerEmail,
CommitterName: committerName,
}, &resp)
if err != nil {
return err
}
fmt.Printf("[✓] Applied branch restriction policy '%s' to service '%s'\n", resp.ID, resp.Service)
return nil
},
}
command.Flags().StringVar(&branchRegex, "branch-regex", "", "Regular expression defining allowed branch names")
// errors are skipped here as the only case they can occur are if the flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("branch-regex")
completion.FlagAnnotation(command, "branch-regex", "__hamctl_get_branches")
command.Flags().StringVarP(&env, "env", "e", "", "Environment to apply restriction to")
//nolint:errcheck
command.MarkFlagRequired("env")
completion.FlagAnnotation(command, "env", "__hamctl_get_environments")
return command
}