From b347348d7ac5d2141e718805982999f85c87d18e Mon Sep 17 00:00:00 2001 From: rc1140 Date: Tue, 16 Jul 2013 22:29:54 +0200 Subject: [PATCH] Added ListStarred and ListLanguages functions Repository service: ListLanguages : Allows a user to list the languages for a repository Activity service: ListStarred : Allows a user to list the starred repositories for a user --- github/activity.go | 64 +++++++++++++++++++++++++++++++++++++++++ github/activity_test.go | 59 +++++++++++++++++++++++++++++++++++++ github/github.go | 2 ++ github/repos.go | 22 ++++++++++++++ github/repos_test.go | 21 +++++++++++++- 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 github/activity.go create mode 100644 github/activity_test.go diff --git a/github/activity.go b/github/activity.go new file mode 100644 index 0000000..491f9e7 --- /dev/null +++ b/github/activity.go @@ -0,0 +1,64 @@ +// 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/url" + "strconv" +) + +// ActivityService handles communication with the activity related +// methods of the GitHub API. +// +// GitHub API docs: http://developer.github.com/v3/users/ +type ActivityService struct { + client *Client +} + +// ActivityListStarredOptions specifies the optional parameters to the +// ActivityService.ListStarred method. +type ActivityListStarredOptions struct { + // How to sort the repository list. Possible values are: created, updated, + // pushed, full_name. Default is "full_name". + Sort string + + // Direction in which to sort repositories. Possible values are: asc, desc. + // Default is "asc" when sort is "full_name", otherwise default is "desc". + Direction string + + // For paginated result sets, page of results to retrieve. + Page int +} + +// ListStarred lists all the repos starred by a user. Passing the empty string +// will list the starred repositories for the authenticated user. +// +// GitHub API docs: http://developer.github.com/v3/activity/starring/#list-repositories-being-starred +func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptions) ([]Repository, error) { + var u string + if user != "" { + u = fmt.Sprintf("users/%v/starred", user) + } else { + u = "user/starred" + } + if opt != nil { + params := url.Values{ + "sort": []string{opt.Sort}, + "direction": []string{opt.Direction}, + "page": []string{strconv.Itoa(opt.Page)}, + } + u += "?" + params.Encode() + } + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, err + } + + repos := new([]Repository) + _, err = s.client.Do(req, repos) + return *repos, err +} diff --git a/github/activity_test.go b/github/activity_test.go new file mode 100644 index 0000000..1db9635 --- /dev/null +++ b/github/activity_test.go @@ -0,0 +1,59 @@ +// 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 TestActivityService_ListStarred_authenticatedUser(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"id":1}]`) + }) + + repos, err := client.Activity.ListStarred("", nil) + if err != nil { + t.Errorf("Activity.ListStarred returned error: %v", err) + } + + want := []Repository{Repository{ID: 1}} + if !reflect.DeepEqual(repos, want) { + t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want) + } +} + +func TestActivityService_ListStarred_specifiedUser(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/users/u/starred", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{ + "sort": "created", + "direction": "asc", + "page": "2", + }) + fmt.Fprint(w, `[{"id":2}]`) + }) + + opt := &ActivityListStarredOptions{"created", "asc", 2} + repos, err := client.Activity.ListStarred("u", opt) + if err != nil { + t.Errorf("Activity.ListStarred returned error: %v", err) + } + + want := []Repository{Repository{ID: 2}} + if !reflect.DeepEqual(repos, want) { + t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want) + } +} diff --git a/github/github.go b/github/github.go index 92666b0..288871b 100644 --- a/github/github.go +++ b/github/github.go @@ -95,6 +95,7 @@ type Client struct { Git *GitService Users *UsersService Gists *GistsService + Activity *ActivityService } // ListOptions specifies the optional parameters to various List methods that @@ -123,6 +124,7 @@ func NewClient(httpClient *http.Client) *Client { c.Git = &GitService{client: c} c.Users = &UsersService{client: c} c.Gists = &GistsService{client: c} + c.Activity = &ActivityService{client: c} return c } diff --git a/github/repos.go b/github/repos.go index 9fcd372..07c7aca 100644 --- a/github/repos.go +++ b/github/repos.go @@ -309,3 +309,25 @@ func (s *RepositoriesService) CreateStatus(owner, repo, ref string, status *Repo _, err = s.client.Do(req, statuses) return statuses, err } + +// ListLanguages lists languages for the specified repository. The returned map +// specifies the languages and the number of bytes of code written in that +// language. For example: +// +// { +// "C": 78769, +// "Python": 7769 +// } +// +// GitHub API Docs: http://developer.github.com/v3/repos/#list-languages +func (s *RepositoriesService) ListLanguages(owner string, repository string) (map[string]int, error) { + u := fmt.Sprintf("/repos/%v/%v/languages", owner, repository) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, err + } + + languages := make(map[string]int) + _, err = s.client.Do(req, &languages) + return languages, err +} diff --git a/github/repos_test.go b/github/repos_test.go index 8b7c8fa..7a30a1d 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -240,7 +240,6 @@ func TestRepositoriesService_Edit_invalidOwner(t *testing.T) { testURLParseError(t, err) } - func TestRepositoriesService_ListForks(t *testing.T) { setup() defer teardown() @@ -352,3 +351,23 @@ func TestRepositoriesService_CreateStatus_invalidOwner(t *testing.T) { _, err := client.Repositories.CreateStatus("%", "r", "r", nil) testURLParseError(t, err) } + +func TestRepositoriesService_ListLanguages(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/u/r/languages", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"go":1}`) + }) + + languages, err := client.Repositories.ListLanguages("u", "r") + if err != nil { + t.Errorf("Repositories.ListLanguages returned error: %v", err) + } + + want := map[string]int{"go": 1} + if !reflect.DeepEqual(languages, want) { + t.Errorf("Repositories.ListLanguages returned %+v, want %+v", languages, want) + } +}