Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 1b00790

Browse files
author
Noah Lee
authored
Separate the config file (#388)
1 parent 59aed2c commit 1b00790

File tree

4 files changed

+278
-263
lines changed

4 files changed

+278
-263
lines changed

model/extent/config.go

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package extent
22

33
import (
4-
"regexp"
54
"strconv"
6-
"strings"
7-
"time"
85

96
"github.com/drone/envsubst"
10-
"github.com/gitploy-io/cronexpr"
117
"gopkg.in/yaml.v3"
128

139
eutil "github.com/gitploy-io/gitploy/pkg/e"
@@ -22,45 +18,6 @@ type (
2218
source []byte
2319
}
2420

25-
Env struct {
26-
Name string `json:"name" yaml:"name"`
27-
28-
// GitHub parameters of deployment.
29-
Task *string `json:"task" yaml:"task"`
30-
Description *string `json:"description" yaml:"description"`
31-
AutoMerge *bool `json:"auto_merge" yaml:"auto_merge"`
32-
RequiredContexts *[]string `json:"required_contexts,omitempty" yaml:"required_contexts"`
33-
Payload interface{} `json:"payload" yaml:"payload"`
34-
ProductionEnvironment *bool `json:"production_environment" yaml:"production_environment"`
35-
36-
// DeployableRef validates the ref is deployable or not.
37-
DeployableRef *string `json:"deployable_ref" yaml:"deployable_ref"`
38-
39-
// AutoDeployOn deploys automatically when the pattern is matched.
40-
AutoDeployOn *string `json:"auto_deploy_on" yaml:"auto_deploy_on"`
41-
42-
// Serialization verify if there is a running deployment.
43-
Serialization *bool `json:"serialization" yaml:"serialization"`
44-
45-
// Review is the configuration of Review,
46-
// It is disabled when it is empty.
47-
Review *Review `json:"review,omitempty" yaml:"review"`
48-
49-
// FrozenWindows is the list of windows to freeze deployments.
50-
FrozenWindows []FrozenWindow `json:"frozen_windows" yaml:"frozen_windows"`
51-
}
52-
53-
Review struct {
54-
Enabled bool `json:"enabled" yaml:"enabled"`
55-
Reviewers []string `json:"reviewers" yaml:"reviewers"`
56-
}
57-
58-
FrozenWindow struct {
59-
Start string `json:"start" yaml:"start"`
60-
Duration string `json:"duration" yaml:"duration"`
61-
Location string `json:"location" yaml:"location"`
62-
}
63-
6421
EvalValues struct {
6522
IsRollback bool
6623
}
@@ -146,78 +103,3 @@ func (c *Config) GetEnv(name string) *Env {
146103

147104
return nil
148105
}
149-
150-
// IsProductionEnvironment verifies whether the environment is production or not.
151-
func (e *Env) IsProductionEnvironment() bool {
152-
return e.ProductionEnvironment != nil && *e.ProductionEnvironment
153-
}
154-
155-
// IsDeployableRef verifies the ref is deployable.
156-
func (e *Env) IsDeployableRef(ref string) (bool, error) {
157-
if e.DeployableRef == nil {
158-
return true, nil
159-
}
160-
161-
matched, err := regexp.MatchString(*e.DeployableRef, ref)
162-
if err != nil {
163-
return false, eutil.NewError(eutil.ErrorCodeConfigInvalid, err)
164-
}
165-
166-
return matched, nil
167-
}
168-
169-
// IsAutoDeployOn verifies the ref is matched with 'auto_deploy_on'.
170-
func (e *Env) IsAutoDeployOn(ref string) (bool, error) {
171-
if e.AutoDeployOn == nil {
172-
return false, nil
173-
}
174-
175-
matched, err := regexp.MatchString(*e.AutoDeployOn, ref)
176-
if err != nil {
177-
return false, eutil.NewError(eutil.ErrorCodeConfigInvalid, err)
178-
}
179-
180-
return matched, nil
181-
}
182-
183-
// HasReview check whether the review is enabled or not.
184-
func (e *Env) HasReview() bool {
185-
return e.Review != nil && e.Review.Enabled
186-
}
187-
188-
// IsFreezed verifies whether the current time is in a freeze window.
189-
// It returns an error when parsing an expression is failed.
190-
func (e *Env) IsFreezed(t time.Time) (bool, error) {
191-
if len(e.FrozenWindows) == 0 {
192-
return false, nil
193-
}
194-
195-
for _, w := range e.FrozenWindows {
196-
s, err := cronexpr.ParseInLocation(strings.TrimSpace(w.Start), w.Location)
197-
if err != nil {
198-
return false, eutil.NewErrorWithMessage(
199-
eutil.ErrorCodeConfigInvalid,
200-
"The crontab expression of the freeze window is invalid.",
201-
err,
202-
)
203-
}
204-
205-
d, err := time.ParseDuration(w.Duration)
206-
if err != nil {
207-
return false, eutil.NewErrorWithMessage(
208-
eutil.ErrorCodeConfigInvalid,
209-
"The duration of the freeze window is invalid.",
210-
err,
211-
)
212-
}
213-
214-
// Add one minute to include the starting time.
215-
start := s.Prev(t.Add(time.Minute))
216-
end := start.Add(d)
217-
if t.After(start) && t.Before(end) {
218-
return true, nil
219-
}
220-
}
221-
222-
return false, nil
223-
}

model/extent/config_test.go

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package extent
33
import (
44
"reflect"
55
"testing"
6-
"time"
76

87
"github.com/AlekSi/pointer"
98
"github.com/davecgh/go-spew/spew"
@@ -187,147 +186,3 @@ envs:
187186
}
188187
})
189188
}
190-
191-
func TestEnv_IsProductionEnvironment(t *testing.T) {
192-
t.Run("Reutrn false when the production environment is nil", func(t *testing.T) {
193-
e := &Env{}
194-
195-
expected := false
196-
if e.IsProductionEnvironment() != expected {
197-
t.Errorf("IsProductionEnvironment = %v, wanted %v", e.IsProductionEnvironment(), expected)
198-
}
199-
})
200-
201-
t.Run("Reutrn true when the production environment is true", func(t *testing.T) {
202-
e := &Env{
203-
ProductionEnvironment: pointer.ToBool(true),
204-
}
205-
206-
expected := true
207-
if e.IsProductionEnvironment() != expected {
208-
t.Errorf("IsProductionEnvironment = %v, wanted %v", e.IsProductionEnvironment(), expected)
209-
}
210-
})
211-
}
212-
213-
func TestEnv_IsDeployableRef(t *testing.T) {
214-
t.Run("Return true when 'deployable_ref' is not defined.", func(t *testing.T) {
215-
e := &Env{}
216-
217-
ret, err := e.IsDeployableRef("")
218-
if err != nil {
219-
t.Fatalf("IsDeployableRef returns an error: %s", err)
220-
}
221-
222-
expected := true
223-
if ret != expected {
224-
t.Fatalf("IsDeployableRef = %v, wanted %v", ret, expected)
225-
}
226-
})
227-
228-
t.Run("Return true when 'deployable_ref' is matched.", func(t *testing.T) {
229-
e := &Env{
230-
DeployableRef: pointer.ToString("main"),
231-
}
232-
233-
ret, err := e.IsDeployableRef("main")
234-
if err != nil {
235-
t.Fatalf("IsDeployableRef returns an error: %s", err)
236-
}
237-
238-
expected := true
239-
if ret != expected {
240-
t.Fatalf("IsDeployableRef = %v, wanted %v", ret, expected)
241-
}
242-
})
243-
244-
t.Run("Return false when 'deployable_ref' is not matched.", func(t *testing.T) {
245-
e := &Env{
246-
DeployableRef: pointer.ToString("main"),
247-
}
248-
249-
ret, err := e.IsDeployableRef("branch")
250-
if err != nil {
251-
t.Fatalf("IsDeployableRef returns an error: %s", err)
252-
}
253-
254-
expected := false
255-
if ret != expected {
256-
t.Fatalf("IsDeployableRef = %v, wanted %v", ret, expected)
257-
}
258-
})
259-
}
260-
261-
func TestEnv_IsFreezed(t *testing.T) {
262-
t.Run("Return true when the time is in the window", func(t *testing.T) {
263-
runs := []struct {
264-
t time.Time
265-
e *Env
266-
want bool
267-
}{
268-
{
269-
t: time.Date(2012, 12, 1, 23, 55, 10, 0, time.UTC),
270-
e: &Env{
271-
FrozenWindows: []FrozenWindow{
272-
{
273-
Start: "55 23 * Dec *",
274-
Duration: "10m",
275-
},
276-
},
277-
},
278-
want: true,
279-
},
280-
{
281-
t: time.Date(2012, 1, 1, 0, 3, 0, 0, time.UTC),
282-
e: &Env{
283-
FrozenWindows: []FrozenWindow{
284-
{
285-
Start: "55 23 * Dec *",
286-
Duration: "10m",
287-
},
288-
},
289-
},
290-
want: true,
291-
},
292-
}
293-
e := &Env{
294-
FrozenWindows: []FrozenWindow{
295-
{
296-
Start: "55 23 * Dec *",
297-
Duration: "10m",
298-
},
299-
},
300-
}
301-
302-
for _, r := range runs {
303-
freezed, err := e.IsFreezed(r.t)
304-
if err != nil {
305-
t.Fatalf("IsFreezed returns an error: %s", err)
306-
}
307-
308-
if freezed != r.want {
309-
t.Fatalf("IsFreezed = %v, wanted %v", freezed, r.want)
310-
}
311-
}
312-
})
313-
314-
t.Run("Return false when the time is out of the window", func(t *testing.T) {
315-
e := &Env{
316-
FrozenWindows: []FrozenWindow{
317-
{
318-
Start: "55 23 * Dec *",
319-
Duration: "10m",
320-
},
321-
},
322-
}
323-
324-
freezed, err := e.IsFreezed(time.Date(2012, 1, 1, 0, 10, 0, 0, time.UTC))
325-
if err != nil {
326-
t.Fatalf("IsFreezed returns an error: %s", err)
327-
}
328-
329-
if freezed != false {
330-
t.Fatalf("IsFreezed = %v, wanted %v", freezed, false)
331-
}
332-
})
333-
}

0 commit comments

Comments
 (0)