...

Source file src/github.com/xanzy/go-gitlab/namespaces.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  // NamespacesService handles communication with the namespace related methods
    25  // of the GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/namespaces.html
    28  type NamespacesService struct {
    29  	client *Client
    30  }
    31  
    32  // Namespace represents a GitLab namespace.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/namespaces.html
    35  type Namespace struct {
    36  	ID                          int      `json:"id"`
    37  	Name                        string   `json:"name"`
    38  	Path                        string   `json:"path"`
    39  	Kind                        string   `json:"kind"`
    40  	FullPath                    string   `json:"full_path"`
    41  	ParentID                    int      `json:"parent_id"`
    42  	AvatarURL                   *string  `json:"avatar_url"`
    43  	WebURL                      string   `json:"web_url"`
    44  	MembersCountWithDescendants int      `json:"members_count_with_descendants"`
    45  	BillableMembersCount        int      `json:"billable_members_count"`
    46  	Plan                        string   `json:"plan"`
    47  	TrialEndsOn                 *ISOTime `json:"trial_ends_on"`
    48  	Trial                       bool     `json:"trial"`
    49  	MaxSeatsUsed                *int     `json:"max_seats_used"`
    50  	SeatsInUse                  *int     `json:"seats_in_use"`
    51  }
    52  
    53  func (n Namespace) String() string {
    54  	return Stringify(n)
    55  }
    56  
    57  // ListNamespacesOptions represents the available ListNamespaces() options.
    58  //
    59  // GitLab API docs: https://docs.gitlab.com/ee/api/namespaces.html#list-namespaces
    60  type ListNamespacesOptions struct {
    61  	ListOptions
    62  	Search    *string `url:"search,omitempty" json:"search,omitempty"`
    63  	OwnedOnly *bool   `url:"owned_only,omitempty" json:"owned_only,omitempty"`
    64  }
    65  
    66  // ListNamespaces gets a list of projects accessible by the authenticated user.
    67  //
    68  // GitLab API docs: https://docs.gitlab.com/ee/api/namespaces.html#list-namespaces
    69  func (s *NamespacesService) ListNamespaces(opt *ListNamespacesOptions, options ...RequestOptionFunc) ([]*Namespace, *Response, error) {
    70  	req, err := s.client.NewRequest(http.MethodGet, "namespaces", opt, options)
    71  	if err != nil {
    72  		return nil, nil, err
    73  	}
    74  
    75  	var n []*Namespace
    76  	resp, err := s.client.Do(req, &n)
    77  	if err != nil {
    78  		return nil, resp, err
    79  	}
    80  
    81  	return n, resp, nil
    82  }
    83  
    84  // SearchNamespace gets all namespaces that match your string in their name
    85  // or path.
    86  //
    87  // GitLab API docs:
    88  // https://docs.gitlab.com/ee/api/namespaces.html#list-namespaces
    89  func (s *NamespacesService) SearchNamespace(query string, options ...RequestOptionFunc) ([]*Namespace, *Response, error) {
    90  	var q struct {
    91  		Search string `url:"search,omitempty" json:"search,omitempty"`
    92  	}
    93  	q.Search = query
    94  
    95  	req, err := s.client.NewRequest(http.MethodGet, "namespaces", &q, options)
    96  	if err != nil {
    97  		return nil, nil, err
    98  	}
    99  
   100  	var n []*Namespace
   101  	resp, err := s.client.Do(req, &n)
   102  	if err != nil {
   103  		return nil, resp, err
   104  	}
   105  
   106  	return n, resp, nil
   107  }
   108  
   109  // GetNamespace gets a namespace by id.
   110  //
   111  // GitLab API docs:
   112  // https://docs.gitlab.com/ee/api/namespaces.html#get-namespace-by-id
   113  func (s *NamespacesService) GetNamespace(id interface{}, options ...RequestOptionFunc) (*Namespace, *Response, error) {
   114  	namespace, err := parseID(id)
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  	u := fmt.Sprintf("namespaces/%s", PathEscape(namespace))
   119  
   120  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   121  	if err != nil {
   122  		return nil, nil, err
   123  	}
   124  
   125  	n := new(Namespace)
   126  	resp, err := s.client.Do(req, n)
   127  	if err != nil {
   128  		return nil, resp, err
   129  	}
   130  
   131  	return n, resp, nil
   132  }
   133  
   134  // NamespaceExistance represents a namespace exists result.
   135  //
   136  // GitLab API docs:
   137  // https://docs.gitlab.com/ee/api/namespaces.html#get-existence-of-a-namespace
   138  type NamespaceExistance struct {
   139  	Exists   bool     `json:"exists"`
   140  	Suggests []string `json:"suggests"`
   141  }
   142  
   143  // NamespaceExistsOptions represents the available NamespaceExists() options.
   144  //
   145  // GitLab API docs:
   146  // https://docs.gitlab.com/ee/api/namespaces.html#get-existence-of-a-namespace
   147  type NamespaceExistsOptions struct {
   148  	ParentID *int `url:"parent_id,omitempty" json:"parent_id,omitempty"`
   149  }
   150  
   151  // NamespaceExists checks the existence of a namespace.
   152  //
   153  // GitLab API docs:
   154  // https://docs.gitlab.com/ee/api/namespaces.html#get-existence-of-a-namespace
   155  func (s *NamespacesService) NamespaceExists(id interface{}, opt *NamespaceExistsOptions, options ...RequestOptionFunc) (*NamespaceExistance, *Response, error) {
   156  	namespace, err := parseID(id)
   157  	if err != nil {
   158  		return nil, nil, err
   159  	}
   160  	u := fmt.Sprintf("namespaces/%s/exists", namespace)
   161  
   162  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   163  	if err != nil {
   164  		return nil, nil, err
   165  	}
   166  
   167  	n := new(NamespaceExistance)
   168  	resp, err := s.client.Do(req, n)
   169  	if err != nil {
   170  		return nil, resp, err
   171  	}
   172  
   173  	return n, resp, nil
   174  }
   175  

View as plain text