...

Source file src/github.com/xanzy/go-gitlab/project_feature_flags.go

Documentation: github.com/xanzy/go-gitlab

     1  package gitlab
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  // ProjectFeatureFlagService handles operations on gitlab project feature
    10  // flags using the following api:
    11  //
    12  // GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
    13  type ProjectFeatureFlagService struct {
    14  	client *Client
    15  }
    16  
    17  // ProjectFeatureFlag represents a GitLab project iteration.
    18  //
    19  // GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
    20  type ProjectFeatureFlag struct {
    21  	Name        string                        `json:"name"`
    22  	Description string                        `json:"description"`
    23  	Active      bool                          `json:"active"`
    24  	Version     string                        `json:"version"`
    25  	CreatedAt   *time.Time                    `json:"created_at"`
    26  	UpdatedAt   *time.Time                    `json:"updated_at"`
    27  	Scopes      []*ProjectFeatureFlagScope    `json:"scopes"`
    28  	Strategies  []*ProjectFeatureFlagStrategy `json:"strategies"`
    29  }
    30  
    31  // ProjectFeatureFlagScope defines the scopes of a feature flag
    32  //
    33  // GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
    34  type ProjectFeatureFlagScope struct {
    35  	ID               int    `json:"id"`
    36  	EnvironmentScope string `json:"environment_scope"`
    37  }
    38  
    39  // ProjectFeatureFlagStrategy defines the strategy used for a feature flag
    40  //
    41  // GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
    42  type ProjectFeatureFlagStrategy struct {
    43  	ID         int                                  `json:"id"`
    44  	Name       string                               `json:"name"`
    45  	Parameters *ProjectFeatureFlagStrategyParameter `json:"parameters"`
    46  	Scopes     []*ProjectFeatureFlagScope           `json:"scopes"`
    47  }
    48  
    49  // ProjectFeatureFlagStrategyParameter is used in updating and creating feature flags
    50  //
    51  // GitLab API docs: https://docs.gitlab.com/ee/api/feature_flags.html
    52  type ProjectFeatureFlagStrategyParameter struct {
    53  	GroupID    string `json:"groupId,omitempty"`
    54  	UserIDs    string `json:"userIds,omitempty"`
    55  	Percentage string `json:"percentage,omitempty"`
    56  
    57  	// Following fields aren't documented in Gitlab API docs,
    58  	// but are present in Gitlab API since 13.5.
    59  	// Docs: https://docs.getunleash.io/reference/activation-strategies#gradual-rollout
    60  	Rollout    string `json:"rollout,omitempty"`
    61  	Stickiness string `json:"stickiness,omitempty"`
    62  }
    63  
    64  func (i ProjectFeatureFlag) String() string {
    65  	return Stringify(i)
    66  }
    67  
    68  // ListProjectFeatureFlagOptions contains the options for ListProjectFeatureFlags
    69  //
    70  // GitLab API docs:
    71  // https://docs.gitlab.com/ee/api/feature_flags.html#list-feature-flags-for-a-project
    72  type ListProjectFeatureFlagOptions struct {
    73  	ListOptions
    74  	Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
    75  }
    76  
    77  // ListProjectFeatureFlags returns a list with the feature flags of a project.
    78  //
    79  // GitLab API docs:
    80  // https://docs.gitlab.com/ee/api/feature_flags.html#list-feature-flags-for-a-project
    81  func (s *ProjectFeatureFlagService) ListProjectFeatureFlags(pid interface{}, opt *ListProjectFeatureFlagOptions, options ...RequestOptionFunc) ([]*ProjectFeatureFlag, *Response, error) {
    82  	project, err := parseID(pid)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  	u := fmt.Sprintf("projects/%s/feature_flags", PathEscape(project))
    87  
    88  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  
    93  	var pffs []*ProjectFeatureFlag
    94  	resp, err := s.client.Do(req, &pffs)
    95  	if err != nil {
    96  		return nil, resp, err
    97  	}
    98  
    99  	return pffs, resp, nil
   100  }
   101  
   102  // GetProjectFeatureFlag gets a single feature flag for the specified project.
   103  //
   104  // GitLab API docs:
   105  // https://docs.gitlab.com/ee/api/feature_flags.html#get-a-single-feature-flag
   106  func (s *ProjectFeatureFlagService) GetProjectFeatureFlag(pid interface{}, name string, options ...RequestOptionFunc) (*ProjectFeatureFlag, *Response, error) {
   107  	project, err := parseID(pid)
   108  	if err != nil {
   109  		return nil, nil, err
   110  	}
   111  	u := fmt.Sprintf("projects/%s/feature_flags/%s", PathEscape(project), name)
   112  
   113  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   114  	if err != nil {
   115  		return nil, nil, err
   116  	}
   117  
   118  	flag := new(ProjectFeatureFlag)
   119  	resp, err := s.client.Do(req, flag)
   120  	if err != nil {
   121  		return nil, resp, err
   122  	}
   123  
   124  	return flag, resp, nil
   125  }
   126  
   127  // CreateProjectFeatureFlagOptions represents the available
   128  // CreateProjectFeatureFlag() options.
   129  //
   130  // Gitlab API docs:
   131  // https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
   132  type CreateProjectFeatureFlagOptions struct {
   133  	Name        *string                        `url:"name,omitempty" json:"name,omitempty"`
   134  	Description *string                        `url:"description,omitempty" json:"description,omitempty"`
   135  	Version     *string                        `url:"version,omitempty" json:"version,omitempty"`
   136  	Active      *bool                          `url:"active,omitempty" json:"active,omitempty"`
   137  	Strategies  *[]*FeatureFlagStrategyOptions `url:"strategies,omitempty" json:"strategies,omitempty"`
   138  }
   139  
   140  // FeatureFlagStrategyOptions represents the available feature flag strategy
   141  // options.
   142  //
   143  // Gitlab API docs:
   144  // https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
   145  type FeatureFlagStrategyOptions struct {
   146  	ID         *int                                 `url:"id,omitempty" json:"id,omitempty"`
   147  	Name       *string                              `url:"name,omitempty" json:"name,omitempty"`
   148  	Parameters *ProjectFeatureFlagStrategyParameter `url:"parameters,omitempty" json:"parameters,omitempty"`
   149  	Scopes     *[]*ProjectFeatureFlagScope          `url:"scopes,omitempty" json:"scopes,omitempty"`
   150  }
   151  
   152  // ProjectFeatureFlagScopeOptions represents the available feature flag scope
   153  // options.
   154  //
   155  // Gitlab API docs:
   156  // https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
   157  type ProjectFeatureFlagScopeOptions struct {
   158  	ID               *int    `url:"id,omitempty" json:"id,omitempty"`
   159  	EnvironmentScope *string `url:"id,omitempty" json:"environment_scope,omitempty"`
   160  }
   161  
   162  // CreateProjectFeatureFlag creates a feature flag
   163  //
   164  // Gitlab API docs:
   165  // https://docs.gitlab.com/ee/api/feature_flags.html#create-a-feature-flag
   166  func (s *ProjectFeatureFlagService) CreateProjectFeatureFlag(pid interface{}, opt *CreateProjectFeatureFlagOptions, options ...RequestOptionFunc) (*ProjectFeatureFlag, *Response, error) {
   167  	project, err := parseID(pid)
   168  	if err != nil {
   169  		return nil, nil, err
   170  	}
   171  	u := fmt.Sprintf("projects/%s/feature_flags",
   172  		PathEscape(project),
   173  	)
   174  
   175  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   176  	if err != nil {
   177  		return nil, nil, err
   178  	}
   179  
   180  	flag := new(ProjectFeatureFlag)
   181  	resp, err := s.client.Do(req, flag)
   182  	if err != nil {
   183  		return flag, resp, err
   184  	}
   185  
   186  	return flag, resp, nil
   187  }
   188  
   189  // UpdateProjectFeatureFlagOptions represents the available
   190  // UpdateProjectFeatureFlag() options.
   191  //
   192  // Gitlab API docs:
   193  // https://docs.gitlab.com/ee/api/feature_flags.html#update-a-feature-flag
   194  type UpdateProjectFeatureFlagOptions struct {
   195  	Name        *string                        `url:"name,omitempty" json:"name,omitempty"`
   196  	Description *string                        `url:"description,omitempty" json:"description,omitempty"`
   197  	Active      *bool                          `url:"active,omitempty" json:"active,omitempty"`
   198  	Strategies  *[]*FeatureFlagStrategyOptions `url:"strategies,omitempty" json:"strategies,omitempty"`
   199  }
   200  
   201  // UpdateProjectFeatureFlag updates a feature flag
   202  //
   203  // Gitlab API docs:
   204  // https://docs.gitlab.com/ee/api/feature_flags.html#update-a-feature-flag
   205  func (s *ProjectFeatureFlagService) UpdateProjectFeatureFlag(pid interface{}, name string, opt *UpdateProjectFeatureFlagOptions, options ...RequestOptionFunc) (*ProjectFeatureFlag, *Response, error) {
   206  	group, err := parseID(pid)
   207  	if err != nil {
   208  		return nil, nil, err
   209  	}
   210  	u := fmt.Sprintf("projects/%s/feature_flags/%s",
   211  		PathEscape(group),
   212  		name,
   213  	)
   214  
   215  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   216  	if err != nil {
   217  		return nil, nil, err
   218  	}
   219  
   220  	flag := new(ProjectFeatureFlag)
   221  	resp, err := s.client.Do(req, flag)
   222  	if err != nil {
   223  		return flag, resp, err
   224  	}
   225  
   226  	return flag, resp, nil
   227  }
   228  
   229  // DeleteProjectFeatureFlag deletes a feature flag
   230  //
   231  // Gitlab API docs:
   232  // https://docs.gitlab.com/ee/api/feature_flags.html#delete-a-feature-flag
   233  func (s *ProjectFeatureFlagService) DeleteProjectFeatureFlag(pid interface{}, name string, options ...RequestOptionFunc) (*Response, error) {
   234  	project, err := parseID(pid)
   235  	if err != nil {
   236  		return nil, err
   237  	}
   238  	u := fmt.Sprintf("projects/%s/feature_flags/%s", PathEscape(project), name)
   239  
   240  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  
   245  	return s.client.Do(req, nil)
   246  }
   247  

View as plain text