Skip to content

Commit e3fdce6

Browse files
authored
Remove field requirements to work with GitHub (#41)
* Remove field requirements to work with GitHub * Fix lint errors
1 parent 15b5c10 commit e3fdce6

File tree

4 files changed

+176
-173
lines changed

4 files changed

+176
-173
lines changed

pkg/config/config.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package config
22

33
import (
4+
"encoding/json"
45
"strings"
6+
7+
"github.com/go-playground/validator/v10"
8+
"github.com/spf13/afero"
9+
)
10+
11+
const (
12+
defaultScheme = "https"
513
)
614

715
type ProviderType string
@@ -16,25 +24,15 @@ type Configuration struct {
1624
}
1725

1826
type Organization struct {
19-
Provider ProviderType `json:"provider" validate:"required"`
27+
Provider ProviderType `json:"provider" validate:"required,oneof='azuredevops' 'github'"`
2028
AzureDevOps AzureDevOps `json:"azuredevops"`
2129
GitHub GitHub `json:"github"`
22-
Host string `json:"host,omitempty" validate:"required"`
30+
Host string `json:"host,omitempty" validate:"required,hostname"`
2331
Scheme string `json:"scheme,omitempty" validate:"required"`
2432
Name string `json:"name" validate:"required"`
2533
Repositories []*Repository `json:"repositories" validate:"required,dive"`
2634
}
2735

28-
type AzureDevOps struct {
29-
Pat string `json:"pat"`
30-
}
31-
32-
type GitHub struct {
33-
AppID int64 `json:"appID"`
34-
InstallationID int64 `json:"installationID"`
35-
PrivateKey string `json:"privateKey"`
36-
}
37-
3836
func (o *Organization) GetSecretName(r *Repository) string {
3937
if r.SecretNameOverride != "" {
4038
return r.SecretNameOverride
@@ -49,9 +47,50 @@ func (o *Organization) GetSecretName(r *Repository) string {
4947
return strings.Join(comps, "-")
5048
}
5149

50+
type AzureDevOps struct {
51+
Pat string `json:"pat"`
52+
}
53+
54+
type GitHub struct {
55+
AppID int64 `json:"appID"`
56+
InstallationID int64 `json:"installationID"`
57+
PrivateKey string `json:"privateKey"`
58+
}
59+
5260
type Repository struct {
53-
Project string `json:"project" validate:"required"`
61+
Project string `json:"project"`
5462
Name string `json:"name" validate:"required"`
5563
Namespaces []string `json:"namespaces" validate:"required"`
5664
SecretNameOverride string `json:"secretNameOverride,omitempty"`
5765
}
66+
67+
func setConfigurationDefaults(cfg *Configuration) *Configuration {
68+
for i, o := range cfg.Organizations {
69+
if o.Scheme == "" {
70+
cfg.Organizations[i].Scheme = defaultScheme
71+
}
72+
}
73+
return cfg
74+
}
75+
76+
// LoadConfiguration parses and validates the configuration file at a given path.
77+
func LoadConfiguration(fs afero.Fs, path string) (*Configuration, error) {
78+
b, err := afero.ReadFile(fs, path)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
cfg := &Configuration{}
84+
err = json.Unmarshal(b, &cfg)
85+
if err != nil {
86+
return nil, err
87+
}
88+
cfg = setConfigurationDefaults(cfg)
89+
90+
validate := validator.New()
91+
err = validate.Struct(cfg)
92+
if err != nil {
93+
return nil, err
94+
}
95+
return cfg, nil
96+
}

pkg/config/config_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/afero"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func fsWithContent(content string) (afero.Fs, string, error) {
11+
path := "config.json"
12+
fs := afero.NewMemMapFs()
13+
file, err := fs.Create(path)
14+
if err != nil {
15+
return nil, "", err
16+
}
17+
defer file.Close()
18+
_, err = file.WriteString(content)
19+
if err != nil {
20+
return nil, "", err
21+
}
22+
return fs, path, nil
23+
}
24+
25+
const invalidJson = `
26+
{
27+
"host": "dev.azure.com",
28+
}}
29+
}
30+
`
31+
32+
func TestInvalidJson(t *testing.T) {
33+
fs, path, err := fsWithContent(invalidJson)
34+
require.NoError(t, err)
35+
_, err = LoadConfiguration(fs, path)
36+
require.Error(t, err)
37+
}
38+
39+
const validAzureDevOps = `
40+
{
41+
"organizations": [
42+
{
43+
"provider": "azuredevops",
44+
"azuredevops": {
45+
"pat": "foobar"
46+
},
47+
"host": "dev.azure.com",
48+
"name": "xenitab",
49+
"repositories": [
50+
{
51+
"project": "Lab",
52+
"name": "gitops-deployment",
53+
"namespaces": ["foo"]
54+
}
55+
]
56+
}
57+
]
58+
}
59+
`
60+
61+
// nolint:dupl // false positive
62+
func TestValidAzureDevOps(t *testing.T) {
63+
fs, path, err := fsWithContent(validAzureDevOps)
64+
require.NoError(t, err)
65+
cfg, err := LoadConfiguration(fs, path)
66+
require.NoError(t, err)
67+
68+
require.NotEmpty(t, cfg.Organizations)
69+
require.Equal(t, "azuredevops", string(cfg.Organizations[0].Provider))
70+
require.Equal(t, "foobar", cfg.Organizations[0].AzureDevOps.Pat)
71+
require.Equal(t, int64(0), cfg.Organizations[0].GitHub.AppID)
72+
require.Equal(t, int64(0), cfg.Organizations[0].GitHub.InstallationID)
73+
require.Equal(t, "", cfg.Organizations[0].GitHub.PrivateKey)
74+
require.Equal(t, "dev.azure.com", cfg.Organizations[0].Host)
75+
require.Equal(t, "https", cfg.Organizations[0].Scheme)
76+
require.Equal(t, "xenitab", cfg.Organizations[0].Name)
77+
require.NotEmpty(t, cfg.Organizations[0].Repositories)
78+
require.Equal(t, "gitops-deployment", cfg.Organizations[0].Repositories[0].Name)
79+
require.Equal(t, "Lab", cfg.Organizations[0].Repositories[0].Project)
80+
}
81+
82+
const validGitHub = `
83+
{
84+
"organizations": [
85+
{
86+
"provider": "github",
87+
"github": {
88+
"appID": 123,
89+
"installationID": 123,
90+
"privateKey": "foobar"
91+
},
92+
"host": "github.com",
93+
"name": "xenitab",
94+
"repositories": [
95+
{
96+
"name": "gitops-deployment",
97+
"namespaces": ["foo"]
98+
}
99+
]
100+
}
101+
]
102+
}
103+
`
104+
105+
// nolint:dupl // false positive
106+
func TestValidGitHub(t *testing.T) {
107+
fs, path, err := fsWithContent(validGitHub)
108+
require.NoError(t, err)
109+
cfg, err := LoadConfiguration(fs, path)
110+
require.NoError(t, err)
111+
112+
require.NotEmpty(t, cfg.Organizations)
113+
require.Equal(t, "github", string(cfg.Organizations[0].Provider))
114+
require.Equal(t, "", cfg.Organizations[0].AzureDevOps.Pat)
115+
require.Equal(t, int64(123), cfg.Organizations[0].GitHub.AppID)
116+
require.Equal(t, int64(123), cfg.Organizations[0].GitHub.InstallationID)
117+
require.Equal(t, "foobar", cfg.Organizations[0].GitHub.PrivateKey)
118+
require.Equal(t, "github.com", cfg.Organizations[0].Host)
119+
require.Equal(t, "https", cfg.Organizations[0].Scheme)
120+
require.Equal(t, "xenitab", cfg.Organizations[0].Name)
121+
require.NotEmpty(t, cfg.Organizations[0].Repositories)
122+
require.Equal(t, "gitops-deployment", cfg.Organizations[0].Repositories[0].Name)
123+
require.Equal(t, "", cfg.Organizations[0].Repositories[0].Project)
124+
}

pkg/config/load.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

pkg/config/load_test.go

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)