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 pathvariables.go
More file actions
86 lines (73 loc) · 2.34 KB
/
variables.go
File metadata and controls
86 lines (73 loc) · 2.34 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/spf13/viper"
)
func (c *Client) UpdateVariables(entity string, id int, settings interface{}) error {
existingVariables, err := c.GetVariables(entity, id)
if err != nil {
return err
}
for _, v := range settings.([]interface{}) {
variable := InterfaceMapToInterfaceMap(v.(map[interface{}]interface{}))
// if variable is masked mask its value
oldMasking := viper.GetStringSlice("mask")
if variable["masked"].(bool) {
viper.Set("mask", append(viper.GetStringSlice("mask"), "value"))
}
if v, ok := existingVariables[variable["key"].(string)]; ok {
diff, _, _, equal := computeDiff(v.(map[string]interface{}), variable)
if !equal {
fmt.Printf("\t Updating variable '%s'\n", variable["key"].(string))
fmt.Println(diff)
}
if !*flagDryRun && !equal {
_, err = c.doFormRequest(http.MethodPut, fmt.Sprintf("%s/%d/variables/%s", entity, id, variable["key"].(string)), variable)
if err != nil {
return fmt.Errorf("error updating variable: %v", err)
}
}
} else {
diff, _, _, equal := computeDiff(map[string]interface{}{"key": variable["key"].(string)}, variable)
if !equal {
fmt.Printf("\t Creating variable '%s'\n", variable["key"].(string))
fmt.Println(diff)
}
if !*flagDryRun && !equal {
_, err = c.doFormRequest(http.MethodPost, fmt.Sprintf("%s/%d/variables", entity, id), variable)
if err != nil {
return fmt.Errorf("error creating variable: %v", err)
}
}
}
// restore old masking config
viper.Set("mask", oldMasking)
}
return nil
}
func (c *Client) GetVariables(entity string, id int) (map[string]interface{}, error) {
resp, err := c.doRequest(http.MethodGet, fmt.Sprintf("%s/%d/variables", entity, id), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("Error getting project %d pipeline schedules. Return code not 2XX: %s", id, resp.Status)
}
r, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
variables := []map[string]interface{}{}
if err := json.Unmarshal(r, &variables); err != nil {
return nil, err
}
variableMap := make(map[string]interface{}, len(variables))
for _, v := range variables {
variableMap[v["key"].(string)] = v
}
return variableMap, nil
}