...

Source file src/github.com/xanzy/go-gitlab/project_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  // ProjectVariablesService handles communication with the
    26  // project variables related methods of the GitLab API.
    27  //
    28  // GitLab API docs:
    29  // https://docs.gitlab.com/ee/api/project_level_variables.html
    30  type ProjectVariablesService struct {
    31  	client *Client
    32  }
    33  
    34  // ProjectVariable represents a GitLab Project Variable.
    35  //
    36  // GitLab API docs:
    37  // https://docs.gitlab.com/ee/api/project_level_variables.html
    38  type ProjectVariable 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 ProjectVariable) String() string {
    50  	return Stringify(v)
    51  }
    52  
    53  // VariableFilter filters available for project variable related functions
    54  type VariableFilter struct {
    55  	EnvironmentScope string `url:"environment_scope, omitempty" json:"environment_scope,omitempty"`
    56  }
    57  
    58  // ListProjectVariablesOptions represents the available options for listing variables
    59  // in a project.
    60  //
    61  // GitLab API docs:
    62  // https://docs.gitlab.com/ee/api/project_level_variables.html#list-project-variables
    63  type ListProjectVariablesOptions ListOptions
    64  
    65  // ListVariables gets a list of all variables in a project.
    66  //
    67  // GitLab API docs:
    68  // https://docs.gitlab.com/ee/api/project_level_variables.html#list-project-variables
    69  func (s *ProjectVariablesService) ListVariables(pid interface{}, opt *ListProjectVariablesOptions, options ...RequestOptionFunc) ([]*ProjectVariable, *Response, error) {
    70  	project, err := parseID(pid)
    71  	if err != nil {
    72  		return nil, nil, err
    73  	}
    74  	u := fmt.Sprintf("projects/%s/variables", PathEscape(project))
    75  
    76  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  
    81  	var vs []*ProjectVariable
    82  	resp, err := s.client.Do(req, &vs)
    83  	if err != nil {
    84  		return nil, resp, err
    85  	}
    86  
    87  	return vs, resp, nil
    88  }
    89  
    90  // GetProjectVariableOptions represents the available GetVariable()
    91  // options.
    92  //
    93  // GitLab API docs:
    94  // https://docs.gitlab.com/ee/api/project_level_variables.html#get-a-single-variable
    95  type GetProjectVariableOptions struct {
    96  	Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
    97  }
    98  
    99  // GetVariable gets a variable.
   100  //
   101  // GitLab API docs:
   102  // https://docs.gitlab.com/ee/api/project_level_variables.html#get-a-single-variable
   103  func (s *ProjectVariablesService) GetVariable(pid interface{}, key string, opt *GetProjectVariableOptions, options ...RequestOptionFunc) (*ProjectVariable, *Response, error) {
   104  	project, err := parseID(pid)
   105  	if err != nil {
   106  		return nil, nil, err
   107  	}
   108  	u := fmt.Sprintf("projects/%s/variables/%s", PathEscape(project), url.PathEscape(key))
   109  
   110  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   111  	if err != nil {
   112  		return nil, nil, err
   113  	}
   114  
   115  	v := new(ProjectVariable)
   116  	resp, err := s.client.Do(req, v)
   117  	if err != nil {
   118  		return nil, resp, err
   119  	}
   120  
   121  	return v, resp, nil
   122  }
   123  
   124  // CreateProjectVariableOptions represents the available CreateVariable()
   125  // options.
   126  //
   127  // GitLab API docs:
   128  // https://docs.gitlab.com/ee/api/project_level_variables.html#create-a-variable
   129  type CreateProjectVariableOptions struct {
   130  	Key              *string            `url:"key,omitempty" json:"key,omitempty"`
   131  	Value            *string            `url:"value,omitempty" json:"value,omitempty"`
   132  	Description      *string            `url:"description,omitempty" json:"description,omitempty"`
   133  	EnvironmentScope *string            `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
   134  	Masked           *bool              `url:"masked,omitempty" json:"masked,omitempty"`
   135  	Protected        *bool              `url:"protected,omitempty" json:"protected,omitempty"`
   136  	Raw              *bool              `url:"raw,omitempty" json:"raw,omitempty"`
   137  	VariableType     *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   138  }
   139  
   140  // CreateVariable creates a new project variable.
   141  //
   142  // GitLab API docs:
   143  // https://docs.gitlab.com/ee/api/project_level_variables.html#create-a-variable
   144  func (s *ProjectVariablesService) CreateVariable(pid interface{}, opt *CreateProjectVariableOptions, options ...RequestOptionFunc) (*ProjectVariable, *Response, error) {
   145  	project, err := parseID(pid)
   146  	if err != nil {
   147  		return nil, nil, err
   148  	}
   149  	u := fmt.Sprintf("projects/%s/variables", PathEscape(project))
   150  
   151  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   152  	if err != nil {
   153  		return nil, nil, err
   154  	}
   155  
   156  	v := new(ProjectVariable)
   157  	resp, err := s.client.Do(req, v)
   158  	if err != nil {
   159  		return nil, resp, err
   160  	}
   161  
   162  	return v, resp, nil
   163  }
   164  
   165  // UpdateProjectVariableOptions represents the available UpdateVariable()
   166  // options.
   167  //
   168  // GitLab API docs:
   169  // https://docs.gitlab.com/ee/api/project_level_variables.html#update-a-variable
   170  type UpdateProjectVariableOptions struct {
   171  	Value            *string            `url:"value,omitempty" json:"value,omitempty"`
   172  	Description      *string            `url:"description,omitempty" json:"description,omitempty"`
   173  	EnvironmentScope *string            `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
   174  	Filter           *VariableFilter    `url:"filter,omitempty" json:"filter,omitempty"`
   175  	Masked           *bool              `url:"masked,omitempty" json:"masked,omitempty"`
   176  	Protected        *bool              `url:"protected,omitempty" json:"protected,omitempty"`
   177  	Raw              *bool              `url:"raw,omitempty" json:"raw,omitempty"`
   178  	VariableType     *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   179  }
   180  
   181  // UpdateVariable updates a project's variable.
   182  //
   183  // GitLab API docs:
   184  // https://docs.gitlab.com/ee/api/project_level_variables.html#update-a-variable
   185  func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, opt *UpdateProjectVariableOptions, options ...RequestOptionFunc) (*ProjectVariable, *Response, error) {
   186  	project, err := parseID(pid)
   187  	if err != nil {
   188  		return nil, nil, err
   189  	}
   190  	u := fmt.Sprintf("projects/%s/variables/%s", PathEscape(project), url.PathEscape(key))
   191  
   192  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   193  	if err != nil {
   194  		return nil, nil, err
   195  	}
   196  
   197  	v := new(ProjectVariable)
   198  	resp, err := s.client.Do(req, v)
   199  	if err != nil {
   200  		return nil, resp, err
   201  	}
   202  
   203  	return v, resp, nil
   204  }
   205  
   206  // RemoveProjectVariableOptions represents the available RemoveVariable()
   207  // options.
   208  //
   209  // GitLab API docs:
   210  // https://docs.gitlab.com/ee/api/project_level_variables.html#delete-a-variable
   211  type RemoveProjectVariableOptions struct {
   212  	Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
   213  }
   214  
   215  // RemoveVariable removes a project's variable.
   216  //
   217  // GitLab API docs:
   218  // https://docs.gitlab.com/ee/api/project_level_variables.html#delete-a-variable
   219  func (s *ProjectVariablesService) RemoveVariable(pid interface{}, key string, opt *RemoveProjectVariableOptions, options ...RequestOptionFunc) (*Response, error) {
   220  	project, err := parseID(pid)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  	u := fmt.Sprintf("projects/%s/variables/%s", PathEscape(project), url.PathEscape(key))
   225  
   226  	req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
   227  	if err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	return s.client.Do(req, nil)
   232  }
   233  

View as plain text