feature_flags.go 1.8 KB

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