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