diff --git a/github/events.go b/github/events.go new file mode 100644 index 0000000..43e6137 --- /dev/null +++ b/github/events.go @@ -0,0 +1,63 @@ +// Copyright 2013 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "encoding/json" + "fmt" + "net/url" + "strconv" + "time" +) + +// EventsService provides access to the event related functions +// in the GitHub API. +// +// GitHub API docs: http://developer.github.com/v3/activity/events/ +type EventsService struct { + client *Client +} + +// Event represents a GitHub event. +type Event struct { + Type string `json:"type,omitempty"` + Public bool `json:"public"` + Payload json.RawMessage `json:"payload,omitempty"` + Repo *Repository `json:"repo,omitempty"` + Actor *User `json:"actor,omitempty"` + Org *Organization `json:"org,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + ID string `json:"id,omitempty"` +} + +// ListPerformedByUser lists the events performed by a user. If publicOnly is +// true, only public events will be returned. +// +// GitHub API docs: http://developer.github.com/v3/activity/events/#list-events-performed-by-a-user +func (s *EventsService) ListPerformedByUser(user string, publicOnly bool, opt *ListOptions) ([]Event, error) { + var u string + if publicOnly { + u = fmt.Sprintf("users/%v/events/public", user) + } else { + u = fmt.Sprintf("users/%v/events", user) + } + + if opt != nil { + params := url.Values{ + "page": []string{strconv.Itoa(opt.Page)}, + } + u += "?" + params.Encode() + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, err + } + + events := new([]Event) + _, err = s.client.Do(req, events) + return *events, err +} diff --git a/github/events_test.go b/github/events_test.go new file mode 100644 index 0000000..86e25b9 --- /dev/null +++ b/github/events_test.go @@ -0,0 +1,57 @@ +// Copyright 2013 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestEventsService_ListPerformedByUser_all(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/users/u/events", func(w http.ResponseWriter, r *http.Request) { + if m := "GET"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`) + }) + + events, err := client.Events.ListPerformedByUser("u", false, nil) + if err != nil { + t.Errorf("Events.ListPerformedByUser returned error: %v", err) + } + + want := []Event{Event{ID: "1"}, Event{ID: "2"}} + if !reflect.DeepEqual(events, want) { + t.Errorf("Events.ListPerformedByUser returned %+v, want %+v", events, want) + } +} + +func TestEventsService_ListPerformedByUser_publicOnly(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/users/u/events/public", func(w http.ResponseWriter, r *http.Request) { + if m := "GET"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`) + }) + + events, err := client.Events.ListPerformedByUser("u", true, nil) + if err != nil { + t.Errorf("Events.ListPerformedByUser returned error: %v", err) + } + + want := []Event{Event{ID: "1"}, Event{ID: "2"}} + if !reflect.DeepEqual(events, want) { + t.Errorf("Events.ListPerformedByUser returned %+v, want %+v", events, want) + } +} diff --git a/github/github.go b/github/github.go index d5d006c..5737640 100644 --- a/github/github.go +++ b/github/github.go @@ -87,6 +87,7 @@ type Client struct { // Services used for talking to different parts of the API + Events *EventsService Issues *IssuesService Organizations *OrganizationsService PullRequests *PullRequestsService @@ -114,6 +115,7 @@ func NewClient(httpClient *http.Client) *Client { baseURL, _ := url.Parse(defaultBaseURL) c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent} + c.Events = &EventsService{client: c} c.Issues = &IssuesService{client: c} c.Organizations = &OrganizationsService{client: c} c.PullRequests = &PullRequestsService{client: c}