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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
package gitlab
import (
"fmt"
"net/http"
"time"
)
// ProjectFeatureFlagService handles operations on gitlab project feature
// flags using the following api:
//
// GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
type ProjectFeatureFlagService struct {
client *Client
}
// ProjectFeatureFlag represents a GitLab project iteration.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
type ProjectFeatureFlag struct {
Name string `json:"name"`
Description string `json:"description"`
Active bool `json:"active"`
Version string `json:"version"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
Scopes []*ProjectFeatureFlagScope `json:"scopes"`
Strategies []*ProjectFeatureFlagStrategy `json:"strategies"`
}
// ProjectFeatureFlagScope defines the scopes of a feature flag
//
// GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
type ProjectFeatureFlagScope struct {
ID int `json:"id"`
EnvironmentScope string `json:"environment_scope"`
}
// ProjectFeatureFlagStrategy defines the strategy used for a feature flag
//
// GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
type ProjectFeatureFlagStrategy struct {
ID int `json:"id"`
Name string `json:"name"`
Parameters *ProjectFeatureFlagStrategyParameter `json:"parameters"`
Scopes []*ProjectFeatureFlagScope `json:"scopes"`
}
// ProjectFeatureFlagStrategyParameter is used in updating and creating feature flags
//
// GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
type ProjectFeatureFlagStrategyParameter struct {
GroupID string `json:"groupId,omitempty"`
UserIDs string `json:"userIds,omitempty"`
Percentage string `json:"percentage,omitempty"`
// Following fields aren't documented in Gitlab API docs,
// but are present in Gitlab API since 13.5.
// Docs: https://docs.getunleash.io/reference/activation-strategies#gradual-rollout
Rollout string `json:"rollout,omitempty"`
Stickiness string `json:"stickiness,omitempty"`
}
func (i ProjectFeatureFlag) String() string {
return Stringify(i)
}
// ListProjectFeatureFlagOptions contains the options for ListProjectFeatureFlags
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#list-feature-flags-for-a-project
type ListProjectFeatureFlagOptions struct {
ListOptions
Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
}
// ListProjectFeatureFlags returns a list with the feature flags of a project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#list-feature-flags-for-a-project
func (s *ProjectFeatureFlagService) ListProjectFeatureFlags(pid interface{}, opt *ListProjectFeatureFlagOptions, options ...RequestOptionFunc) ([]*ProjectFeatureFlag, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/feature_flags", PathEscape(project))
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
var pffs []*ProjectFeatureFlag
resp, err := s.client.Do(req, &pffs)
if err != nil {
return nil, resp, err
}
return pffs, resp, nil
}
// GetProjectFeatureFlag gets a single feature flag for the specified project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#get-a-single-feature-flag
func (s *ProjectFeatureFlagService) GetProjectFeatureFlag(pid interface{}, name string, options ...RequestOptionFunc) (*ProjectFeatureFlag, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/feature_flags/%s", PathEscape(project), name)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
flag := new(ProjectFeatureFlag)
resp, err := s.client.Do(req, flag)
if err != nil {
return nil, resp, err
}
return flag, resp, nil
}
// CreateProjectFeatureFlagOptions represents the available
// CreateProjectFeatureFlag() options.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
type CreateProjectFeatureFlagOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Version *string `url:"version,omitempty" json:"version,omitempty"`
Active *bool `url:"active,omitempty" json:"active,omitempty"`
Strategies *[]*FeatureFlagStrategyOptions `url:"strategies,omitempty" json:"strategies,omitempty"`
}
// FeatureFlagStrategyOptions represents the available feature flag strategy
// options.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
type FeatureFlagStrategyOptions struct {
ID *int `url:"id,omitempty" json:"id,omitempty"`
Name *string `url:"name,omitempty" json:"name,omitempty"`
Parameters *ProjectFeatureFlagStrategyParameter `url:"parameters,omitempty" json:"parameters,omitempty"`
Scopes *[]*ProjectFeatureFlagScope `url:"scopes,omitempty" json:"scopes,omitempty"`
}
// ProjectFeatureFlagScopeOptions represents the available feature flag scope
// options.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
type ProjectFeatureFlagScopeOptions struct {
ID *int `url:"id,omitempty" json:"id,omitempty"`
EnvironmentScope *string `url:"id,omitempty" json:"environment_scope,omitempty"`
}
// CreateProjectFeatureFlag creates a feature flag
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
func (s *ProjectFeatureFlagService) CreateProjectFeatureFlag(pid interface{}, opt *CreateProjectFeatureFlagOptions, options ...RequestOptionFunc) (*ProjectFeatureFlag, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/feature_flags",
PathEscape(project),
)
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}
flag := new(ProjectFeatureFlag)
resp, err := s.client.Do(req, flag)
if err != nil {
return flag, resp, err
}
return flag, resp, nil
}
// UpdateProjectFeatureFlagOptions represents the available
// UpdateProjectFeatureFlag() options.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#update-a-feature-flag
type UpdateProjectFeatureFlagOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Active *bool `url:"active,omitempty" json:"active,omitempty"`
Strategies *[]*FeatureFlagStrategyOptions `url:"strategies,omitempty" json:"strategies,omitempty"`
}
// UpdateProjectFeatureFlag updates a feature flag
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#update-a-feature-flag
func (s *ProjectFeatureFlagService) UpdateProjectFeatureFlag(pid interface{}, name string, opt *UpdateProjectFeatureFlagOptions, options ...RequestOptionFunc) (*ProjectFeatureFlag, *Response, error) {
group, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/feature_flags/%s",
PathEscape(group),
name,
)
req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
if err != nil {
return nil, nil, err
}
flag := new(ProjectFeatureFlag)
resp, err := s.client.Do(req, flag)
if err != nil {
return flag, resp, err
}
return flag, resp, nil
}
// DeleteProjectFeatureFlag deletes a feature flag
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/feature_flags.html#delete-a-feature-flag
func (s *ProjectFeatureFlagService) DeleteProjectFeatureFlag(pid interface{}, name string, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/feature_flags/%s", PathEscape(project), name)
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
|