namespaces.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright 2017, 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. package gitlab
  17. import (
  18. "fmt"
  19. )
  20. // NamespacesService handles communication with the namespace related methods
  21. // of the GitLab API.
  22. //
  23. // GitLab API docs: https://docs.gitlab.com/ce/api/namespaces.html
  24. type NamespacesService struct {
  25. client *Client
  26. }
  27. // Namespace represents a GitLab namespace.
  28. //
  29. // GitLab API docs: https://docs.gitlab.com/ce/api/namespaces.html
  30. type Namespace struct {
  31. ID int `json:"id"`
  32. Name string `json:"name"`
  33. Path string `json:"path"`
  34. Kind string `json:"kind"`
  35. FullPath string `json:"full_path"`
  36. ParentID int `json:"parent_id"`
  37. MembersCountWithDescendants int `json:"members_count_with_descendants"`
  38. }
  39. func (n Namespace) String() string {
  40. return Stringify(n)
  41. }
  42. // ListNamespacesOptions represents the available ListNamespaces() options.
  43. //
  44. // GitLab API docs: https://docs.gitlab.com/ce/api/namespaces.html#list-namespaces
  45. type ListNamespacesOptions struct {
  46. ListOptions
  47. Search *string `url:"search,omitempty" json:"search,omitempty"`
  48. }
  49. // ListNamespaces gets a list of projects accessible by the authenticated user.
  50. //
  51. // GitLab API docs: https://docs.gitlab.com/ce/api/namespaces.html#list-namespaces
  52. func (s *NamespacesService) ListNamespaces(opt *ListNamespacesOptions, options ...OptionFunc) ([]*Namespace, *Response, error) {
  53. req, err := s.client.NewRequest("GET", "namespaces", opt, options)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. var n []*Namespace
  58. resp, err := s.client.Do(req, &n)
  59. if err != nil {
  60. return nil, resp, err
  61. }
  62. return n, resp, err
  63. }
  64. // SearchNamespace gets all namespaces that match your string in their name
  65. // or path.
  66. //
  67. // GitLab API docs:
  68. // https://docs.gitlab.com/ce/api/namespaces.html#search-for-namespace
  69. func (s *NamespacesService) SearchNamespace(query string, options ...OptionFunc) ([]*Namespace, *Response, error) {
  70. var q struct {
  71. Search string `url:"search,omitempty" json:"search,omitempty"`
  72. }
  73. q.Search = query
  74. req, err := s.client.NewRequest("GET", "namespaces", &q, options)
  75. if err != nil {
  76. return nil, nil, err
  77. }
  78. var n []*Namespace
  79. resp, err := s.client.Do(req, &n)
  80. if err != nil {
  81. return nil, resp, err
  82. }
  83. return n, resp, err
  84. }
  85. // GetNamespace gets a namespace by id.
  86. //
  87. // GitLab API docs:
  88. // https://docs.gitlab.com/ce/api/namespaces.html#get-namespace-by-id
  89. func (s *NamespacesService) GetNamespace(id interface{}, options ...OptionFunc) (*Namespace, *Response, error) {
  90. namespace, err := parseID(id)
  91. if err != nil {
  92. return nil, nil, err
  93. }
  94. u := fmt.Sprintf("namespaces/%s", namespace)
  95. req, err := s.client.NewRequest("GET", u, nil, options)
  96. if err != nil {
  97. return nil, nil, err
  98. }
  99. n := new(Namespace)
  100. resp, err := s.client.Do(req, n)
  101. if err != nil {
  102. return nil, resp, err
  103. }
  104. return n, resp, err
  105. }