1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 )
13
14
15 type Environment struct {
16 Owner *string `json:"owner,omitempty"`
17 Repo *string `json:"repo,omitempty"`
18 EnvironmentName *string `json:"environment_name,omitempty"`
19 WaitTimer *int `json:"wait_timer,omitempty"`
20 Reviewers []*EnvReviewers `json:"reviewers,omitempty"`
21 DeploymentBranchPolicy *BranchPolicy `json:"deployment_branch_policy,omitempty"`
22
23 ID *int64 `json:"id,omitempty"`
24 NodeID *string `json:"node_id,omitempty"`
25 Name *string `json:"name,omitempty"`
26 URL *string `json:"url,omitempty"`
27 HTMLURL *string `json:"html_url,omitempty"`
28 CreatedAt *Timestamp `json:"created_at,omitempty"`
29 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
30 ProtectionRules []*ProtectionRule `json:"protection_rules,omitempty"`
31 }
32
33
34 type EnvReviewers struct {
35 Type *string `json:"type,omitempty"`
36 ID *int64 `json:"id,omitempty"`
37 }
38
39
40 type BranchPolicy struct {
41 ProtectedBranches *bool `json:"protected_branches,omitempty"`
42 CustomBranchPolicies *bool `json:"custom_branch_policies,omitempty"`
43 }
44
45
46 type EnvResponse struct {
47 TotalCount *int `json:"total_count,omitempty"`
48 Environments []*Environment `json:"environments,omitempty"`
49 }
50
51
52 type ProtectionRule struct {
53 ID *int64 `json:"id,omitempty"`
54 NodeID *string `json:"node_id,omitempty"`
55 Type *string `json:"type,omitempty"`
56 WaitTimer *int `json:"wait_timer,omitempty"`
57 Reviewers []*RequiredReviewer `json:"reviewers,omitempty"`
58 }
59
60
61 type RequiredReviewer struct {
62 Type *string `json:"type,omitempty"`
63 Reviewer interface{} `json:"reviewer,omitempty"`
64 }
65
66
67
68 type EnvironmentListOptions struct {
69 ListOptions
70 }
71
72
73
74 func (r *RequiredReviewer) UnmarshalJSON(data []byte) error {
75 type aliasReviewer RequiredReviewer
76 var reviewer aliasReviewer
77 if err := json.Unmarshal(data, &reviewer); err != nil {
78 return err
79 }
80
81 r.Type = reviewer.Type
82
83 switch *reviewer.Type {
84 case "User":
85 reviewer.Reviewer = &User{}
86 if err := json.Unmarshal(data, &reviewer); err != nil {
87 return err
88 }
89 r.Reviewer = reviewer.Reviewer
90 case "Team":
91 reviewer.Reviewer = &Team{}
92 if err := json.Unmarshal(data, &reviewer); err != nil {
93 return err
94 }
95 r.Reviewer = reviewer.Reviewer
96 default:
97 r.Type = nil
98 r.Reviewer = nil
99 return fmt.Errorf("reviewer.Type is %T, not a string of 'User' or 'Team', unable to unmarshal", reviewer.Type)
100 }
101
102 return nil
103 }
104
105
106
107
108 func (s *RepositoriesService) ListEnvironments(ctx context.Context, owner, repo string, opts *EnvironmentListOptions) (*EnvResponse, *Response, error) {
109 u := fmt.Sprintf("repos/%s/%s/environments", owner, repo)
110 u, err := addOptions(u, opts)
111 if err != nil {
112 return nil, nil, err
113 }
114
115 req, err := s.client.NewRequest("GET", u, nil)
116 if err != nil {
117 return nil, nil, err
118 }
119
120 var list *EnvResponse
121 resp, err := s.client.Do(ctx, req, &list)
122 if err != nil {
123 return nil, resp, err
124 }
125 return list, resp, nil
126 }
127
128
129
130
131 func (s *RepositoriesService) GetEnvironment(ctx context.Context, owner, repo, name string) (*Environment, *Response, error) {
132 u := fmt.Sprintf("repos/%s/%s/environments/%s", owner, repo, name)
133
134 req, err := s.client.NewRequest("GET", u, nil)
135 if err != nil {
136 return nil, nil, err
137 }
138
139 var env *Environment
140 resp, err := s.client.Do(ctx, req, &env)
141 if err != nil {
142 return nil, resp, err
143 }
144 return env, resp, nil
145 }
146
147
148
149 func (c *CreateUpdateEnvironment) MarshalJSON() ([]byte, error) {
150 type Alias CreateUpdateEnvironment
151 if c.WaitTimer == nil {
152 c.WaitTimer = Int(0)
153 }
154 return json.Marshal(&struct {
155 *Alias
156 }{
157 Alias: (*Alias)(c),
158 })
159 }
160
161
162
163
164
165 type CreateUpdateEnvironment struct {
166 WaitTimer *int `json:"wait_timer"`
167 Reviewers []*EnvReviewers `json:"reviewers"`
168 DeploymentBranchPolicy *BranchPolicy `json:"deployment_branch_policy"`
169 }
170
171
172
173
174 func (s *RepositoriesService) CreateUpdateEnvironment(ctx context.Context, owner, repo, name string, environment *CreateUpdateEnvironment) (*Environment, *Response, error) {
175 u := fmt.Sprintf("repos/%s/%s/environments/%s", owner, repo, name)
176
177 req, err := s.client.NewRequest("PUT", u, environment)
178 if err != nil {
179 return nil, nil, err
180 }
181
182 e := new(Environment)
183 resp, err := s.client.Do(ctx, req, e)
184 if err != nil {
185 return nil, resp, err
186 }
187 return e, resp, nil
188 }
189
190
191
192
193 func (s *RepositoriesService) DeleteEnvironment(ctx context.Context, owner, repo, name string) (*Response, error) {
194 u := fmt.Sprintf("repos/%s/%s/environments/%s", owner, repo, name)
195
196 req, err := s.client.NewRequest("DELETE", u, nil)
197 if err != nil {
198 return nil, err
199 }
200 return s.client.Do(ctx, req, nil)
201 }
202
View as plain text