Skip to content

Commit

Permalink
Implement new watchers api as subscriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyw committed Oct 27, 2012
1 parent 44a8c8b commit 97d2c8e
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/octokit/client/repositories.rb
Expand Up @@ -613,6 +613,74 @@ def repository_assignees(repo, options={})
get "repos/#{Repository.new repo}/assignees", options, 3 get "repos/#{Repository.new repo}/assignees", options, 3
end end
alias :repo_assignees :repository_assignees alias :repo_assignees :repository_assignees

# List watchers subscribing to notifications for a repo
#
# @param repo [String, Hash, Repository] A GitHub repository.
#
# @return [Array] Array of users watching.
#
# @see http://developer.github.com/v3/activity/watching/#list-watchers
#
# @example
# @client.subscribers("pengwynn/octokit")
def subscribers(repo, options={})
get("repos/#{Repository.new repo}/subscribers", options, 3)
end

# Get a repository subscription
#
# @param repo [String, Hash, Repository] A GitHub repository.
#
# @return [Hashie::Mash] Repository subscription.
#
# @see http://developer.github.com/v3/activity/watching/#get-a-repository-subscription
#
# @example
# @client.subscription("pengwynn/octokit")
def subscription(repo, options={})
get("repos/#{Repository.new repo}/subscription", options, 3)
end

# Update repository subscription
#
# @param repo [String, Hash, Repository] A GitHub repository.
# @param options [Hash]
#
# @option options [Boolean] :subscribed Determines if notifications
# should be received from this repository.
#
# @option options [Boolean] :ignored Deterimines if all notifications
# should be blocked from this repository.
#
# @return [Hashie::Mash] Updated repository subscription.
#
# @see http://developer.github.com/v3/activity/watching/#set-a-repository-subscription
#
# @example Subscribe to notifications for a repository
# @client.update_subscription("pengwynn/octokit", {subscribed: true})
def update_subscription(repo, options={})
put("repos/#{Repository.new repo}/subscription", options, 3)
end

# Delete a repository subscription
#
# @param repo [String, Hash, Repository] A GitHub repository.
#
# @return [Boolean] True if subscription deleted, false otherwise.
#
# @see http://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
#
# @example
# @client.delete_subscription("pengwynn/octokit")
def delete_subscription(repo, options={})
begin
delete("repos/#{Repository.new repo}/subscription", options, 3, true, true).status == 204
rescue
false
end
end

end end
end end
end end
15 changes: 15 additions & 0 deletions lib/octokit/client/users.rb
Expand Up @@ -282,6 +282,21 @@ def add_email(email, options={})
def remove_email(email, options={}) def remove_email(email, options={})
delete("user/emails", options.merge({:email => email}), 3, true, raw=true).status == 204 delete("user/emails", options.merge({:email => email}), 3, true, raw=true).status == 204
end end

# List repositories being watched by a user.
#
# @param user [String] User's GitHub username.
#
# @return [Array<Hashie::Mashie>] Array of repositories.
#
# @see http://developer.github.com/v3/activity/watching/#list-repositories-being-watched
#
# @example
# @client.subscriptions("pengwynn")
def subscriptions(user=login, options={})
get("users/#{user}/subscriptions", options, 3)
end

