github.com/google/go-github/v57@v57.0.0/github/issues_labels.go (about)

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  )
    12  
    13  // Label represents a GitHub label on an Issue
    14  type Label struct {
    15  	ID          *int64  `json:"id,omitempty"`
    16  	URL         *string `json:"url,omitempty"`
    17  	Name        *string `json:"name,omitempty"`
    18  	Color       *string `json:"color,omitempty"`
    19  	Description *string `json:"description,omitempty"`
    20  	Default     *bool   `json:"default,omitempty"`
    21  	NodeID      *string `json:"node_id,omitempty"`
    22  }
    23  
    24  func (l Label) String() string {
    25  	return Stringify(l)
    26  }
    27  
    28  // ListLabels lists all labels for a repository.
    29  //
    30  // GitHub API docs: https://docs.github.com/rest/issues/labels#list-labels-for-a-repository
    31  //
    32  //meta:operation GET /repos/{owner}/{repo}/labels
    33  func (s *IssuesService) ListLabels(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*Label, *Response, error) {
    34  	u := fmt.Sprintf("repos/%v/%v/labels", owner, repo)
    35  	u, err := addOptions(u, opts)
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  
    40  	req, err := s.client.NewRequest("GET", u, nil)
    41  	if err != nil {
    42  		return nil, nil, err
    43  	}
    44  
    45  	var labels []*Label
    46  	resp, err := s.client.Do(ctx, req, &labels)
    47  	if err != nil {
    48  		return nil, resp, err
    49  	}
    50  
    51  	return labels, resp, nil
    52  }
    53  
    54  // GetLabel gets a single label.
    55  //
    56  // GitHub API docs: https://docs.github.com/rest/issues/labels#get-a-label
    57  //
    58  //meta:operation GET /repos/{owner}/{repo}/labels/{name}
    59  func (s *IssuesService) GetLabel(ctx context.Context, owner string, repo string, name string) (*Label, *Response, error) {
    60  	u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
    61  	req, err := s.client.NewRequest("GET", u, nil)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	label := new(Label)
    67  	resp, err := s.client.Do(ctx, req, label)
    68  	if err != nil {
    69  		return nil, resp, err
    70  	}
    71  
    72  	return label, resp, nil
    73  }
    74  
    75  // CreateLabel creates a new label on the specified repository.
    76  //
    77  // GitHub API docs: https://docs.github.com/rest/issues/labels#create-a-label
    78  //
    79  //meta:operation POST /repos/{owner}/{repo}/labels
    80  func (s *IssuesService) CreateLabel(ctx context.Context, owner string, repo string, label *Label) (*Label, *Response, error) {
    81  	u := fmt.Sprintf("repos/%v/%v/labels", owner, repo)
    82  	req, err := s.client.NewRequest("POST", u, label)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  
    87  	l := new(Label)
    88  	resp, err := s.client.Do(ctx, req, l)
    89  	if err != nil {
    90  		return nil, resp, err
    91  	}
    92  
    93  	return l, resp, nil
    94  }
    95  
    96  // EditLabel edits a label.
    97  //
    98  // GitHub API docs: https://docs.github.com/rest/issues/labels#update-a-label
    99  //
   100  //meta:operation PATCH /repos/{owner}/{repo}/labels/{name}
   101  func (s *IssuesService) EditLabel(ctx context.Context, owner string, repo string, name string, label *Label) (*Label, *Response, error) {
   102  	u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
   103  	req, err := s.client.NewRequest("PATCH", u, label)
   104  	if err != nil {
   105  		return nil, nil, err
   106  	}
   107  
   108  	l := new(Label)
   109  	resp, err := s.client.Do(ctx, req, l)
   110  	if err != nil {
   111  		return nil, resp, err
   112  	}
   113  
   114  	return l, resp, nil
   115  }
   116  
   117  // DeleteLabel deletes a label.
   118  //
   119  // GitHub API docs: https://docs.github.com/rest/issues/labels#delete-a-label
   120  //
   121  //meta:operation DELETE /repos/{owner}/{repo}/labels/{name}
   122  func (s *IssuesService) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*Response, error) {
   123  	u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
   124  	req, err := s.client.NewRequest("DELETE", u, nil)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	return s.client.Do(ctx, req, nil)
   129  }
   130  
   131  // ListLabelsByIssue lists all labels for an issue.
   132  //
   133  // GitHub API docs: https://docs.github.com/rest/issues/labels#list-labels-for-an-issue
   134  //
   135  //meta:operation GET /repos/{owner}/{repo}/issues/{issue_number}/labels
   136  func (s *IssuesService) ListLabelsByIssue(ctx context.Context, owner string, repo string, number int, opts *ListOptions) ([]*Label, *Response, error) {
   137  	u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
   138  	u, err := addOptions(u, opts)
   139  	if err != nil {
   140  		return nil, nil, err
   141  	}
   142  
   143  	req, err := s.client.NewRequest("GET", u, nil)
   144  	if err != nil {
   145  		return nil, nil, err
   146  	}
   147  
   148  	var labels []*Label
   149  	resp, err := s.client.Do(ctx, req, &labels)
   150  	if err != nil {
   151  		return nil, resp, err
   152  	}
   153  
   154  	return labels, resp, nil
   155  }
   156  
   157  // AddLabelsToIssue adds labels to an issue.
   158  //
   159  // GitHub API docs: https://docs.github.com/rest/issues/labels#add-labels-to-an-issue
   160  //
   161  //meta:operation POST /repos/{owner}/{repo}/issues/{issue_number}/labels
   162  func (s *IssuesService) AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
   163  	u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
   164  	req, err := s.client.NewRequest("POST", u, labels)
   165  	if err != nil {
   166  		return nil, nil, err
   167  	}
   168  
   169  	var l []*Label
   170  	resp, err := s.client.Do(ctx, req, &l)
   171  	if err != nil {
   172  		return nil, resp, err
   173  	}
   174  
   175  	return l, resp, nil
   176  }
   177  
   178  // RemoveLabelForIssue removes a label for an issue.
   179  //
   180  // GitHub API docs: https://docs.github.com/rest/issues/labels#remove-a-label-from-an-issue
   181  //
   182  //meta:operation DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels/{name}
   183  func (s *IssuesService) RemoveLabelForIssue(ctx context.Context, owner string, repo string, number int, label string) (*Response, error) {
   184  	u := fmt.Sprintf("repos/%v/%v/issues/%d/labels/%v", owner, repo, number, label)
   185  	req, err := s.client.NewRequest("DELETE", u, nil)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	return s.client.Do(ctx, req, nil)
   191  }
   192  
   193  // ReplaceLabelsForIssue replaces all labels for an issue.
   194  //
   195  // GitHub API docs: https://docs.github.com/rest/issues/labels#set-labels-for-an-issue
   196  //
   197  //meta:operation PUT /repos/{owner}/{repo}/issues/{issue_number}/labels
   198  func (s *IssuesService) ReplaceLabelsForIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
   199  	u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
   200  	req, err := s.client.NewRequest("PUT", u, labels)
   201  	if err != nil {
   202  		return nil, nil, err
   203  	}
   204  
   205  	var l []*Label
   206  	resp, err := s.client.Do(ctx, req, &l)
   207  	if err != nil {
   208  		return nil, resp, err
   209  	}
   210  
   211  	return l, resp, nil
   212  }
   213  
   214  // RemoveLabelsForIssue removes all labels for an issue.
   215  //
   216  // GitHub API docs: https://docs.github.com/rest/issues/labels#remove-all-labels-from-an-issue
   217  //
   218  //meta:operation DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels
   219  func (s *IssuesService) RemoveLabelsForIssue(ctx context.Context, owner string, repo string, number int) (*Response, error) {
   220  	u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
   221  	req, err := s.client.NewRequest("DELETE", u, nil)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  
   226  	return s.client.Do(ctx, req, nil)
   227  }
   228  
   229  // ListLabelsForMilestone lists labels for every issue in a milestone.
   230  //
   231  // GitHub API docs: https://docs.github.com/rest/issues/labels#list-labels-for-issues-in-a-milestone
   232  //
   233  //meta:operation GET /repos/{owner}/{repo}/milestones/{milestone_number}/labels
   234  func (s *IssuesService) ListLabelsForMilestone(ctx context.Context, owner string, repo string, number int, opts *ListOptions) ([]*Label, *Response, error) {
   235  	u := fmt.Sprintf("repos/%v/%v/milestones/%d/labels", owner, repo, number)
   236  	u, err := addOptions(u, opts)
   237  	if err != nil {
   238  		return nil, nil, err
   239  	}
   240  
   241  	req, err := s.client.NewRequest("GET", u, nil)
   242  	if err != nil {
   243  		return nil, nil, err
   244  	}
   245  
   246  	var labels []*Label
   247  	resp, err := s.client.Do(ctx, req, &labels)
   248  	if err != nil {
   249  		return nil, resp, err
   250  	}
   251  
   252  	return labels, resp, nil
   253  }