Skip to content

Commit

Permalink
Merge pull request octokit#760 from octokit/reactions
Browse files Browse the repository at this point in the history
Add Reactions Preview to Octokit
  • Loading branch information
pengwynn authored Jun 26, 2016
2 parents 29918c0 + 48864e7 commit 7404385
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/octokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
require 'octokit/client/pub_sub_hubbub'
require 'octokit/client/pull_requests'
require 'octokit/client/rate_limit'
require 'octokit/client/reactions'
require 'octokit/client/refs'
require 'octokit/client/releases'
require 'octokit/client/repositories'
Expand Down Expand Up @@ -85,6 +86,7 @@ class Client
include Octokit::Client::PubSubHubbub
include Octokit::Client::PullRequests
include Octokit::Client::RateLimit
include Octokit::Client::Reactions
include Octokit::Client::Refs
include Octokit::Client::Releases
include Octokit::Client::Repositories
Expand Down
158 changes: 158 additions & 0 deletions lib/octokit/client/reactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
module Octokit
class Client

# Methods for the Reacions API
#
# @see https://developer.github.com/v3/reactions/
module Reactions

# List reactions for a commit comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The id of the commit comment
# @see https://developer.github.com/v3/reactions/#list-reactions-for-a-commit-comment
#
# @example
# @client.commit_comment_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def commit_comment_reactions(repo, id, options = {})
options = ensure_api_media_type(:reactions, options)
get "#{Repository.path repo}/comments/#{id}/reactions", options
end

