github.com/google/go-github/v52@v52.0.0/github/issues_milestones.go (about) 1 // Copyright 2014 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 // Milestone represents a GitHub repository milestone. 14 type Milestone struct { 15 URL *string `json:"url,omitempty"` 16 HTMLURL *string `json:"html_url,omitempty"` 17 LabelsURL *string `json:"labels_url,omitempty"` 18 ID *int64 `json:"id,omitempty"` 19 Number *int `json:"number,omitempty"` 20 State *string `json:"state,omitempty"` 21 Title *string `json:"title,omitempty"` 22 Description *string `json:"description,omitempty"` 23 Creator *User `json:"creator,omitempty"` 24 OpenIssues *int `json:"open_issues,omitempty"` 25 ClosedIssues *int `json:"closed_issues,omitempty"` 26 CreatedAt *Timestamp `json:"created_at,omitempty"` 27 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 28 ClosedAt *Timestamp `json:"closed_at,omitempty"` 29 DueOn *Timestamp `json:"due_on,omitempty"` 30 NodeID *string `json:"node_id,omitempty"` 31 } 32 33 func (m Milestone) String() string { 34 return Stringify(m) 35 } 36 37 // MilestoneListOptions specifies the optional parameters to the 38 // IssuesService.ListMilestones method. 39 type MilestoneListOptions struct { 40 // State filters milestones based on their state. Possible values are: 41 // open, closed, all. Default is "open". 42 State string `url:"state,omitempty"` 43 44 // Sort specifies how to sort milestones. Possible values are: due_on, completeness. 45 // Default value is "due_on". 46 Sort string `url:"sort,omitempty"` 47 48 // Direction in which to sort milestones. Possible values are: asc, desc. 49 // Default is "asc". 50 Direction string `url:"direction,omitempty"` 51 52 ListOptions 53 } 54 55 // ListMilestones lists all milestones for a repository. 56 // 57 // GitHub API docs: https://docs.github.com/en/rest/issues/milestones#list-milestones 58 func (s *IssuesService) ListMilestones(ctx context.Context, owner string, repo string, opts *MilestoneListOptions) ([]*Milestone, *Response, error) { 59 u := fmt.Sprintf("repos/%v/%v/milestones", owner, repo) 60 u, err := addOptions(u, opts) 61 if err != nil { 62 return nil, nil, err 63 } 64 65 req, err := s.client.NewRequest("GET", u, nil) 66 if err != nil { 67 return nil, nil, err 68 } 69 70 var milestones []*Milestone 71 resp, err := s.client.Do(ctx, req, &milestones) 72 if err != nil { 73 return nil, resp, err 74 } 75 76 return milestones, resp, nil 77 } 78 79 // GetMilestone gets a single milestone. 80 // 81 // GitHub API docs: https://docs.github.com/en/rest/issues/milestones#get-a-milestone 82 func (s *IssuesService) GetMilestone(ctx context.Context, owner string, repo string, number int) (*Milestone, *Response, error) { 83 u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number) 84 req, err := s.client.NewRequest("GET", u, nil) 85 if err != nil { 86 return nil, nil, err 87 } 88 89 milestone := new(Milestone) 90 resp, err := s.client.Do(ctx, req, milestone) 91 if err != nil { 92 return nil, resp, err 93 } 94 95 return milestone, resp, nil 96 } 97 98 // CreateMilestone creates a new milestone on the specified repository. 99 // 100 // GitHub API docs: https://docs.github.com/en/rest/issues/milestones#create-a-milestone 101 func (s *IssuesService) CreateMilestone(ctx context.Context, owner string, repo string, milestone *Milestone) (*Milestone, *Response, error) { 102 u := fmt.Sprintf("repos/%v/%v/milestones", owner, repo) 103 req, err := s.client.NewRequest("POST", u, milestone) 104 if err != nil { 105 return nil, nil, err 106 } 107 108 m := new(Milestone) 109 resp, err := s.client.Do(ctx, req, m) 110 if err != nil { 111 return nil, resp, err 112 } 113 114 return m, resp, nil 115 } 116 117 // EditMilestone edits a milestone. 118 // 119 // GitHub API docs: https://docs.github.com/en/rest/issues/milestones#update-a-milestone 120 func (s *IssuesService) EditMilestone(ctx context.Context, owner string, repo string, number int, milestone *Milestone) (*Milestone, *Response, error) { 121 u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number) 122 req, err := s.client.NewRequest("PATCH", u, milestone) 123 if err != nil { 124 return nil, nil, err 125 } 126 127 m := new(Milestone) 128 resp, err := s.client.Do(ctx, req, m) 129 if err != nil { 130 return nil, resp, err 131 } 132 133 return m, resp, nil 134 } 135 136 // DeleteMilestone deletes a milestone. 137 // 138 // GitHub API docs: https://docs.github.com/en/rest/issues/milestones#delete-a-milestone 139 func (s *IssuesService) DeleteMilestone(ctx context.Context, owner string, repo string, number int) (*Response, error) { 140 u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number) 141 req, err := s.client.NewRequest("DELETE", u, nil) 142 if err != nil { 143 return nil, err 144 } 145 146 return s.client.Do(ctx, req, nil) 147 }