feature_flags.go

 1package gitlab
 2
 3import (
 4	"fmt"
 5	"net/url"
 6)
 7
 8// FeaturesService handles the communication with the application FeaturesService
 9// related methods of the GitLab API.
10//
11// GitLab API docs: https://docs.gitlab.com/ce/api/features.html
12type FeaturesService struct {
13	client *Client
14}
15
16// Feature represents a GitLab feature flag.
17//
18// GitLab API docs: https://docs.gitlab.com/ce/api/features.html
19type Feature struct {
20	Name  string `json:"name"`
21	State string `json:"state"`
22	Gates []Gate
23}
24
25// Gate represents a gate of a GitLab feature flag.
26//
27// GitLab API docs: https://docs.gitlab.com/ce/api/features.html
28type Gate struct {
29	Key   string      `json:"key"`
30	Value interface{} `json:"value"`
31}
32
33func (f Feature) String() string {
34	return Stringify(f)
35}
36
37// ListFeatures gets a list of feature flags
38//
39// GitLab API docs:
40// https://docs.gitlab.com/ce/api/features.html#list-all-features
41func (s *FeaturesService) ListFeatures(options ...OptionFunc) ([]*Feature, *Response, error) {
42	req, err := s.client.NewRequest("GET", "features", nil, options)
43	if err != nil {
44		return nil, nil, err
45	}
46
47	var f []*Feature
48	resp, err := s.client.Do(req, &f)
49	if err != nil {
50		return nil, resp, err
51	}
52	return f, resp, err
53}
54
55// SetFeatureFlag sets or creates a feature flag gate
56//
57// GitLab API docs:
58// https://docs.gitlab.com/ce/api/features.html#set-or-create-a-feature
59func (s *FeaturesService) SetFeatureFlag(name string, value interface{}, options ...OptionFunc) (*Feature, *Response, error) {
60	u := fmt.Sprintf("features/%s", url.PathEscape(name))
61
62	opt := struct {
63		Value interface{} `url:"value" json:"value"`
64	}{
65		value,
66	}
67
68	req, err := s.client.NewRequest("POST", u, opt, options)
69	if err != nil {
70		return nil, nil, err
71	}
72
73	f := &Feature{}
74	resp, err := s.client.Do(req, f)
75	if err != nil {
76		return nil, resp, err
77	}
78	return f, resp, err
79}