repos_statuses.go 4.0 KB

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