end end
end end
end end
9 changes: 9 additions & 0 deletions spec/fixtures/v3/subscribers.json
@@ -0,0 +1,9 @@
[
{
"url": "https://api.github.com/users/pengwynn",
"login": "pengwynn",
"avatar_url": "https://secure.gravatar.com/avatar/7e19cd5486b5d6dc1ef90e671ba52ae0?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
"gravatar_id": "7e19cd5486b5d6dc1ef90e671ba52ae0",
"id": 865
}
]
8 changes: 8 additions & 0 deletions spec/fixtures/v3/subscription.json
@@ -0,0 +1,8 @@
{
"url": "https://api.github.com/repos/pengwynn/octokit/subscription",
"reason": null,
"created_at": "2012-10-13T02:21:53Z",
"subscribed": true,
"repository_url": "https://api.github.com/repos/pengwynn/octokit",
"ignored": false
}
8 changes: 8 additions & 0 deletions spec/fixtures/v3/subscription_update.json
@@ -0,0 +1,8 @@
{
"url": "https://api.github.com/repos/pengwynn/octokit/subscription",
"reason": null,
"created_at": "2012-10-13T02:21:53Z",
"subscribed": false,
"repository_url": "https://api.github.com/repos/pengwynn/octokit",
"ignored": false
}
39 changes: 39 additions & 0 deletions spec/fixtures/v3/subscriptions.json
@@ -0,0 +1,39 @@
[
{
"watchers": 12,
"url": "https://api.github.com/repos/pengwynn/ujs_sort_helper",
"svn_url": "https://github.com/pengwynn/ujs_sort_helper",
"watchers_count": 12,
"owner": {
"url": "https://api.github.com/users/pengwynn",
"avatar_url": "https://secure.gravatar.com/avatar/7e19cd5486b5d6dc1ef90e671ba52ae0?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
"gravatar_id": "7e19cd5486b5d6dc1ef90e671ba52ae0",
"login": "pengwynn",
"id": 865
},
"language": "JavaScript",
"has_wiki": true,
"description": "Unobtrusive sort helper for Rails",
"clone_url": "https://github.com/pengwynn/ujs_sort_helper.git",
"open_issues_count": 1,
"updated_at": "2012-09-10T04:46:49Z",
"html_url": "https://github.com/pengwynn/ujs_sort_helper",
"open_issues": 1,
"ssh_url": "git@github.com:pengwynn/ujs_sort_helper.git",
"size": 140,
"fork": false,
"pushed_at": "2011-10-14T13:14:06Z",
"full_name": "pengwynn/ujs_sort_helper",
"name": "ujs_sort_helper",
"forks": 8,
"has_issues": true,
"has_downloads": true,
"forks_count": 8,
"mirror_url": null,
"private": false,
"id": 11560,
"created_at": "2008-04-21T15:55:02Z",
"git_url": "git://github.com/pengwynn/ujs_sort_helper.git",
"homepage": ""
}
]
52 changes: 52 additions & 0 deletions spec/octokit/client/repositories_spec.rb
Expand Up @@ -447,4 +447,56 @@


end end


describe ".subscribers" do

it "lists all the users watching the repository" do
stub_get("/repos/pengwynn/octokit/subscribers").
to_return(:body => fixture("v3/subscribers.json"))
subscribers = @client.subscribers("pengwynn/octokit")
expect(subscribers.first.id).to eq(865)
expect(subscribers.first.login).to eq("pengwynn")
end

end

describe ".subscription" do

it "returns a repository subscription" do
stub_get("/repos/pengwynn/octokit/subscription").
to_return(:body => fixture("v3/subscription.json"))
subscription = @client.subscription("pengwynn/octokit")
expect(subscription.subscribed).to be_true
end

end

describe ".update_subscription" do

it "updates a repository subscription" do
stub_put("/repos/pengwynn/octokit/subscription").
to_return(:body => fixture("v3/subscription_update.json"))
subscription = @client.update_subscription("pengwynn/octokit", :subscribed => false)
expect(subscription.subscribed).to be_false
end

end

describe ".delete_subscription" do

it "returns true when repo subscription deleted" do
stub_delete("/repos/pengwynn/octokit/subscription").
to_return(:status => 204)
result = @client.delete_subscription("pengwynn/octokit")
expect(result).to be_true
end

it "returns false when delete repo subscription fails" do
stub_delete("/repos/pengwynn/octokit/subscription").
to_return(:status => 500)
result = @client.delete_subscription("pengwynn/octokit")
expect(result).to be_false
end

end

end end
11 changes: 11 additions & 0 deletions spec/octokit/client/users_spec.rb
Expand Up @@ -340,4 +340,15 @@


end end


describe ".subscriptions" do

it "returns the repositories the user watches for notifications" do
stub_get("https://api.github.com/users/pengwynn/subscriptions").
to_return(:body => fixture("v3/subscriptions.json"))
subscriptions = @client.subscriptions("pengwynn")
expect(subscriptions.first.id).to eq(11560)
expect(subscriptions.first.name).to eq("ujs_sort_helper")
end
end

end end

0 comments on commit 97d2c8e

Please sign in to comment.