This repository was archived by the owner on Dec 19, 2022. It is now read-only.
forked from leominov/gitlab-project-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (131 loc) · 2.75 KB
/
main.go
File metadata and controls
152 lines (131 loc) · 2.75 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/logrusorgru/aurora"
)
var (
flagConfigFile = flag.String("config", "./config.yml", "Path to configuration file")
flagDryRun = flag.Bool("dry-run", false, "Dry run mode")
flagNoColours = flag.Bool("no-colours", false, "Disable colour output")
cfg *Config
formatter aurora.Aurora
)
func init() {
formatter = aurora.NewAurora(!*flagNoColours)
}
func InArray(k string, arr []string) bool {
for _, val := range arr {
if val == k {
return true
}
}
return false
}
func InProjects(k string, arr []*Project) bool {
for _, p := range arr {
if k == p.Get("name").(string) {
return true
}
}
return false
}
func CanProcessProject(name string) bool {
if InArray(name, cfg.ExcludeProjects) {
return false
}
if len(cfg.OnlyProject) == 0 {
return true
}
if InArray(name, cfg.OnlyProject) {
return true
}
return false
}
func IsEqual(a, b interface{}) bool {
if fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b) {
return true
}
return false
}
func CopyMap(m map[string]interface{}) map[string]interface{} {
cp := make(map[string]interface{})
for k, v := range m {
vm, ok := v.(map[string]interface{})
if ok {
cp[k] = CopyMap(vm)
} else {
cp[k] = v
}
}
return cp
}
func main() {
var err error
flag.Parse()
cfg, err = ConfigFromFile(*flagConfigFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
client := NewClient(cfg.GitLabUrl, cfg.GitLabToken)
groupId, err := client.GetGroupIdByName(cfg.GroupID)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = client.UpdateGroup(groupId, cfg.GroupSettings)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
projects, err := client.GetGroupProjects(groupId)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Found %d projects\n", len(projects))
for _, project := range projects {
name := project.Get("name").(string)
if !CanProcessProject(name) {
continue
}
settings := cfg.Settings
if v, ok := cfg.Overrides[strings.ToLower(name)]; ok {
settings = CopyMap(settings)
err := MergeConfig(settings, v.(map[string]interface{}))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
err = client.UpdateProject(project, settings)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if cfg.CreateMissing {
for _, name := range cfg.OnlyProject {
if InProjects(name, projects) {
continue
}
settings := cfg.Settings
if v, ok := cfg.Overrides[name]; ok {
settings = CopyMap(settings)
err := MergeConfig(settings, v.(map[string]interface{}))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
err = client.CreateProject(name, groupId, settings)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
}