forked from plouc/go-gitlab-client
/
environments.go
106 lines (85 loc) · 3.63 KB
/
environments.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
package gitlab
import (
"encoding/json"
"io"
"strconv"
)
const (
ProjectEnvironmentsApiPath = "/projects/:id/environments"
ProjectEnvironmentApiPath = "/projects/:id/environments/:environment_id"
)
type EnvironmentAddPayload struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
ExternalUrl string `json:"external_url,omitempty" yaml:"external_url,omitempty"`
}
type Environment struct {
Id int `json:"id,omitempty" yaml:"id,omitempty"`
EnvironmentAddPayload `yaml:",inline"`
Slug string `json:"slug,omitempty" yaml:"slug,omitempty"`
Project *EnvironmentProject `json:"project,omitempty" yaml:"project,omitempty"`
}
type EnvironmentProject struct {
Id int `json:"id,omitempty" yaml:"id,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
NameWithNamespace string `json:"name_with_namespace,omitempty" yaml:"name_with_namespace,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
PathWithNamespace string `json:"path_with_namespace,omitempty" yaml:"path_with_namespace,omitempty"`
Description string `json:"description" yaml:"description"`
CreatedAtRaw string `json:"created_at,omitempty" yaml:"created_at,omitempty"`
DefaultBranch string `json:"default_branch" yaml:"default_branch"`
TagList []string `json:"tag_list" yaml:"tag_list"`
SshUrlToRepo string `json:"ssh_url_to_repo" yaml:"ssh_url_to_repo"`
HttpUrlToRepo string `json:"http_url_to_repo" yaml:"http_url_to_repo"`
WebUrl string `json:"web_url" yaml:"web_url"`
AvatarUrl string `json:"avatar_url" yaml:"avatar_url"`
ForksCount int `json:"forks_count" yaml:"forks_count"`
StarCount int `json:"star_count" yaml:"star_count"`
LastActivityAtRaw string `json:"last_activity_at,omitempty" yaml:"last_activity_at,omitempty"`
}
type EnvironmentCollection struct {
Items []*Environment
}
func (e *Environment) RenderJson(w io.Writer) error {
return renderJson(w, e)
}
func (e *Environment) RenderYaml(w io.Writer) error {
return renderYaml(w, e)
}
func (c *EnvironmentCollection) RenderJson(w io.Writer) error {
return renderJson(w, c.Items)
}
func (c *EnvironmentCollection) RenderYaml(w io.Writer) error {
return renderYaml(w, c.Items)
}
func (g *Gitlab) ProjectEnvironments(projectId string, o *PaginationOptions) (*EnvironmentCollection, *ResponseMeta, error) {
u := g.ResourceUrlQ(ProjectEnvironmentsApiPath, map[string]string{
":id": projectId,
}, o)
collection := new(EnvironmentCollection)
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) AddProjectEnvironment(projectId string, environment *EnvironmentAddPayload) (*Environment, *ResponseMeta, error) {
u := g.ResourceUrl(ProjectEnvironmentsApiPath, map[string]string{":id": projectId})
environmentJson, err := json.Marshal(environment)
if err != nil {
return nil, nil, err
}
var createdEnvironment *Environment
contents, meta, err := g.buildAndExecRequest("POST", u.String(), environmentJson)
if err == nil {
err = json.Unmarshal(contents, &createdEnvironment)
}
return createdEnvironment, meta, err
}
func (g *Gitlab) RemoveProjectEnvironment(projectId string, id int) (*ResponseMeta, error) {
u := g.ResourceUrl(ProjectEnvironmentApiPath, map[string]string{
":id": projectId,
":environment_id": strconv.Itoa(id),
})
_, meta, err := g.buildAndExecRequest("DELETE", u.String(), nil)
return meta, err
}