diff --git a/lib/octokit/client.rb b/lib/octokit/client.rb index cfe0d8a47..382e79177 100644 --- a/lib/octokit/client.rb +++ b/lib/octokit/client.rb @@ -21,6 +21,7 @@ require 'octokit/client/users' require 'octokit/client/events' require 'octokit/client/authorizations' +require 'octokit/client/refs' module Octokit class Client @@ -54,5 +55,6 @@ def initialize(options={}) include Octokit::Client::Users include Octokit::Client::Events include Octokit::Client::Authorizations + include Octokit::Client::Refs end end diff --git a/lib/octokit/client/refs.rb b/lib/octokit/client/refs.rb new file mode 100644 index 000000000..c78cbd5ae --- /dev/null +++ b/lib/octokit/client/refs.rb @@ -0,0 +1,85 @@ +module Octokit + class Client + module Refs + + # List all refs for a given user and repo + # + # @param repo [String, Repository, Hash] A GitHub repository + # @param namespace [String] The ref namespace, e.g. tag or heads + # @return [Array] A list of references matching the repo and the namespace + # @see http://developer.github.com/v3/git/refs/ + # @example Fetch all refs for sferik/rails_admin + # Octokit.refs("sferik/rails_admin") + def refs(repo, namespace="", options={}) + get("/repos/#{Repository.new(repo)}/git/refs/#{namespace}", options, 3) + end + alias :list_refs :refs + alias :references :refs + alias :list_references :refs + + # Fetch a given reference + # + # @param repo [String, Repository, Hash] A GitHub repository + # @param ref [String] The ref, e.g. tags/v0.0.3 + # @return [Reference] The reference matching the given repo and the ref id + # @see http://developer.github.com/v3/git/refs/ + # @example Fetch tags/v0.0.3 for sferik/rails_admin + # Octokit.ref("sferik/rails_admin","tags/v0.0.3") + def ref(repo, ref, options={}) + get("/repos/#{Repository.new(repo)}/git/refs/#{ref}", options, 3) + end + alias :reference :ref + + # Create a reference + # + # @param repo [String, Repository, Hash] A GitHub repository + # @param ref [String] The ref, e.g. tags/v0.0.3 + # @param sha [String] A SHA, e.g. 827efc6d56897b048c772eb4087f854f46256132 + # @return [Array] The list of references, already containing the new one + # @see http://developer.github.com/v3/git/refs/ + # @example Create refs/heads/master for octocat/Hello-World with sha 827efc6d56897b048c772eb4087f854f46256132 + # Octokit.create_ref("octocat/Hello-World","heads/master", "827efc6d56897b048c772eb4087f854f46256132") + def create_ref(repo, ref, sha, options={}) + parameters = { + :ref => "refs/#{ref}", + :sha => sha + } + post("repos/#{Repository.new(repo)}/git/refs", options.merge(parameters)) + end + alias :create_reference :create_ref + + # Update a reference + # + # @param repo [String, Repository, Hash] A GitHub repository + # @param ref [String] The ref, e.g. tags/v0.0.3 + # @param sha [String] A SHA, e.g. 827efc6d56897b048c772eb4087f854f46256132 + # @param force [Boolean] A flag indicating one wants to force the update to make sure the update is a fast-forward update. + # @return [Array] The list of references updated + # @see http://developer.github.com/v3/git/refs/ + # @example Force update heads/sc/featureA for octocat/Hello-World with sha aa218f56b14c9653891f9e74264a383fa43fefbd + # Octokit.update_ref("octocat/Hello-World","heads/sc/featureA", "aa218f56b14c9653891f9e74264a383fa43fefbd") + def update_ref(repo, ref, sha, force=true, options={}) + parameters = { + :sha => sha, + :force => force + } + patch("repos/#{Repository.new(repo)}/git/refs/#{ref}", options.merge(parameters)) + end + alias :update_reference :update_ref + + # Delete a single reference + # + # @param repo [String, Repository, Hash] A GitHub repository + # @param ref [String] The ref, e.g. tags/v0.0.3 + # @return [Response] A response object with status + # @see http://developer.github.com/v3/git/refs/ + # @example Delete tags/v0.0.3 for sferik/rails_admin + # Octokit.delete_ref("sferik/rails_admin","tags/v0.0.3") + def delete_ref(repo, ref, options={}) + delete("/repos/#{Repository.new(repo)}/git/refs/#{ref}", options, 3, true, true) + end + alias :delete_reference :delete_ref + + end + end +end diff --git a/spec/fixtures/v3/ref.json b/spec/fixtures/v3/ref.json new file mode 100644 index 000000000..d54242590 --- /dev/null +++ b/spec/fixtures/v3/ref.json @@ -0,0 +1,9 @@ +{ + "object": { + "sha": "0591c7b765779ea8797f0f0b9222299e600393ca", + "type": "tag", + "url": "https://api.github.com/repos/sferik/rails_admin/git/tags/0591c7b765779ea8797f0f0b9222299e600393ca" + }, + "ref": "refs/tags/v0.0.3", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/tags/v0.0.3" +} diff --git a/spec/fixtures/v3/ref_create.json b/spec/fixtures/v3/ref_create.json new file mode 100644 index 000000000..cafc94ec0 --- /dev/null +++ b/spec/fixtures/v3/ref_create.json @@ -0,0 +1,29 @@ +[ + { + "ref": "refs/heads/master", + "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/master", + "object": { + "type": "commit", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", + "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" + } + }, + { + "ref": "refs/heads/gh-pages", + "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/gh-pages", + "object": { + "type": "commit", + "sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac", + "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac" + } + }, + { + "ref": "refs/tags/v0.0.1", + "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/tags/v0.0.1", + "object": { + "type": "tag", + "sha": "940bd336248efae0f9ee5bc7b2d5c985887b16ac", + "url": "https://api.github.com/repos/octocat/Hello-World/git/tags/940bd336248efae0f9ee5bc7b2d5c985887b16ac" + } + } +] diff --git a/spec/fixtures/v3/ref_update.json b/spec/fixtures/v3/ref_update.json new file mode 100644 index 000000000..6a23e09d5 --- /dev/null +++ b/spec/fixtures/v3/ref_update.json @@ -0,0 +1,11 @@ +[ + { + "ref": "refs/heads/sc/featureA", + "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/sc/featureA", + "object": { + "type": "commit", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", + "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" + } + } +] diff --git a/spec/fixtures/v3/refs.json b/spec/fixtures/v3/refs.json new file mode 100644 index 000000000..e1a82fadc --- /dev/null +++ b/spec/fixtures/v3/refs.json @@ -0,0 +1,38 @@ +[ + { + "object": { + "sha": "19453d77913e5cc92dd836294bcb6e7ceb4d2a7f", + "type": "commit", + "url": "https://api.github.com/repos/sferik/rails_admin/git/commits/19453d77913e5cc92dd836294bcb6e7ceb4d2a7f" + }, + "ref": "refs/heads/actions", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/heads/actions" + }, + { + "object": { + "sha": "1cb0628eea5be6120c4bfcbe609b3cea5f246290", + "type": "commit", + "url": "https://api.github.com/repos/sferik/rails_admin/git/commits/1cb0628eea5be6120c4bfcbe609b3cea5f246290" + }, + "ref": "refs/heads/activo", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/heads/activo" + }, + { + "object": { + "sha": "acfa1e5d2bf3c80a511d3d2ea6dcd60169d5c564", + "type": "commit", + "url": "https://api.github.com/repos/sferik/rails_admin/git/commits/acfa1e5d2bf3c80a511d3d2ea6dcd60169d5c564" + }, + "ref": "refs/heads/associated-collection-refact", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/heads/associated-collection-refact" + }, + { + "object": { + "sha": "14cdd3be4ada38f8e6974935f806c8de170abf11", + "type": "commit", + "url": "https://api.github.com/repos/sferik/rails_admin/git/commits/14cdd3be4ada38f8e6974935f806c8de170abf11" + }, + "ref": "refs/heads/dsl-refactoring", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/heads/dsl-refactoring" + } +] diff --git a/spec/fixtures/v3/refs_tags.json b/spec/fixtures/v3/refs_tags.json new file mode 100644 index 000000000..e80116657 --- /dev/null +++ b/spec/fixtures/v3/refs_tags.json @@ -0,0 +1,29 @@ +[ + { + "object": { + "sha": "69edc0b0ac7dfa2780a39c113b54718b6defe081", + "type": "tag", + "url": "https://api.github.com/repos/sferik/rails_admin/git/tags/69edc0b0ac7dfa2780a39c113b54718b6defe081" + }, + "ref": "refs/tags/v0.0.1", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/tags/v0.0.1" + }, + { + "object": { + "sha": "daa8d377587115484ab4f06272a9242436ab615c", + "type": "tag", + "url": "https://api.github.com/repos/sferik/rails_admin/git/tags/daa8d377587115484ab4f06272a9242436ab615c" + }, + "ref": "refs/tags/v0.0.2", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/tags/v0.0.2" + }, + { + "object": { + "sha": "0591c7b765779ea8797f0f0b9222299e600393ca", + "type": "tag", + "url": "https://api.github.com/repos/sferik/rails_admin/git/tags/0591c7b765779ea8797f0f0b9222299e600393ca" + }, + "ref": "refs/tags/v0.0.3", + "url": "https://api.github.com/repos/sferik/rails_admin/git/refs/tags/v0.0.3" + } +] diff --git a/spec/octokit/client/refs_spec.rb b/spec/octokit/client/refs_spec.rb new file mode 100644 index 000000000..093359480 --- /dev/null +++ b/spec/octokit/client/refs_spec.rb @@ -0,0 +1,79 @@ +# -*- encoding: utf-8 -*- +require 'helper' + +describe Octokit::Client::Refs do + + before do + @client = Octokit::Client.new(:login => 'sferik') + end + + describe ".refs" do + + it "should return all refs" do + stub_get("/repos/sferik/rails_admin/git/refs/"). + to_return(:body => fixture("v3/refs.json")) + refs = @client.refs("sferik/rails_admin") + refs.first.ref.should == "refs/heads/actions" + end + + it "should return all tag refs" do + stub_get("/repos/sferik/rails_admin/git/refs/tags"). + to_return(:body => fixture("v3/refs_tags.json")) + refs = @client.refs("sferik/rails_admin","tags") + refs.first.ref.should == "refs/tags/v0.0.1" + end + + end + + describe ".ref" do + + it "should return the tags/v0.0.3 ref" do + stub_get("/repos/sferik/rails_admin/git/refs/tags/v0.0.3"). + to_return(:body => fixture("v3/ref.json")) + ref = @client.ref("sferik/rails_admin","tags/v0.0.3") + ref.object.type.should eq("tag") + ref.ref.should eq("refs/tags/v0.0.3") + ref.url.should eq("https://api.github.com/repos/sferik/rails_admin/git/refs/tags/v0.0.3") + end + + end + + describe ".create_ref" do + + it "should create a ref" do + stub_post("/repos/octocat/Hello-World/git/refs"). + with(:body => { "ref" => "refs/heads/master", "sha" => "827efc6d56897b048c772eb4087f854f46256132" }, + :headers => {'Content-Type'=>'application/json'}). + to_return(:body => fixture("v3/ref_create.json")) + ref = @client.create_ref("octocat/Hello-World","heads/master", "827efc6d56897b048c772eb4087f854f46256132") + ref.first.ref.should eq("refs/heads/master") + end + + end + + describe ".update_ref" do + + it "should update a ref" do + stub_patch("/repos/octocat/Hello-World/git/refs/heads/sc/featureA"). + with(:body => { "sha" => "aa218f56b14c9653891f9e74264a383fa43fefbd", "force" => true }, + :headers => {'Content-Type'=>'application/json'}). + to_return(:body => fixture("v3/ref_update.json")) + refs = @client.update_ref("octocat/Hello-World","heads/sc/featureA", "aa218f56b14c9653891f9e74264a383fa43fefbd", true) + refs.first.ref.should eq("refs/heads/sc/featureA") + refs.first.object.sha.should eq("aa218f56b14c9653891f9e74264a383fa43fefbd") + end + end + + describe ".delete_ref" do + + it "should delete an existing ref" do + stub_delete("/repos/octocat/Hello-World/git/refs/heads/feature-a"). + to_return(:status => 204) + ref = @client.delete_ref("octocat/Hello-World", "heads/feature-a") + ref.status.should == 204 + end + + end + +end +