Browse Source

make room for thread subscriptions

So it appears GitHub already supports multiple types of subscriptions:
repositories and threads.  I'm leaving the data type simply as
'Subscription', but am updating the method names to avoid confusion,
since they're both part of the ActivitiesService.
Will Norris 12 years ago
parent
commit
51e9d946b9
3 changed files with 60 additions and 44 deletions
  1. +27
    -17
      github/activity_watching.go
  2. +23
    -17
      github/activity_watching_test.go
  3. +10
    -10
      tests/integration/activity_test.go

+ 27
- 17
github/activity_watching.go View File

@ -1,15 +1,25 @@
// Copyright 2014 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 package github
import "fmt" import "fmt"
// Subscription identifies a repository subscription.
// Subscription identifies a repository or thread subscription.
type Subscription struct { type Subscription struct {
Subscribed *bool `json:"subscribed,omitempty"`
Ignored *bool `json:"ignored,omitempty"`
Reason *string `json:"reason,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
URL *string `json:"url,omitempty"`
RepositoryURL *string `json:"repository_url,omitempty"`
Subscribed *bool `json:"subscribed,omitempty"`
Ignored *bool `json:"ignored,omitempty"`
Reason *string `json:"reason,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
URL *string `json:"url,omitempty"`
// only populated for repository subscriptions
RepositoryURL *string `json:"repository_url,omitempty"`
// only populated for thread subscriptions
ThreadURL *string `json:"thread_url,omitempty"`
} }
// ListWatchers lists watchers of a particular repo. // ListWatchers lists watchers of a particular repo.
@ -61,12 +71,12 @@ func (s *ActivityService) ListWatched(user string) ([]Repository, *Response, err
return *watched, resp, err return *watched, resp, err
} }
// GetSubscription returns the subscription for the specified repository for
// the authenticated user. If the authenticated user is not watching the
// repository, a nil Subscription is returned.
// GetRepositorySubscription returns the subscription for the specified
// repository for the authenticated user. If the authenticated user is not
// watching the repository, a nil Subscription is returned.
// //
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#get-a-repository-subscription // GitHub API Docs: https://developer.github.com/v3/activity/watching/#get-a-repository-subscription
func (s *ActivityService) GetSubscription(owner, repo string) (*Subscription, *Response, error) {
func (s *ActivityService) GetRepositorySubscription(owner, repo string) (*Subscription, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo) u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
req, err := s.client.NewRequest("GET", u, nil) req, err := s.client.NewRequest("GET", u, nil)
@ -85,11 +95,11 @@ func (s *ActivityService) GetSubscription(owner, repo string) (*Subscription, *R
return sub, resp, err return sub, resp, err
} }
// SetSubscription sets the subscription for the specified repository for the
// authenticated user.
// SetRepositorySubscription sets the subscription for the specified repository
// for the authenticated user.
// //
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#set-a-repository-subscription // GitHub API Docs: https://developer.github.com/v3/activity/watching/#set-a-repository-subscription
func (s *ActivityService) SetSubscription(owner, repo string, subscription *Subscription) (*Subscription, *Response, error) {
func (s *ActivityService) SetRepositorySubscription(owner, repo string, subscription *Subscription) (*Subscription, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo) u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
req, err := s.client.NewRequest("PUT", u, subscription) req, err := s.client.NewRequest("PUT", u, subscription)
@ -106,11 +116,11 @@ func (s *ActivityService) SetSubscription(owner, repo string, subscription *Subs
return sub, resp, err return sub, resp, err
} }
// DeleteSubscription deletes the subscription for the specified repository for
// the authenticated user.
// DeleteRepositorySubscription deletes the subscription for the specified
// repository for the authenticated user.
// //
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription // GitHub API Docs: https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
func (s *ActivityService) DeleteSubscription(owner, repo string) (*Response, error) {
func (s *ActivityService) DeleteRepositorySubscription(owner, repo string) (*Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo) u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
req, err := s.client.NewRequest("DELETE", u, nil) req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil { if err != nil {


+ 23
- 17
github/activity_watching_test.go View File

@ -1,3 +1,8 @@
// Copyright 2014 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 package github
import ( import (
@ -72,7 +77,7 @@ func TestActivityService_ListWatched_specifiedUser(t *testing.T) {
} }
} }
func TestActivityService_GetSubscription_true(t *testing.T) {
func TestActivityService_GetRepositorySubscription_true(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@ -81,18 +86,18 @@ func TestActivityService_GetSubscription_true(t *testing.T) {
fmt.Fprint(w, `{"subscribed":true}`) fmt.Fprint(w, `{"subscribed":true}`)
}) })
sub, _, err := client.Activity.GetSubscription("o", "r")
sub, _, err := client.Activity.GetRepositorySubscription("o", "r")
if err != nil { if err != nil {
t.Errorf("Activity.GetSubscription returned error: %v", err)
t.Errorf("Activity.GetRepositorySubscription returned error: %v", err)
} }
want := &Subscription{Subscribed: Bool(true)} want := &Subscription{Subscribed: Bool(true)}
if !reflect.DeepEqual(sub, want) { if !reflect.DeepEqual(sub, want) {
t.Errorf("Activity.GetSubscription returned %+v, want %+v", sub, want)
t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want)
} }
} }
func TestActivityService_GetSubscription_false(t *testing.T) {
func TestActivityService_GetRepositorySubscription_false(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@ -101,18 +106,18 @@ func TestActivityService_GetSubscription_false(t *testing.T) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
}) })
sub, _, err := client.Activity.GetSubscription("o", "r")
sub, _, err := client.Activity.GetRepositorySubscription("o", "r")
if err != nil { if err != nil {
t.Errorf("Activity.GetSubscription returned error: %v", err)
t.Errorf("Activity.GetRepositorySubscription returned error: %v", err)
} }
var want *Subscription var want *Subscription
if !reflect.DeepEqual(sub, want) { if !reflect.DeepEqual(sub, want) {
t.Errorf("Activity.GetSubscription returned %+v, want %+v", sub, want)
t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want)
} }
} }
func TestActivityService_GetSubscription_error(t *testing.T) {
func TestActivityService_GetRepositorySubscription_error(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@ -121,13 +126,13 @@ func TestActivityService_GetSubscription_error(t *testing.T) {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
}) })
_, _, err := client.Activity.GetSubscription("o", "r")
_, _, err := client.Activity.GetRepositorySubscription("o", "r")
if err == nil { if err == nil {
t.Errorf("Expected HTTP 400 response") t.Errorf("Expected HTTP 400 response")
} }
} }
func TestActivityService_SetSubscription(t *testing.T) {
func TestActivityService_SetRepositorySubscription(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@ -145,27 +150,28 @@ func TestActivityService_SetSubscription(t *testing.T) {
fmt.Fprint(w, `{"ignored":true}`) fmt.Fprint(w, `{"ignored":true}`)
}) })
sub, _, err := client.Activity.SetSubscription("o", "r", input)
sub, _, err := client.Activity.SetRepositorySubscription("o", "r", input)
if err != nil { if err != nil {
t.Errorf("Activity.SetSubscription returned error: %v", err)
t.Errorf("Activity.SetRepositorySubscription returned error: %v", err)
} }
want := &Subscription{Ignored: Bool(true)} want := &Subscription{Ignored: Bool(true)}
if !reflect.DeepEqual(sub, want) { if !reflect.DeepEqual(sub, want) {
t.Errorf("Activity.SetSubscription returned %+v, want %+v", sub, want)
t.Errorf("Activity.SetRepositorySubscription returned %+v, want %+v", sub, want)
} }
} }
func TestActivityService_DeleteSubscription(t *testing.T) {
func TestActivityService_DeleteRepositorySubscription(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
}) })
_, err := client.Activity.DeleteSubscription("o", "r")
_, err := client.Activity.DeleteRepositorySubscription("o", "r")
if err != nil { if err != nil {
t.Errorf("Activity.DeleteSubscription returned error: %v", err)
t.Errorf("Activity.DeleteRepositorySubscription returned error: %v", err)
} }
} }

