...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"net/url"
    23  )
    24  
    25  // FeaturesService handles the communication with the application FeaturesService
    26  // related methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/features.html
    29  type FeaturesService struct {
    30  	client *Client
    31  }
    32  
    33  // Feature represents a GitLab feature flag.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/features.html
    36  type Feature struct {
    37  	Name  string `json:"name"`
    38  	State string `json:"state"`
    39  	Gates []Gate
    40  }
    41  
    42  // Gate represents a gate of a GitLab feature flag.
    43  //
    44  // GitLab API docs: https://docs.gitlab.com/ee/api/features.html
    45  type Gate struct {
    46  	Key   string      `json:"key"`
    47  	Value interface{} `json:"value"`
    48  }
    49  
    50  func (f Feature) String() string {
    51  	return Stringify(f)
    52  }
    53  
    54  // ListFeatures gets a list of feature flags
    55  //
    56  // GitLab API docs:
    57  // https://docs.gitlab.com/ee/api/features.html#list-all-features
    58  func (s *FeaturesService) ListFeatures(options ...RequestOptionFunc) ([]*Feature, *Response, error) {
    59  	req, err := s.client.NewRequest(http.MethodGet, "features", nil, options)
    60  	if err != nil {
    61  		return nil, nil, err
    62  	}
    63  
    64  	var f []*Feature
    65  	resp, err := s.client.Do(req, &f)
    66  	if err != nil {
    67  		return nil, resp, err
    68  	}
    69  	return f, resp, nil
    70  }
    71  
    72  // SetFeatureFlag sets or creates a feature flag gate
    73  //
    74  // GitLab API docs:
    75  // https://docs.gitlab.com/ee/api/features.html#set-or-create-a-feature
    76  func (s *FeaturesService) SetFeatureFlag(name string, value interface{}, options ...RequestOptionFunc) (*Feature, *Response, error) {
    77  	u := fmt.Sprintf("features/%s", url.PathEscape(name))
    78  
    79  	opt := struct {
    80  		Value interface{} `url:"value" json:"value"`
    81  	}{
    82  		value,
    83  	}
    84  
    85  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  
    90  	f := &Feature{}
    91  	resp, err := s.client.Do(req, f)
    92  	if err != nil {
    93  		return nil, resp, err
    94  	}
    95  	return f, resp, nil
    96  }
    97  

View as plain text