...

Source file src/github.com/xanzy/go-gitlab/group_labels.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  // GroupLabelsService handles communication with the label related methods of the
    25  // GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/group_labels.html
    28  type GroupLabelsService struct {
    29  	client *Client
    30  }
    31  
    32  // GroupLabel represents a GitLab group label.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/group_labels.html
    35  type GroupLabel Label
    36  
    37  func (l GroupLabel) String() string {
    38  	return Stringify(l)
    39  }
    40  
    41  // ListGroupLabelsOptions represents the available ListGroupLabels() options.
    42  //
    43  // GitLab API docs: https://docs.gitlab.com/ee/api/group_labels.html#list-group-labels
    44  type ListGroupLabelsOptions struct {
    45  	ListOptions
    46  	WithCounts               *bool   `url:"with_counts,omitempty" json:"with_counts,omitempty"`
    47  	IncludeAncestorGroups    *bool   `url:"include_ancestor_groups,omitempty" json:"include_ancestor_groups,omitempty"`
    48  	IncludeDescendantGrouops *bool   `url:"include_descendant_groups,omitempty" json:"include_descendant_groups,omitempty"`
    49  	OnlyGroupLabels          *bool   `url:"only_group_labels,omitempty" json:"only_group_labels,omitempty"`
    50  	Search                   *string `url:"search,omitempty" json:"search,omitempty"`
    51  }
    52  
    53  // ListGroupLabels gets all labels for given group.
    54  //
    55  // GitLab API docs:
    56  // https://docs.gitlab.com/ee/api/group_labels.html#list-group-labels
    57  func (s *GroupLabelsService) ListGroupLabels(gid interface{}, opt *ListGroupLabelsOptions, options ...RequestOptionFunc) ([]*GroupLabel, *Response, error) {
    58  	group, err := parseID(gid)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  	u := fmt.Sprintf("groups/%s/labels", PathEscape(group))
    63  
    64  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    65  	if err != nil {
    66  		return nil, nil, err
    67  	}
    68  
    69  	var l []*GroupLabel
    70  	resp, err := s.client.Do(req, &l)
    71  	if err != nil {
    72  		return nil, resp, err
    73  	}
    74  
    75  	return l, resp, nil
    76  }
    77  
    78  // GetGroupLabel get a single label for a given group.
    79  //
    80  // GitLab API docs:
    81  // https://docs.gitlab.com/ee/api/group_labels.html#get-a-single-group-label
    82  func (s *GroupLabelsService) GetGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
    83  	group, err := parseID(gid)
    84  	if err != nil {
    85  		return nil, nil, err
    86  	}
    87  	label, err := parseID(labelID)
    88  	if err != nil {
    89  		return nil, nil, err
    90  	}
    91  	u := fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label))
    92  
    93  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    94  	if err != nil {
    95  		return nil, nil, err
    96  	}
    97  
    98  	var l *GroupLabel
    99  	resp, err := s.client.Do(req, &l)
   100  	if err != nil {
   101  		return nil, resp, err
   102  	}
   103  
   104  	return l, resp, nil
   105  }
   106  
   107  // CreateGroupLabelOptions represents the available CreateGroupLabel() options.
   108  //
   109  // GitLab API docs:
   110  // https://docs.gitlab.com/ee/api/group_labels.html#create-a-new-group-label
   111  type CreateGroupLabelOptions CreateLabelOptions
   112  
   113  // CreateGroupLabel creates a new label for given group with given name and
   114  // color.
   115  //
   116  // GitLab API docs:
   117  // https://docs.gitlab.com/ee/api/group_labels.html#create-a-new-group-label
   118  func (s *GroupLabelsService) CreateGroupLabel(gid interface{}, opt *CreateGroupLabelOptions, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
   119  	group, err := parseID(gid)
   120  	if err != nil {
   121  		return nil, nil, err
   122  	}
   123  	u := fmt.Sprintf("groups/%s/labels", PathEscape(group))
   124  
   125  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   126  	if err != nil {
   127  		return nil, nil, err
   128  	}
   129  
   130  	l := new(GroupLabel)
   131  	resp, err := s.client.Do(req, l)
   132  	if err != nil {
   133  		return nil, resp, err
   134  	}
   135  
   136  	return l, resp, nil
   137  }
   138  
   139  // DeleteGroupLabelOptions represents the available DeleteGroupLabel() options.
   140  //
   141  // GitLab API docs:
   142  // https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label
   143  type DeleteGroupLabelOptions DeleteLabelOptions
   144  
   145  // DeleteGroupLabel deletes a group label given by its name or ID.
   146  //
   147  // GitLab API docs:
   148  // https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label
   149  func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, lid interface{}, opt *DeleteGroupLabelOptions, options ...RequestOptionFunc) (*Response, error) {
   150  	group, err := parseID(gid)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	u := fmt.Sprintf("groups/%s/labels", PathEscape(group))
   155  
   156  	if lid != nil {
   157  		label, err := parseID(lid)
   158  		if err != nil {
   159  			return nil, err
   160  		}
   161  		u = fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label))
   162  	}
   163  
   164  	req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	return s.client.Do(req, nil)
   170  }
   171  
   172  // UpdateGroupLabelOptions represents the available UpdateGroupLabel() options.
   173  //
   174  // GitLab API docs:
   175  // https://docs.gitlab.com/ee/api/group_labels.html#update-a-group-label
   176  type UpdateGroupLabelOptions UpdateLabelOptions
   177  
   178  // UpdateGroupLabel updates an existing label with new name or now color. At least
   179  // one parameter is required, to update the label.
   180  //
   181  // GitLab API docs:
   182  // https://docs.gitlab.com/ee/api/group_labels.html#update-a-group-label
   183  func (s *GroupLabelsService) UpdateGroupLabel(gid interface{}, opt *UpdateGroupLabelOptions, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
   184  	group, err := parseID(gid)
   185  	if err != nil {
   186  		return nil, nil, err
   187  	}
   188  	u := fmt.Sprintf("groups/%s/labels", PathEscape(group))
   189  
   190  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   191  	if err != nil {
   192  		return nil, nil, err
   193  	}
   194  
   195  	l := new(GroupLabel)
   196  	resp, err := s.client.Do(req, l)
   197  	if err != nil {
   198  		return nil, resp, err
   199  	}
   200  
   201  	return l, resp, nil
   202  }
   203  
   204  // SubscribeToGroupLabel subscribes the authenticated user to a label to receive
   205  // notifications. If the user is already subscribed to the label, the status
   206  // code 304 is returned.
   207  //
   208  // GitLab API docs:
   209  // https://docs.gitlab.com/ee/api/group_labels.html#subscribe-to-a-group-label
   210  func (s *GroupLabelsService) SubscribeToGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
   211  	group, err := parseID(gid)
   212  	if err != nil {
   213  		return nil, nil, err
   214  	}
   215  	label, err := parseID(labelID)
   216  	if err != nil {
   217  		return nil, nil, err
   218  	}
   219  	u := fmt.Sprintf("groups/%s/labels/%s/subscribe", PathEscape(group), PathEscape(label))
   220  
   221  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   222  	if err != nil {
   223  		return nil, nil, err
   224  	}
   225  
   226  	l := new(GroupLabel)
   227  	resp, err := s.client.Do(req, l)
   228  	if err != nil {
   229  		return nil, resp, err
   230  	}
   231  
   232  	return l, resp, nil
   233  }
   234  
   235  // UnsubscribeFromGroupLabel unsubscribes the authenticated user from a label to not
   236  // receive notifications from it. If the user is not subscribed to the label, the
   237  // status code 304 is returned.
   238  //
   239  // GitLab API docs:
   240  // https://docs.gitlab.com/ee/api/group_labels.html#unsubscribe-from-a-group-label
   241  func (s *GroupLabelsService) UnsubscribeFromGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) {
   242  	group, err := parseID(gid)
   243  	if err != nil {
   244  		return nil, err
   245  	}
   246  	label, err := parseID(labelID)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  	u := fmt.Sprintf("groups/%s/labels/%s/unsubscribe", PathEscape(group), PathEscape(label))
   251  
   252  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   253  	if err != nil {
   254  		return nil, err
   255  	}
   256  
   257  	return s.client.Do(req, nil)
   258  }
   259  

View as plain text