Browse Source

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
rc1140 13 years ago
committed by Will Norris
parent
commit
b347348d7a
5 changed files with 167 additions and 1 deletions
  1. +64
    -0
      github/activity.go
  2. +59
    -0
      github/activity_test.go
  3. +2
    -0
      github/github.go
  4. +22
    -0
      github/repos.go
  5. +20
    -1
      github/repos_test.go

+ 64
- 0
github/activity.go View File

@ -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
}

+ 59
- 0
github/activity_test.go View File

@ -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)
}
}

+ 2
- 0
github/github.go View File

@ -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
}


+ 22
- 0
github/repos.go View File

@ -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
}

+ 20
- 1
github/repos_test.go View File

@ -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)
}
}

Loading…
Cancel
Save