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