activity_events.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. "encoding/json"
  9. "fmt"
  10. "time"
  11. )
  12. // Event represents a GitHub event.
  13. type Event struct {
  14. Type *string `json:"type,omitempty"`
  15. Public *bool `json:"public,omitempty"`
  16. RawPayload *json.RawMessage `json:"payload,omitempty"`
  17. Repo *Repository `json:"repo,omitempty"`
  18. Actor *User `json:"actor,omitempty"`
  19. Org *Organization `json:"org,omitempty"`
  20. CreatedAt *time.Time `json:"created_at,omitempty"`
  21. ID *string `json:"id,omitempty"`
  22. }
  23. func (e Event) String() string {
  24. return Stringify(e)
  25. }
  26. // ParsePayload parses the event payload. For recognized event types,
  27. // a value of the corresponding struct type will be returned.
  28. func (e *Event) ParsePayload() (payload interface{}, err error) {
  29. switch *e.Type {
  30. case "CommitCommentEvent":
  31. payload = &CommitCommentEvent{}
  32. case "CreateEvent":
  33. payload = &CreateEvent{}
  34. case "DeleteEvent":
  35. payload = &DeleteEvent{}
  36. case "DeploymentEvent":
  37. payload = &DeploymentEvent{}
  38. case "DeploymentStatusEvent":
  39. payload = &DeploymentStatusEvent{}
  40. case "ForkEvent":
  41. payload = &ForkEvent{}
  42. case "GollumEvent":
  43. payload = &GollumEvent{}
  44. case "InstallationEvent":
  45. payload = &InstallationEvent{}
  46. case "InstallationRepositoriesEvent":
  47. payload = &InstallationRepositoriesEvent{}
  48. case "IssueCommentEvent":
  49. payload = &IssueCommentEvent{}
  50. case "IssuesEvent":
  51. payload = &IssuesEvent{}
  52. case "LabelEvent":
  53. payload = &LabelEvent{}
  54. case "MarketplacePurchaseEvent":
  55. payload = &MarketplacePurchaseEvent{}
  56. case "MemberEvent":
  57. payload = &MemberEvent{}
  58. case "MembershipEvent":
  59. payload = &MembershipEvent{}
  60. case "MilestoneEvent":
  61. payload = &MilestoneEvent{}
  62. case "OrganizationEvent":
  63. payload = &OrganizationEvent{}
  64. case "OrgBlockEvent":
  65. payload = &OrgBlockEvent{}
  66. case "PageBuildEvent":
  67. payload = &PageBuildEvent{}
  68. case "PingEvent":
  69. payload = &PingEvent{}
  70. case "ProjectEvent":
  71. payload = &ProjectEvent{}
  72. case "ProjectCardEvent":
  73. payload = &ProjectCardEvent{}
  74. case "ProjectColumnEvent":
  75. payload = &ProjectColumnEvent{}
  76. case "PublicEvent":
  77. payload = &PublicEvent{}
  78. case "PullRequestEvent":
  79. payload = &PullRequestEvent{}
  80. case "PullRequestReviewEvent":
  81. payload = &PullRequestReviewEvent{}
  82. case "PullRequestReviewCommentEvent":
  83. payload = &PullRequestReviewCommentEvent{}
  84. case "PushEvent":
  85. payload = &PushEvent{}
  86. case "ReleaseEvent":
  87. payload = &ReleaseEvent{}
  88. case "RepositoryEvent":
  89. payload = &RepositoryEvent{}
  90. case "StatusEvent":
  91. payload = &StatusEvent{}
  92. case "TeamEvent":
  93. payload = &TeamEvent{}
  94. case "TeamAddEvent":
  95. payload = &TeamAddEvent{}
  96. case "WatchEvent":
  97. payload = &WatchEvent{}
  98. }
  99. err = json.Unmarshal(*e.RawPayload, &payload)
  100. return payload, err
  101. }
  102. // Payload returns the parsed event payload. For recognized event types,
  103. // a value of the corresponding struct type will be returned.
  104. //
  105. // Deprecated: Use ParsePayload instead, which returns an error
  106. // rather than panics if JSON unmarshaling raw payload fails.
  107. func (e *Event) Payload() (payload interface{}) {
  108. var err error
  109. payload, err = e.ParsePayload()
  110. if err != nil {
  111. panic(err)
  112. }
  113. return payload
  114. }
  115. // ListEvents drinks from the firehose of all public events across GitHub.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events
  118. func (s *ActivityService) ListEvents(ctx context.Context, opt *ListOptions) ([]*Event, *Response, error) {
  119. u, err := addOptions("events", opt)
  120. if err != nil {
  121. return nil, nil, err
  122. }
  123. req, err := s.client.NewRequest("GET", u, nil)
  124. if err != nil {
  125. return nil, nil, err
  126. }
  127. var events []*Event
  128. resp, err := s.client.Do(ctx, req, &events)
  129. if err != nil {
  130. return nil, resp, err
  131. }
  132. return events, resp, nil
  133. }
  134. // ListRepositoryEvents lists events for a repository.
  135. //
  136. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-repository-events
  137. func (s *ActivityService) ListRepositoryEvents(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
  138. u := fmt.Sprintf("repos/%v/%v/events", owner, repo)
  139. u, err := addOptions(u, opt)
  140. if err != nil {
  141. return nil, nil, err
  142. }
  143. req, err := s.client.NewRequest("GET", u, nil)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. var events []*Event
  148. resp, err := s.client.Do(ctx, req, &events)
  149. if err != nil {
  150. return nil, resp, err
  151. }
  152. return events, resp, nil
  153. }
  154. // ListIssueEventsForRepository lists issue events for a repository.
  155. //
  156. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-issue-events-for-a-repository
  157. func (s *ActivityService) ListIssueEventsForRepository(ctx context.Context, owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
  158. u := fmt.Sprintf("repos/%v/%v/issues/events", owner, repo)
  159. u, err := addOptions(u, opt)
  160. if err != nil {
  161. return nil, nil, err
  162. }
  163. req, err := s.client.NewRequest("GET", u, nil)
  164. if err != nil {
  165. return nil, nil, err
  166. }
  167. var events []*IssueEvent
  168. resp, err := s.client.Do(ctx, req, &events)
  169. if err != nil {
  170. return nil, resp, err
  171. }
  172. return events, resp, nil
  173. }
  174. // ListEventsForRepoNetwork lists public events for a network of repositories.
  175. //
  176. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events-for-a-network-of-repositories
  177. func (s *ActivityService) ListEventsForRepoNetwork(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
  178. u := fmt.Sprintf("networks/%v/%v/events", owner, repo)
  179. u, err := addOptions(u, opt)
  180. if err != nil {
  181. return nil, nil, err
  182. }
  183. req, err := s.client.NewRequest("GET", u, nil)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. var events []*Event
  188. resp, err := s.client.Do(ctx, req, &events)
  189. if err != nil {
  190. return nil, resp, err
  191. }
  192. return events, resp, nil
  193. }
  194. // ListEventsForOrganization lists public events for an organization.
  195. //
  196. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events-for-an-organization
  197. func (s *ActivityService) ListEventsForOrganization(ctx context.Context, org string, opt *ListOptions) ([]*Event, *Response, error) {
  198. u := fmt.Sprintf("orgs/%v/events", org)
  199. u, err := addOptions(u, opt)
  200. if err != nil {
  201. return nil, nil, err
  202. }
  203. req, err := s.client.NewRequest("GET", u, nil)
  204. if err != nil {
  205. return nil, nil, err
  206. }
  207. var events []*Event
  208. resp, err := s.client.Do(ctx, req, &events)
  209. if err != nil {
  210. return nil, resp, err
  211. }
  212. return events, resp, nil
  213. }
  214. // ListEventsPerformedByUser lists the events performed by a user. If publicOnly is
  215. // true, only public events will be returned.
  216. //
  217. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
  218. func (s *ActivityService) ListEventsPerformedByUser(ctx context.Context, user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
  219. var u string
  220. if publicOnly {
  221. u = fmt.Sprintf("users/%v/events/public", user)
  222. } else {
  223. u = fmt.Sprintf("users/%v/events", user)
  224. }
  225. u, err := addOptions(u, opt)
  226. if err != nil {
  227. return nil, nil, err
  228. }
  229. req, err := s.client.NewRequest("GET", u, nil)
  230. if err != nil {
  231. return nil, nil, err
  232. }
  233. var events []*Event
  234. resp, err := s.client.Do(ctx, req, &events)
  235. if err != nil {
  236. return nil, resp, err
  237. }
  238. return events, resp, nil
  239. }
  240. // ListEventsReceivedByUser lists the events received by a user. If publicOnly is
  241. // true, only public events will be returned.
  242. //
  243. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-that-a-user-has-received
  244. func (s *ActivityService) ListEventsReceivedByUser(ctx context.Context, user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
  245. var u string
  246. if publicOnly {
  247. u = fmt.Sprintf("users/%v/received_events/public", user)
  248. } else {
  249. u = fmt.Sprintf("users/%v/received_events", user)
  250. }
  251. u, err := addOptions(u, opt)
  252. if err != nil {
  253. return nil, nil, err
  254. }
  255. req, err := s.client.NewRequest("GET", u, nil)
  256. if err != nil {
  257. return nil, nil, err
  258. }
  259. var events []*Event
  260. resp, err := s.client.Do(ctx, req, &events)
  261. if err != nil {
  262. return nil, resp, err
  263. }
  264. return events, resp, nil
  265. }
  266. // ListUserEventsForOrganization provides the user’s organization dashboard. You
  267. // must be authenticated as the user to view this.
  268. //
  269. // GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-for-an-organization
  270. func (s *ActivityService) ListUserEventsForOrganization(ctx context.Context, org, user string, opt *ListOptions) ([]*Event, *Response, error) {
  271. u := fmt.Sprintf("users/%v/events/orgs/%v", user, org)
  272. u, err := addOptions(u, opt)
  273. if err != nil {
  274. return nil, nil, err
  275. }
  276. req, err := s.client.NewRequest("GET", u, nil)
  277. if err != nil {
  278. return nil, nil, err
  279. }
  280. var events []*Event
  281. resp, err := s.client.Do(ctx, req, &events)
  282. if err != nil {
  283. return nil, resp, err
  284. }
  285. return events, resp, nil
  286. }