events.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Copyright 2017, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package gitlab
  17. import (
  18. "fmt"
  19. "net/url"
  20. "time"
  21. )
  22. // EventsService handles communication with the event related methods of
  23. // the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/events.html
  26. type EventsService struct {
  27. client *Client
  28. }
  29. // ContributionEvent represents a user's contribution
  30. //
  31. // GitLab API docs:
  32. // https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
  33. type ContributionEvent struct {
  34. Title string `json:"title"`
  35. ProjectID int `json:"project_id"`
  36. ActionName string `json:"action_name"`
  37. TargetID int `json:"target_id"`
  38. TargetIID int `json:"target_iid"`
  39. TargetType string `json:"target_type"`
  40. AuthorID int `json:"author_id"`
  41. TargetTitle string `json:"target_title"`
  42. CreatedAt *time.Time `json:"created_at"`
  43. PushData struct {
  44. CommitCount int `json:"commit_count"`
  45. Action string `json:"action"`
  46. RefType string `json:"ref_type"`
  47. CommitFrom string `json:"commit_from"`
  48. CommitTo string `json:"commit_to"`
  49. Ref string `json:"ref"`
  50. CommitTitle string `json:"commit_title"`
  51. } `json:"push_data"`
  52. Note *Note `json:"note"`
  53. Author struct {
  54. Name string `json:"name"`
  55. Username string `json:"username"`
  56. ID int `json:"id"`
  57. State string `json:"state"`
  58. AvatarURL string `json:"avatar_url"`
  59. WebURL string `json:"web_url"`
  60. } `json:"author"`
  61. AuthorUsername string `json:"author_username"`
  62. }
  63. // ListContributionEventsOptions represents the options for GetUserContributionEvents
  64. //
  65. // GitLap API docs:
  66. // https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
  67. type ListContributionEventsOptions struct {
  68. ListOptions
  69. Action *EventTypeValue `json:"action,omitempty"`
  70. TargetType *EventTargetTypeValue `json:"target_type,omitempty"`
  71. Before *ISOTime `json:"before,omitempty"`
  72. After *ISOTime `json:"after,omitempty"`
  73. Sort *string `json:"sort,omitempty"`
  74. }
  75. // ListUserContributionEvents retrieves user contribution events
  76. // for the specified user, sorted from newest to oldest.
  77. //
  78. // GitLab API docs:
  79. // https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
  80. func (s *UsersService) ListUserContributionEvents(uid interface{}, opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
  81. user, err := parseID(uid)
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. u := fmt.Sprintf("users/%s/events", user)
  86. req, err := s.client.NewRequest("GET", u, opt, options)
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. var cs []*ContributionEvent
  91. resp, err := s.client.Do(req, &cs)
  92. if err != nil {
  93. return nil, resp, err
  94. }
  95. return cs, resp, err
  96. }
  97. // ListCurrentUserContributionEvents gets a list currently authenticated user's events
  98. //
  99. // GitLab API docs: https://docs.gitlab.com/ce/api/events.html#list-currently-authenticated-user-39-s-events
  100. func (s *EventsService) ListCurrentUserContributionEvents(opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
  101. req, err := s.client.NewRequest("GET", "events", opt, options)
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. var cs []*ContributionEvent
  106. resp, err := s.client.Do(req, &cs)
  107. if err != nil {
  108. return nil, resp, err
  109. }
  110. return cs, resp, err
  111. }
  112. // ListProjectVisibleEvents gets a list of visible events for a particular project
  113. //
  114. // GitLab API docs: https://docs.gitlab.com/ee/api/events.html#list-a-project-s-visible-events
  115. func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
  116. project, err := parseID(pid)
  117. if err != nil {
  118. return nil, nil, err
  119. }
  120. u := fmt.Sprintf("projects/%s/events", url.QueryEscape(project))
  121. req, err := s.client.NewRequest("GET", u, opt, options)
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. var cs []*ContributionEvent
  126. resp, err := s.client.Do(req, &cs)
  127. if err != nil {
  128. return nil, resp, err
  129. }
  130. return cs, resp, err
  131. }