本站源代码
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.

131 lines
4.1KB

  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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "net/url"
  10. "time"
  11. )
  12. // RepoStatus represents the status of a repository at a particular reference.
  13. type RepoStatus struct {
  14. ID *int64 `json:"id,omitempty"`
  15. URL *string `json:"url,omitempty"`
  16. // State is the current state of the repository. Possible values are:
  17. // pending, success, error, or failure.
  18. State *string `json:"state,omitempty"`
  19. // TargetURL is the URL of the page representing this status. It will be
  20. // linked from the GitHub UI to allow users to see the source of the status.
  21. TargetURL *string `json:"target_url,omitempty"`
  22. // Description is a short high level summary of the status.
  23. Description *string `json:"description,omitempty"`
  24. // A string label to differentiate this status from the statuses of other systems.
  25. Context *string `json:"context,omitempty"`
  26. Creator *User `json:"creator,omitempty"`
  27. CreatedAt *time.Time `json:"created_at,omitempty"`
  28. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  29. }
  30. func (r RepoStatus) String() string {
  31. return Stringify(r)
  32. }
  33. // ListStatuses lists the statuses of a repository at the specified
  34. // reference. ref can be a SHA, a branch name, or a tag name.
  35. //
  36. // GitHub API docs: https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref
  37. func (s *RepositoriesService) ListStatuses(ctx context.Context, owner, repo, ref string, opt *ListOptions) ([]*RepoStatus, *Response, error) {
  38. u := fmt.Sprintf("repos/%v/%v/commits/%v/statuses", owner, repo, url.QueryEscape(ref))
  39. u, err := addOptions(u, opt)
  40. if err != nil {
  41. return nil, nil, err
  42. }
  43. req, err := s.client.NewRequest("GET", u, nil)
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. var statuses []*RepoStatus
  48. resp, err := s.client.Do(ctx, req, &statuses)
  49. if err != nil {
  50. return nil, resp, err
  51. }
  52. return statuses, resp, nil
  53. }
  54. // CreateStatus creates a new status for a repository at the specified
  55. // reference. Ref can be a SHA, a branch name, or a tag name.
  56. //
  57. // GitHub API docs: https://developer.github.com/v3/repos/statuses/#create-a-status
  58. func (s *RepositoriesService) CreateStatus(ctx context.Context, owner, repo, ref string, status *RepoStatus) (*RepoStatus, *Response, error) {
  59. u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, url.QueryEscape(ref))
  60. req, err := s.client.NewRequest("POST", u, status)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. repoStatus := new(RepoStatus)
  65. resp, err := s.client.Do(ctx, req, repoStatus)
  66. if err != nil {
  67. return nil, resp, err
  68. }
  69. return repoStatus, resp, nil
  70. }
  71. // CombinedStatus represents the combined status of a repository at a particular reference.
  72. type CombinedStatus struct {
  73. // State is the combined state of the repository. Possible values are:
  74. // failure, pending, or success.
  75. State *string `json:"state,omitempty"`
  76. Name *string `json:"name,omitempty"`
  77. SHA *string `json:"sha,omitempty"`
  78. TotalCount *int `json:"total_count,omitempty"`
  79. Statuses []RepoStatus `json:"statuses,omitempty"`
  80. CommitURL *string `json:"commit_url,omitempty"`
  81. RepositoryURL *string `json:"repository_url,omitempty"`
  82. }
  83. func (s CombinedStatus) String() string {
  84. return Stringify(s)
  85. }
  86. // GetCombinedStatus returns the combined status of a repository at the specified
  87. // reference. ref can be a SHA, a branch name, or a tag name.
  88. //
  89. // GitHub API docs: https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref
  90. func (s *RepositoriesService) GetCombinedStatus(ctx context.Context, owner, repo, ref string, opt *ListOptions) (*CombinedStatus, *Response, error) {
  91. u := fmt.Sprintf("repos/%v/%v/commits/%v/status", owner, repo, url.QueryEscape(ref))
  92. u, err := addOptions(u, opt)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. req, err := s.client.NewRequest("GET", u, nil)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. status := new(CombinedStatus)
  101. resp, err := s.client.Do(ctx, req, status)
  102. if err != nil {
  103. return nil, resp, err
  104. }
  105. return status, resp, nil
  106. }
上海开阖软件有限公司 沪ICP备12045867号-1