+ 10
- 10
tests/integration/activity_test.go View File

@ -82,9 +82,9 @@ func TestActivity_Watching(t *testing.T) {
} }
// first, check if already watching google/go-github // first, check if already watching google/go-github
sub, _, err := client.Activity.GetSubscription("google", "go-github")
sub, _, err := client.Activity.GetRepositorySubscription("google", "go-github")
if err != nil { if err != nil {
t.Fatalf("Activity.GetSubscription returned error: %v", err)
t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err)
} }
if sub != nil { if sub != nil {
t.Fatalf("Already watching google/go-github. Please manually stop watching it first.") t.Fatalf("Already watching google/go-github. Please manually stop watching it first.")
@ -92,30 +92,30 @@ func TestActivity_Watching(t *testing.T) {
// watch google/go-github // watch google/go-github
sub = &github.Subscription{Subscribed: github.Bool(true)} sub = &github.Subscription{Subscribed: github.Bool(true)}
_, _, err = client.Activity.SetSubscription("google", "go-github", sub)
_, _, err = client.Activity.SetRepositorySubscription("google", "go-github", sub)
if err != nil { if err != nil {
t.Fatalf("Activity.SetSubscription returned error: %v", err)
t.Fatalf("Activity.SetRepositorySubscription returned error: %v", err)
} }
// check again and verify watching // check again and verify watching
sub, _, err = client.Activity.GetSubscription("google", "go-github")
sub, _, err = client.Activity.GetRepositorySubscription("google", "go-github")
if err != nil { if err != nil {
t.Fatalf("Activity.GetSubscription returned error: %v", err)
t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err)
} }
if sub == nil || !*sub.Subscribed { if sub == nil || !*sub.Subscribed {
t.Fatalf("Not watching google/go-github after setting subscription.") t.Fatalf("Not watching google/go-github after setting subscription.")
} }
// delete subscription // delete subscription
_, err = client.Activity.DeleteSubscription("google", "go-github")
_, err = client.Activity.DeleteRepositorySubscription("google", "go-github")
if err != nil { if err != nil {
t.Fatalf("Activity.DeleteSubscription returned error: %v", err)
t.Fatalf("Activity.DeleteRepositorySubscription returned error: %v", err)
} }
// check again and verify not watching // check again and verify not watching
sub, _, err = client.Activity.GetSubscription("google", "go-github")
sub, _, err = client.Activity.GetRepositorySubscription("google", "go-github")
if err != nil { if err != nil {
t.Fatalf("Activity.GetSubscription returned error: %v", err)
t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err)
} }
if sub != nil { if sub != nil {
t.Fatalf("Still watching google/go-github after deleting subscription.") t.Fatalf("Still watching google/go-github after deleting subscription.")


Loading…
Cancel
Save