This repository has been 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
/
Copy pathbranch.go
105 lines (91 loc) · 2.5 KB
/
branch.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
package main
type Branch map[string]interface{}
func (p Branch) Get(key string) interface{} {
return p[key]
}
func (c *Client) UnmarshalBranches(settings map[string]interface{}) ([]map[string]interface{}, error) {
branches := make([]map[string]interface{}, 0)
for n, b := range settings {
branch := make(map[string]interface{})
access := b.(map[string]interface{})
branch["name"] = n
if v, err := AccessString(access["push_access_level"].(string)); err == nil {
branch["push_access_level"] = v
} else {
return nil, err
}
if v, err := AccessString(access["merge_access_level"].(string)); err == nil {
branch["merge_access_level"] = v
} else {
return nil, err
}
if v, err := AccessString(access["unprotect_access_level"].(string)); err == nil {
branch["unprotect_access_level"] = v
} else {
return nil, err
}
if v, ok := access["allowed_to_push"]; ok {
branch["allowed_to_push"] = v
}
if v, ok := access["allowed_to_merge"]; ok {
branch["allowed_to_merge"] = v
}
if v, ok := access["allowed_to_unprotect"]; ok {
branch["allowed_to_unprotect"] = v
}
branches = append(branches, branch)
}
return branches, nil
}
func (c *Client) BranchAllowedToIds(branch map[string]interface{}) (map[string]interface{}, error) {
push, err := c.UnmarshallAllowed(branch["allowed_to_push"])
if err != nil {
return nil, err
}
branch["allowed_to_push"] = push
merge, err := c.UnmarshallAllowed(branch["allowed_to_merge"])
if err != nil {
return nil, err
}
branch["allowed_to_merge"] = merge
unpro, err := c.UnmarshallAllowed(branch["allowed_to_unprotect"])
branch["allowed_to_unprotect"] = unpro
return branch, nil
}
func (c *Client) UnmarshallAllowed(a interface{}) ([]map[string]interface{}, error) {
var allowed []map[string]interface{}
if a == nil {
return nil, nil
}
for _, m := range a.([]interface{}) {
for k, v := range InterfaceMapToStringMap(m.(map[interface{}]interface{})) {
var i int
var err error
switch k {
case "user_id":
i, err = c.GetUserIdByName(v)
if err != nil {
return nil, err
}
case "group_id":
i, err = c.GetGroupIdByName(v)
if err != nil {
return nil, err
}
}
allowed = append(allowed, map[string]interface{}{
k: i,
})
}
}
return allowed, nil
}
func FindBranchInList(name string, branches []map[string]interface{}) map[string]interface{} {
for _, b := range branches {
if b["name"].(string) == name {
return b
}
}
// return empty if not found
return map[string]interface{}{}
}