...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Patrick Webster
     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  // GroupVariablesService handles communication with the
    26  // group variables related methods of the GitLab API.
    27  //
    28  // GitLab API docs:
    29  // https://docs.gitlab.com/ee/api/group_level_variables.html
    30  type GroupVariablesService struct {
    31  	client *Client
    32  }
    33  
    34  // GroupVariable represents a GitLab group Variable.
    35  //
    36  // GitLab API docs:
    37  // https://docs.gitlab.com/ee/api/group_level_variables.html
    38  type GroupVariable struct {
    39  	Key              string            `json:"key"`
    40  	Value            string            `json:"value"`
    41  	VariableType     VariableTypeValue `json:"variable_type"`
    42  	Protected        bool              `json:"protected"`
    43  	Masked           bool              `json:"masked"`
    44  	Raw              bool              `json:"raw"`
    45  	EnvironmentScope string            `json:"environment_scope"`
    46  	Description      string            `json:"description"`
    47  }
    48  
    49  func (v GroupVariable) String() string {
    50  	return Stringify(v)
    51  }
    52  
    53  // ListGroupVariablesOptions represents the available options for listing variables
    54  // for a group.
    55  //
    56  // GitLab API docs:
    57  // https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
    58  type ListGroupVariablesOptions ListOptions
    59  
    60  // ListVariables gets a list of all variables for a group.
    61  //
    62  // GitLab API docs:
    63  // https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
    64  func (s *GroupVariablesService) ListVariables(gid interface{}, opt *ListGroupVariablesOptions, options ...RequestOptionFunc) ([]*GroupVariable, *Response, error) {
    65  	group, err := parseID(gid)
    66  	if err != nil {
    67  		return nil, nil, err
    68  	}
    69  	u := fmt.Sprintf("groups/%s/variables", PathEscape(group))
    70  
    71  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    72  	if err != nil {
    73  		return nil, nil, err
    74  	}
    75  
    76  	var vs []*GroupVariable
    77  	resp, err := s.client.Do(req, &vs)
    78  	if err != nil {
    79  		return nil, resp, err
    80  	}
    81  
    82  	return vs, resp, nil
    83  }
    84  
    85  // GetVariable gets a variable.
    86  //
    87  // GitLab API docs:
    88  // https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
    89  func (s *GroupVariablesService) GetVariable(gid interface{}, key string, options ...RequestOptionFunc) (*GroupVariable, *Response, error) {
    90  	group, err := parseID(gid)
    91  	if err != nil {
    92  		return nil, nil, err
    93  	}
    94  	u := fmt.Sprintf("groups/%s/variables/%s", PathEscape(group), url.PathEscape(key))
    95  
    96  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  
   101  	v := new(GroupVariable)
   102  	resp, err := s.client.Do(req, v)
   103  	if err != nil {
   104  		return nil, resp, err
   105  	}
   106  
   107  	return v, resp, nil
   108  }
   109  
   110  // CreateGroupVariableOptions represents the available CreateVariable()
   111  // options.
   112  //
   113  // GitLab API docs:
   114  // https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
   115  type CreateGroupVariableOptions struct {
   116  	Key              *string            `url:"key,omitempty" json:"key,omitempty"`
   117  	Value            *string            `url:"value,omitempty" json:"value,omitempty"`
   118  	Description      *string            `url:"description,omitempty" json:"description,omitempty"`
   119  	EnvironmentScope *string            `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
   120  	Masked           *bool              `url:"masked,omitempty" json:"masked,omitempty"`
   121  	Protected        *bool              `url:"protected,omitempty" json:"protected,omitempty"`
   122  	Raw              *bool              `url:"raw,omitempty" json:"raw,omitempty"`
   123  	VariableType     *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   124  }
   125  
   126  // CreateVariable creates a new group variable.
   127  //
   128  // GitLab API docs:
   129  // https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
   130  func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateGroupVariableOptions, options ...RequestOptionFunc) (*GroupVariable, *Response, error) {
   131  	group, err := parseID(gid)
   132  	if err != nil {
   133  		return nil, nil, err
   134  	}
   135  	u := fmt.Sprintf("groups/%s/variables", PathEscape(group))
   136  
   137  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	v := new(GroupVariable)
   143  	resp, err := s.client.Do(req, v)
   144  	if err != nil {
   145  		return nil, resp, err
   146  	}
   147  
   148  	return v, resp, nil
   149  }
   150  
   151  // UpdateGroupVariableOptions represents the available UpdateVariable()
   152  // options.
   153  //
   154  // GitLab API docs:
   155  // https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
   156  type UpdateGroupVariableOptions struct {
   157  	Value            *string            `url:"value,omitempty" json:"value,omitempty"`
   158  	Description      *string            `url:"description,omitempty" json:"description,omitempty"`
   159  	EnvironmentScope *string            `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
   160  	Masked           *bool              `url:"masked,omitempty" json:"masked,omitempty"`
   161  	Protected        *bool              `url:"protected,omitempty" json:"protected,omitempty"`
   162  	Raw              *bool              `url:"raw,omitempty" json:"raw,omitempty"`
   163  	VariableType     *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   164  }
   165  
   166  // UpdateVariable updates the position of an existing
   167  // group issue board list.
   168  //
   169  // GitLab API docs:
   170  // https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
   171  func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateGroupVariableOptions, options ...RequestOptionFunc) (*GroupVariable, *Response, error) {
   172  	group, err := parseID(gid)
   173  	if err != nil {
   174  		return nil, nil, err
   175  	}
   176  	u := fmt.Sprintf("groups/%s/variables/%s", PathEscape(group), url.PathEscape(key))
   177  
   178  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   179  	if err != nil {
   180  		return nil, nil, err
   181  	}
   182  
   183  	v := new(GroupVariable)
   184  	resp, err := s.client.Do(req, v)
   185  	if err != nil {
   186  		return nil, resp, err
   187  	}
   188  
   189  	return v, resp, nil
   190  }
   191  
   192  // RemoveVariable removes a group's variable.
   193  //
   194  // GitLab API docs:
   195  // https://docs.gitlab.com/ee/api/group_level_variables.html#remove-variable
   196  func (s *GroupVariablesService) RemoveVariable(gid interface{}, key string, options ...RequestOptionFunc) (*Response, error) {
   197  	group, err := parseID(gid)
   198  	if err != nil {
   199  		return nil, err
   200  	}
   201  	u := fmt.Sprintf("groups/%s/variables/%s", PathEscape(group), url.PathEscape(key))
   202  
   203  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  
   208  	return s.client.Do(req, nil)
   209  }
   210  

View as plain text