issues_milestones.go 5.1 KB

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