# Create a reaction for a commit comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The id of the commit comment
# @param reaction [String] The Reaction
# @see https://developer.github.com/v3/reactions/#create-reaction-for-a-commit-comment
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_commit_comment_reactions("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hash representing the reaction
def create_commit_comment_reaction(repo, id, reaction, options = {})
options = ensure_api_media_type(:reactions, options.merge(:content => reaction))
post "#{Repository.path repo}/comments/#{id}/reactions", options
end

# List reactions for an issue
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param number [Integer] The Issue number
# @see https://developer.github.com/v3/reactions/#list-reactions-for-an-issue
#
# @example
# @client.issue_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def issue_reactions(repo, number, options = {})
options = ensure_api_media_type(:reactions, options)
get "#{Repository.path repo}/issues/#{number}/reactions", options
end

# Create reaction for an issue
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param number [Integer] The Issue number
# @param reaction [String] The Reaction
#
# @see https://developer.github.com/v3/reactions/#create-reaction-for-an-issue
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_issue_reaction("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hash representing the reaction.
def create_issue_reaction(repo, number, reaction, options = {})
options = ensure_api_media_type(:reactions, options.merge(:content => reaction))
post "#{Repository.path repo}/issues/#{number}/reactions", options
end

# List reactions for an issue comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
#
# @see https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
#
# @example
# @client.issue_comment_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def issue_comment_reactions(repo, id, options = {})
options = ensure_api_media_type(:reactions, options)
get "#{Repository.path repo}/issues/comments/#{id}/reactions", options
end

# Create reaction for an issue comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
# @param reaction [String] The Reaction
#
# @see https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_issue_comment_reaction("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hashes representing the reaction.
def create_issue_comment_reaction(repo, id, reaction, options = {})
options = ensure_api_media_type(:reactions, options.merge(:content => reaction))
post "#{Repository.path repo}/issues/comments/#{id}/reactions", options
end

# List reactions for a pull request review comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
#
# @see https://developer.github.com/v3/reactions/#list-reactions-for-a-pull-request-review-comment
#
# @example
# @client.pull_request_review_comment_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def pull_request_review_comment_reactions(repo, id, options = {})
options = ensure_api_media_type(:reactions, options)
get "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
end

# Create reaction for a pull request review comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
# @param reaction [String] The Reaction
#
# @see https://developer.github.com/v3/reactions/#create-reaction-for-a-pull-request-review-comment
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_pull_request_reiew_comment_reaction("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hash representing the reaction.
def create_pull_request_review_comment_reaction(repo, id, reaction, options = {})
options = ensure_api_media_type(:reactions, options.merge(:content => reaction))
post "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
end

# Delete a reaction
#
# @param id [Integer] Reaction id
#
# @see https://developer.github.com/v3/reactions/#delete-a-reaction
#
# @example
# @client.delete_reaction(1)
#
# @return [Boolean] Return true if reaction was deleted, false otherwise.
def delete_reaction(id, options = {})
options = ensure_api_media_type(:reactions, options)
boolean_from_response :delete, "reactions/#{id}", options
end
end
end
end
1 change: 1 addition & 0 deletions lib/octokit/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Preview
:migrations => 'application/vnd.github.wyandotte-preview+json'.freeze,
:licenses => 'application/vnd.github.drax-preview+json'.freeze,
:source_imports => 'application/vnd.github.barred-rock-preview'.freeze,
:reactions => 'application/vnd.github.squirrel-girl-preview'.freeze,
}

def ensure_api_media_type(type, options)
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions spec/octokit/client/reactions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
require 'helper'

describe Octokit::Client::Reactions do
before do
Octokit.reset!
@client = oauth_client
end

context "with repository", :vcr do
before(:each) do
@repo = @client.create_repository("an-repo", :auto_init => true)
end

after(:each) do
begin
@client.delete_repository(@repo.full_name)
rescue Octokit::NotFound
end
end

context "with commit comment" do
before do
@commit = @client.commits(@repo.full_name).last.rels[:self].get.data
@commit_comment = @client.create_commit_comment \
@repo.full_name,
@commit.sha, ":metal:\n:sparkles:\n:cake:",
@commit.files.last.filename
end

describe ".commit_comment_reactions" do
it "returns an Array of reactions" do
reactions = @client.commit_comment_reactions(@repo.full_name, @commit_comment.id)
expect(reactions).to be_kind_of Array
assert_requested :get, github_url("/repos/#{@repo.full_name}/comments/#{@commit_comment.id}/reactions")
end
end # .commit_comment_reactions

describe ".create_commit_comment_reaction" do
it "creates a reaction" do
reaction = @client.create_commit_comment_reaction(@repo.full_name, @commit_comment.id, "+1")
expect(reaction.content).to eql("+1")
assert_requested :post, github_url("/repos/#{@repo.full_name}/comments/#{@commit_comment.id}/reactions")
end
end # .create_commit_comment_reaction
end # with commit comment

context "with issue", :vcr do
before do
@issue = @client.create_issue(@repo.full_name, "Migrate issues to v3", "Move all Issues calls to v3 of the API")
end

describe ".issue_reactions" do
it "returns an Array of reactions" do
reactions = @client.issue_reactions(@repo.full_name, @issue.number)
expect(reactions).to be_kind_of Array
assert_requested :get, github_url("/repos/#{@repo.full_name}/issues/#{@issue.number}/reactions")
end
end # .issue_reactions

describe ".create_issue_reaction" do
it "creates a reaction" do
reaction = @client.create_issue_reaction(@repo.full_name, @issue.number, "+1")
expect(reaction.content).to eql("+1")
assert_requested :post, github_url("/repos/#{@repo.full_name}/issues/#{@issue.number}/reactions")
end
end # .create_issue_reaction

context "with issue comment" do
before do
@issue_comment = @client.add_comment(@repo.full_name, @issue.number, "Another test comment")
end

describe ".issue_comment_reactions" do
it "returns an Array of reactions" do
reactions = @client.issue_comment_reactions(@repo.full_name, @issue_comment.id)
expect(reactions).to be_kind_of Array
assert_requested :get, github_url("/repos/#{@repo.full_name}/issues/comments/#{@issue_comment.id}/reactions")
end
end # .issue_commit_comment_reactions

describe ".create_issue_comment_reaction" do
it "creates a reaction" do
reaction = @client.create_issue_comment_reaction(@repo.full_name, @issue_comment.id, "+1")
expect(reaction.content).to eql("+1")
assert_requested :post, github_url("/repos/#{@repo.full_name}/issues/comments/#{@issue_comment.id}/reactions")
end
end # .create_issue_comment_reaction
end # with issue comment

context "with reaction" do
before do
@reaction = @client.create_issue_reaction(@repo.full_name, @issue.number, "+1")
end

describe ".delete_reaction" do
it "deletes the reaction" do
@client.delete_reaction(@reaction.id)
assert_requested :delete, github_url("/reactions/#{@reaction.id}")
end
end # .delete_reaction
end # with reaction
end # with issue

context "with pull request" do
before do
master_ref = @client.ref(@repo.full_name, "heads/master")
@client.create_ref(@repo.full_name, "heads/branch-for-pr", master_ref.object.sha)
@content = @client.create_contents(@repo.full_name, "lib/test.txt", "Adding content", "File Content", :branch => "branch-for-pr")

args = [@repo.full_name, "master", "branch-for-pr", "A new PR", "The Body"]
@pull = @client.create_pull_request(*args)
end

context "with pull request review comment" do
before do
new_comment = {
:body => "Looks good!",
:commit_id => @content.commit.sha,
:path => "lib/test.txt",
:position => 1
}

@pull_request_review_comment = @client.create_pull_request_comment \
@repo.full_name,
@pull.number,
new_comment[:body],
new_comment[:commit_id],
new_comment[:path],
new_comment[:position]
end

describe ".pull_request_review_comment_reactions" do
it "returns an Array of reactions" do
reactions = @client.pull_request_review_comment_reactions(@repo.full_name, @pull_request_review_comment.id)
expect(reactions).to be_kind_of Array
assert_requested :get, github_url("/repos/#{@repo.full_name}/pulls/comments/#{@pull_request_review_comment.id}/reactions")
end
end # .pull_request_review_comment_reactions

describe ".create_pull_request_review_comment_reaction" do
it "creates a reaction" do
reaction = @client.create_pull_request_review_comment_reaction(@repo.full_name, @pull_request_review_comment.id, "+1")
expect(reaction.content).to eql("+1")
assert_requested :post, github_url("/repos/#{@repo.full_name}/pulls/comments/#{@pull_request_review_comment.id}/reactions")
end
end # .create_pull_request_review_comment_reaction
end # with pull request review comment
end # with pull request
end # with repository
end

0 comments on commit 7404385

Please sign in to comment.