本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6KB

  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. var milestones []*Milestone
  61. resp, err := s.client.Do(ctx, req, &milestones)
  62. if err != nil {
  63. return nil, resp, err
  64. }
  65. return milestones, resp, nil
  66. }
  67. // GetMilestone gets a single milestone.
  68. //
  69. // GitHub API docs: https://developer.github.com/v3/issues/milestones/#get-a-single-milestone
  70. func (s *IssuesService) GetMilestone(ctx context.Context, owner string, repo string, number int) (*Milestone, *Response, error) {
  71. u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number)
  72. req, err := s.client.NewRequest("GET", u, nil)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. milestone := new(Milestone)
  77. resp, err := s.client.Do(ctx, req, milestone)
  78. if err != nil {
  79. return nil, resp, err
  80. }
  81. return milestone, resp, nil
  82. }
  83. // CreateMilestone creates a new milestone on the specified repository.
  84. //
  85. // GitHub API docs: https://developer.github.com/v3/issues/milestones/#create-a-milestone
  86. func (s *IssuesService) CreateMilestone(ctx context.Context, owner string, repo string, milestone *Milestone) (*Milestone, *Response, error) {
  87. u := fmt.Sprintf("repos/%v/%v/milestones", owner, repo)
  88. req, err := s.client.NewRequest("POST", u, milestone)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. m := new(Milestone)
  93. resp, err := s.client.Do(ctx, req, m)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return m, resp, nil
  98. }
  99. // EditMilestone edits a milestone.
  100. //
  101. // GitHub API docs: https://developer.github.com/v3/issues/milestones/#update-a-milestone
  102. func (s *IssuesService) EditMilestone(ctx context.Context, owner string, repo string, number int, milestone *Milestone) (*Milestone, *Response, error) {
  103. u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number)
  104. req, err := s.client.NewRequest("PATCH", u, milestone)
  105. if err != nil {
  106. return nil, nil, err
  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. return m, resp, nil
  114. }
  115. // DeleteMilestone deletes a milestone.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/issues/milestones/#delete-a-milestone
  118. func (s *IssuesService) DeleteMilestone(ctx context.Context, owner string, repo string, number int) (*Response, error) {
  119. u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number)
  120. req, err := s.client.NewRequest("DELETE", u, nil)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return s.client.Do(ctx, req, nil)
  125. }
上海开阖软件有限公司 沪ICP备12045867号-1