-
Notifications
You must be signed in to change notification settings - Fork 787
/
config.go
132 lines (116 loc) · 3.35 KB
/
config.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
130
131
132
package auth
import (
"errors"
"fmt"
)
//UserKind define for user kind
type UserKind string
const (
// UserKindLocal indicates a user of type local
UserKindLocal UserKind = "local"
// UserKindPipeline indicates a user of type pipeline (e.g. used by the build within the pipeline
UserKindPipeline UserKind = "pipeline"
)
//User store the auth infomation for a user
type User struct {
Username string `yaml:"username"`
ApiToken string `yaml:"apitoken"`
BearerToken string `yaml:"bearertoken"`
Password string `yaml:"password,omitempty" json:"password"`
Kind UserKind `yaml:"kind"`
}
//ServerKind type of the server
type ServerKind string
const (
// ServerKindGit idicates a server configuration for git
ServerKindGit ServerKind = "git"
// ServerKindIssue indicates a server configuration for issue
ServerKindIssue ServerKind = "issue"
// ServerKindChat idicates a server configuration for chat
ServerKindChat ServerKind = "chat"
)
//ServiceKind type for service used by the server
type ServiceKind string
const (
// ServiceKindGithub indicates that the git server is using as service GitHub
ServiceKindGithub ServiceKind = "github"
// ServiceKindGitlab indicates that the git server is using as service Gitlab
ServiceKindGitlab ServiceKind = "gitlab"
// ServiceKindGitea indicates that the git server is using as service Gitea
ServiceKindGitea ServiceKind = "gitea"
// ServiceKindBitbucketCloud indicates that the git server is using as service Bitbucket Cloud
ServiceKindBitbucketCloud ServiceKind = "bitbucketcloud"
// ServiceKindBitbucketServer indicates that the git server is using as service Bitbuckst Server
ServiceKindBitbucketServer ServiceKind = "bitbucketserver"
)
//Server stores the server configuration for a server
type Server struct {
URL string `yaml:"url"`
Name string `yaml:"name"`
Kind ServerKind `yaml:"kind"`
ServiceKind ServiceKind `yaml:"servicekind"`
Users []*User `yaml:"users"`
}
// Config stores the entire auth configuration for a number of sservers
type Config struct {
Servers []*Server `yaml:"servers"`
}
// Checker verifies if the configuration is valid
type Checker interface {
Valid() error
}
// Valid checks if the user config is valid
func (u *User) Valid() error {
if u.BearerToken != "" {
return nil
}
if u.Username == "" {
return errors.New("Empty username")
}
if u.ApiToken == "" && u.Password == "" {
return errors.New("Empty API token and password")
}
return nil
}
// Valid checks if a server config is valid
func (s *Server) Valid() error {
if len(s.Users) == 0 {
return fmt.Errorf("%s server has no users", s.Name)
}
if s.URL == "" {
return fmt.Errorf("%s server has an empty URL", s.Name)
}
for _, u := range s.Users {
err := u.Valid()
if err != nil {
return err
}
}
return nil
}
// PipelineUser returns the pipeline user, if there is not pipeline user available
// returns the first user
func (s *Server) PipelineUser() User {
for _, user := range s.Users {
if user.Kind == UserKindPipeline {
return *user
}
}
if len(s.Users) > 0 {
return *s.Users[0]
}
return User{}
}
// Valid checks if the config is valid
func (c *Config) Valid() error {
if len(c.Servers) == 0 {
return fmt.Errorf("No servers in config")
}
for _, s := range c.Servers {
err := s.Valid()
if err != nil {
return err
}
}
return nil
}