forked from plouc/go-gitlab-client
/
variables.go
135 lines (104 loc) · 3.62 KB
/
variables.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
133
134
135
package gitlab
import (
"encoding/json"
"io"
)
const (
VariablesApiPath = "/:type/:id/variables"
VariableApiPath = "/:type/:id/variables/:key"
)
type Variable struct {
Key string `json:"key"`
Value string `json:"value"`
Protected bool `json:"protected"`
EnvironmentScope string `json:"environment_scope,omitempty"`
}
type VariableCollection struct {
Items []*Variable
}
func (v *Variable) RenderJson(w io.Writer) error {
return renderJson(w, v)
}
func (v *Variable) RenderYaml(w io.Writer) error {
return renderYaml(w, v)
}
func (c *VariableCollection) RenderJson(w io.Writer) error {
return renderJson(w, c.Items)
}
func (c *VariableCollection) RenderYaml(w io.Writer) error {
return renderYaml(w, c.Items)
}
func (g *Gitlab) getVariables(resourceType, id string, o *PaginationOptions) (*VariableCollection, *ResponseMeta, error) {
u := g.ResourceUrlQ(VariablesApiPath, map[string]string{
":type": resourceType,
":id": id,
}, o)
collection := new(VariableCollection)
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &collection.Items)
}
return collection, meta, err
}
func (g *Gitlab) ProjectVariables(projectId string, o *PaginationOptions) (*VariableCollection, *ResponseMeta, error) {
return g.getVariables("projects", projectId, o)
}
func (g *Gitlab) GroupVariables(groupId string, o *PaginationOptions) (*VariableCollection, *ResponseMeta, error) {
return g.getVariables("groups", groupId, o)
}
func (g *Gitlab) getVariable(resourceType, projectId, varKey string) (*Variable, *ResponseMeta, error) {
u := g.ResourceUrl(VariableApiPath, map[string]string{
":type": "projects",
":id": projectId,
":key": varKey,
})
variable := new(Variable)
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &variable)
}
return variable, meta, err
}
func (g *Gitlab) ProjectVariable(projectId, varKey string) (*Variable, *ResponseMeta, error) {
return g.getVariable("projects", projectId, varKey)
}
func (g *Gitlab) GroupVariable(groupId, varKey string) (*Variable, *ResponseMeta, error) {
return g.getVariable("groups", groupId, varKey)
}
func (g *Gitlab) addVariable(resourceType, id string, variable *Variable) (*Variable, *ResponseMeta, error) {
u := g.ResourceUrl(VariablesApiPath, map[string]string{
":type": resourceType,
":id": id,
})
variableJson, err := json.Marshal(variable)
if err != nil {
return nil, nil, err
}
var createdVariable *Variable
contents, meta, err := g.buildAndExecRequest("POST", u.String(), variableJson)
if err == nil {
err = json.Unmarshal(contents, &createdVariable)
}
return createdVariable, meta, err
}
func (g *Gitlab) AddProjectVariable(projectId string, variable *Variable) (*Variable, *ResponseMeta, error) {
return g.addVariable("projects", projectId, variable)
}
func (g *Gitlab) AddGroupVariable(groupId string, variable *Variable) (*Variable, *ResponseMeta, error) {
return g.addVariable("groups", groupId, variable)
}
func (g *Gitlab) removeVariable(resourceType, id, varKey string) (*ResponseMeta, error) {
u := g.ResourceUrl(VariableApiPath, map[string]string{
":type": resourceType,
":id": id,
":key": varKey,
})
_, meta, err := g.buildAndExecRequest("DELETE", u.String(), nil)
return meta, err
}
func (g *Gitlab) RemoveProjectVariable(projectId, varKey string) (*ResponseMeta, error) {
return g.removeVariable("projects", projectId, varKey)
}
func (g *Gitlab) RemoveGroupVariable(groupId, varKey string) (*ResponseMeta, error) {
return g.removeVariable("groups", groupId, varKey)
}