...

Source file src/github.com/xanzy/go-gitlab/custom_attributes.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  )
    23  
    24  // CustomAttributesService handles communication with the group, project and
    25  // user custom attributes related methods of the GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/custom_attributes.html
    28  type CustomAttributesService struct {
    29  	client *Client
    30  }
    31  
    32  // CustomAttribute struct is used to unmarshal response to api calls.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/custom_attributes.html
    35  type CustomAttribute struct {
    36  	Key   string `json:"key"`
    37  	Value string `json:"value"`
    38  }
    39  
    40  // ListCustomUserAttributes lists the custom attributes of the specified user.
    41  //
    42  // GitLab API docs:
    43  // https://docs.gitlab.com/ee/api/custom_attributes.html#list-custom-attributes
    44  func (s *CustomAttributesService) ListCustomUserAttributes(user int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
    45  	return s.listCustomAttributes("users", user, options...)
    46  }
    47  
    48  // ListCustomGroupAttributes lists the custom attributes of the specified group.
    49  //
    50  // GitLab API docs:
    51  // https://docs.gitlab.com/ee/api/custom_attributes.html#list-custom-attributes
    52  func (s *CustomAttributesService) ListCustomGroupAttributes(group int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
    53  	return s.listCustomAttributes("groups", group, options...)
    54  }
    55  
    56  // ListCustomProjectAttributes lists the custom attributes of the specified project.
    57  //
    58  // GitLab API docs:
    59  // https://docs.gitlab.com/ee/api/custom_attributes.html#list-custom-attributes
    60  func (s *CustomAttributesService) ListCustomProjectAttributes(project int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
    61  	return s.listCustomAttributes("projects", project, options...)
    62  }
    63  
    64  func (s *CustomAttributesService) listCustomAttributes(resource string, id int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
    65  	u := fmt.Sprintf("%s/%d/custom_attributes", resource, id)
    66  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    67  	if err != nil {
    68  		return nil, nil, err
    69  	}
    70  
    71  	var cas []*CustomAttribute
    72  	resp, err := s.client.Do(req, &cas)
    73  	if err != nil {
    74  		return nil, resp, err
    75  	}
    76  	return cas, resp, nil
    77  }
    78  
    79  // GetCustomUserAttribute returns the user attribute with a speciifc key.
    80  //
    81  // GitLab API docs:
    82  // https://docs.gitlab.com/ee/api/custom_attributes.html#single-custom-attribute
    83  func (s *CustomAttributesService) GetCustomUserAttribute(user int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
    84  	return s.getCustomAttribute("users", user, key, options...)
    85  }
    86  
    87  // GetCustomGroupAttribute returns the group attribute with a speciifc key.
    88  //
    89  // GitLab API docs:
    90  // https://docs.gitlab.com/ee/api/custom_attributes.html#single-custom-attribute
    91  func (s *CustomAttributesService) GetCustomGroupAttribute(group int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
    92  	return s.getCustomAttribute("groups", group, key, options...)
    93  }
    94  
    95  // GetCustomProjectAttribute returns the project attribute with a speciifc key.
    96  //
    97  // GitLab API docs:
    98  // https://docs.gitlab.com/ee/api/custom_attributes.html#single-custom-attribute
    99  func (s *CustomAttributesService) GetCustomProjectAttribute(project int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
   100  	return s.getCustomAttribute("projects", project, key, options...)
   101  }
   102  
   103  func (s *CustomAttributesService) getCustomAttribute(resource string, id int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
   104  	u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
   105  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  
   110  	var ca *CustomAttribute
   111  	resp, err := s.client.Do(req, &ca)
   112  	if err != nil {
   113  		return nil, resp, err
   114  	}
   115  	return ca, resp, nil
   116  }
   117  
   118  // SetCustomUserAttribute sets the custom attributes of the specified user.
   119  //
   120  // GitLab API docs:
   121  // https://docs.gitlab.com/ee/api/custom_attributes.html#set-custom-attribute
   122  func (s *CustomAttributesService) SetCustomUserAttribute(user int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
   123  	return s.setCustomAttribute("users", user, c, options...)
   124  }
   125  
   126  // SetCustomGroupAttribute sets the custom attributes of the specified group.
   127  //
   128  // GitLab API docs:
   129  // https://docs.gitlab.com/ee/api/custom_attributes.html#set-custom-attribute
   130  func (s *CustomAttributesService) SetCustomGroupAttribute(group int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
   131  	return s.setCustomAttribute("groups", group, c, options...)
   132  }
   133  
   134  // SetCustomProjectAttribute sets the custom attributes of the specified project.
   135  //
   136  // GitLab API docs:
   137  // https://docs.gitlab.com/ee/api/custom_attributes.html#set-custom-attribute
   138  func (s *CustomAttributesService) SetCustomProjectAttribute(project int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
   139  	return s.setCustomAttribute("projects", project, c, options...)
   140  }
   141  
   142  func (s *CustomAttributesService) setCustomAttribute(resource string, id int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
   143  	u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, c.Key)
   144  	req, err := s.client.NewRequest(http.MethodPut, u, c, options)
   145  	if err != nil {
   146  		return nil, nil, err
   147  	}
   148  
   149  	ca := new(CustomAttribute)
   150  	resp, err := s.client.Do(req, ca)
   151  	if err != nil {
   152  		return nil, resp, err
   153  	}
   154  	return ca, resp, nil
   155  }
   156  
   157  // DeleteCustomUserAttribute removes the custom attribute of the specified user.
   158  //
   159  // GitLab API docs:
   160  // https://docs.gitlab.com/ee/api/custom_attributes.html#delete-custom-attribute
   161  func (s *CustomAttributesService) DeleteCustomUserAttribute(user int, key string, options ...RequestOptionFunc) (*Response, error) {
   162  	return s.deleteCustomAttribute("users", user, key, options...)
   163  }
   164  
   165  // DeleteCustomGroupAttribute removes the custom attribute of the specified group.
   166  //
   167  // GitLab API docs:
   168  // https://docs.gitlab.com/ee/api/custom_attributes.html#delete-custom-attribute
   169  func (s *CustomAttributesService) DeleteCustomGroupAttribute(group int, key string, options ...RequestOptionFunc) (*Response, error) {
   170  	return s.deleteCustomAttribute("groups", group, key, options...)
   171  }
   172  
   173  // DeleteCustomProjectAttribute removes the custom attribute of the specified project.
   174  //
   175  // GitLab API docs:
   176  // https://docs.gitlab.com/ee/api/custom_attributes.html#delete-custom-attribute
   177  func (s *CustomAttributesService) DeleteCustomProjectAttribute(project int, key string, options ...RequestOptionFunc) (*Response, error) {
   178  	return s.deleteCustomAttribute("projects", project, key, options...)
   179  }
   180  
   181  func (s *CustomAttributesService) deleteCustomAttribute(resource string, id int, key string, options ...RequestOptionFunc) (*Response, error) {
   182  	u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
   183  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	return s.client.Do(req, nil)
   188  }
   189  

View as plain text