custom_attributes.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package gitlab
  2. import (
  3. "fmt"
  4. )
  5. // CustomAttributesService handles communication with the group, project and
  6. // user custom attributes related methods of the GitLab API.
  7. //
  8. // GitLab API docs: https://docs.gitlab.com/ce/api/custom_attributes.html
  9. type CustomAttributesService struct {
  10. client *Client
  11. }
  12. // CustomAttribute struct is used to unmarshal response to api calls.
  13. //
  14. // GitLab API docs: https://docs.gitlab.com/ce/api/custom_attributes.html
  15. type CustomAttribute struct {
  16. Key string `json:"key"`
  17. Value string `json:"value"`
  18. }
  19. // ListCustomUserAttributes lists the custom attributes of the specified user.
  20. //
  21. // GitLab API docs:
  22. // https://docs.gitlab.com/ce/api/custom_attributes.html#list-custom-attributes
  23. func (s *CustomAttributesService) ListCustomUserAttributes(user int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
  24. return s.listCustomAttributes("users", user, options...)
  25. }
  26. // ListCustomGroupAttributes lists the custom attributes of the specified group.
  27. //
  28. // GitLab API docs:
  29. // https://docs.gitlab.com/ce/api/custom_attributes.html#list-custom-attributes
  30. func (s *CustomAttributesService) ListCustomGroupAttributes(group int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
  31. return s.listCustomAttributes("groups", group, options...)
  32. }
  33. // ListCustomProjectAttributes lists the custom attributes of the specified project.
  34. //
  35. // GitLab API docs:
  36. // https://docs.gitlab.com/ce/api/custom_attributes.html#list-custom-attributes
  37. func (s *CustomAttributesService) ListCustomProjectAttributes(project int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
  38. return s.listCustomAttributes("projects", project, options...)
  39. }
  40. func (s *CustomAttributesService) listCustomAttributes(resource string, id int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
  41. u := fmt.Sprintf("%s/%d/custom_attributes", resource, id)
  42. req, err := s.client.NewRequest("GET", u, nil, options)
  43. if err != nil {
  44. return nil, nil, err
  45. }
  46. var cas []*CustomAttribute
  47. resp, err := s.client.Do(req, &cas)
  48. if err != nil {
  49. return nil, resp, err
  50. }
  51. return cas, resp, err
  52. }
  53. // GetCustomUserAttribute returns the user attribute with a speciifc key.
  54. //
  55. // GitLab API docs:
  56. // https://docs.gitlab.com/ce/api/custom_attributes.html#single-custom-attribute
  57. func (s *CustomAttributesService) GetCustomUserAttribute(user int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  58. return s.getCustomAttribute("users", user, key, options...)
  59. }
  60. // GetCustomGroupAttribute returns the group attribute with a speciifc key.
  61. //
  62. // GitLab API docs:
  63. // https://docs.gitlab.com/ce/api/custom_attributes.html#single-custom-attribute
  64. func (s *CustomAttributesService) GetCustomGroupAttribute(group int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  65. return s.getCustomAttribute("groups", group, key, options...)
  66. }
  67. // GetCustomProjectAttribute returns the project attribute with a speciifc key.
  68. //
  69. // GitLab API docs:
  70. // https://docs.gitlab.com/ce/api/custom_attributes.html#single-custom-attribute
  71. func (s *CustomAttributesService) GetCustomProjectAttribute(project int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  72. return s.getCustomAttribute("projects", project, key, options...)
  73. }
  74. func (s *CustomAttributesService) getCustomAttribute(resource string, id int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  75. u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
  76. req, err := s.client.NewRequest("GET", u, nil, options)
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. var ca *CustomAttribute
  81. resp, err := s.client.Do(req, &ca)
  82. if err != nil {
  83. return nil, resp, err
  84. }
  85. return ca, resp, err
  86. }
  87. // SetCustomUserAttribute sets the custom attributes of the specified user.
  88. //
  89. // GitLab API docs:
  90. // https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
  91. func (s *CustomAttributesService) SetCustomUserAttribute(user int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  92. return s.setCustomAttribute("users", user, c, options...)
  93. }
  94. // SetCustomGroupAttribute sets the custom attributes of the specified group.
  95. //
  96. // GitLab API docs:
  97. // https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
  98. func (s *CustomAttributesService) SetCustomGroupAttribute(group int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  99. return s.setCustomAttribute("groups", group, c, options...)
  100. }
  101. // SetCustomProjectAttribute sets the custom attributes of the specified project.
  102. //
  103. // GitLab API docs:
  104. // https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
  105. func (s *CustomAttributesService) SetCustomProjectAttribute(project int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  106. return s.setCustomAttribute("projects", project, c, options...)
  107. }
  108. func (s *CustomAttributesService) setCustomAttribute(resource string, id int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
  109. u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, c.Key)
  110. req, err := s.client.NewRequest("PUT", u, c, options)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. ca := new(CustomAttribute)
  115. resp, err := s.client.Do(req, ca)
  116. if err != nil {
  117. return nil, resp, err
  118. }
  119. return ca, resp, err
  120. }
  121. // DeleteCustomUserAttribute removes the custom attribute of the specified user.
  122. //
  123. // GitLab API docs:
  124. // https://docs.gitlab.com/ce/api/custom_attributes.html#delete-custom-attribute
  125. func (s *CustomAttributesService) DeleteCustomUserAttribute(user int, key string, options ...OptionFunc) (*Response, error) {
  126. return s.deleteCustomAttribute("users", user, key, options...)
  127. }
  128. // DeleteCustomGroupAttribute removes the custom attribute of the specified group.
  129. //
  130. // GitLab API docs:
  131. // https://docs.gitlab.com/ce/api/custom_attributes.html#delete-custom-attribute
  132. func (s *CustomAttributesService) DeleteCustomGroupAttribute(group int, key string, options ...OptionFunc) (*Response, error) {
  133. return s.deleteCustomAttribute("groups", group, key, options...)
  134. }
  135. // DeleteCustomProjectAttribute removes the custom attribute of the specified project.
  136. //
  137. // GitLab API docs:
  138. // https://docs.gitlab.com/ce/api/custom_attributes.html#delete-custom-attribute
  139. func (s *CustomAttributesService) DeleteCustomProjectAttribute(project int, key string, options ...OptionFunc) (*Response, error) {
  140. return s.deleteCustomAttribute("projects", project, key, options...)
  141. }
  142. func (s *CustomAttributesService) deleteCustomAttribute(resource string, id int, key string, options ...OptionFunc) (*Response, error) {
  143. u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
  144. req, err := s.client.NewRequest("DELETE", u, nil, options)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return s.client.Do(req, nil)
  149. }