Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Template Repositories API #1133

Merged
merged 10 commits into from
Jul 20, 2019
43 changes: 43 additions & 0 deletions lib/octokit/client/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def fork(repo, options = {})
# @option options [String] :private `true` makes the repository private, and `false` makes it public.
# @option options [String] :has_issues `true` enables issues for this repo, `false` disables issues.
# @option options [String] :has_wiki `true` enables wiki for this repo, `false` disables wiki.
# @option options [Boolean] :is_template `true` makes this repo available as a template repository, `false` to prevent it.
# @option options [String] :has_downloads `true` enables downloads for this repo, `false` disables downloads.
# @option options [String] :organization Short name for the org under which to create the repo.
# @option options [Integer] :team_id The id of the team that will be granted access to this repository. This is only valid when creating a repo in an organization.
Expand All @@ -155,6 +156,7 @@ def create_repository(name, options = {})
opts = options.dup
organization = opts.delete :organization
opts.merge! :name => name
ensure_api_media_type(:template_repositories, opts) if opts.include? :is_template
EricPickup marked this conversation as resolved.
Show resolved Hide resolved

if organization.nil?
post 'user/repos', opts
Expand Down Expand Up @@ -192,6 +194,23 @@ def transfer_repository(repo, new_owner, options = {})
end
alias :transfer_repo :transfer_repository

# Clone a template repository for a user or organization
#
# @param repo [Integer, String, Hash, Repository] A GitHub template repository
# @param name [String] Name of the new repo
# @option options [String] :owner Organization or user who will own the new repository.
# @option options [String] :description Description of the repo
# @option options [String] :private `true` makes the repository private, and `false` makes it public.
# @return [Sawyer::Resource] Repository info for the new repository
def clone_template_repository(repo, name, options = {})
options.merge! :name => name
options = ensure_api_media_type(:template_repositories, options)
post "#{Repository.path repo}/generate", options
end
alias :create_repository_from_template :clone_template_repository
alias :clone_template_repo :clone_template_repository
alias :create_repo_from_template :clone_template_repository
EricPickup marked this conversation as resolved.
Show resolved Hide resolved

# Hide a public repository
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
Expand Down Expand Up @@ -696,6 +715,30 @@ def update_subscription(repo, options = {})
def delete_subscription(repo, options = {})
boolean_from_response :delete, "#{Repository.path repo}/subscription", options
end

# Check if a repository is a template repository
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @return [Boolean] True if repository is a template repository, false otherwise.
def template_repository?(repo)
options = ensure_api_media_type(:template_repositories, {})
response = get Repository.path(repo), options
response.is_template?
end
alias :template_repo? :template_repository?
EricPickup marked this conversation as resolved.
Show resolved Hide resolved

# Change whether a repository is a template repository
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param is_template [Boolean] True if making repo a template, false if making it not a template.
# @return [Sawyer::Resource] Repository information
def template_repository(repo, is_template)
repo = Repository.new(repo)
options = { is_template: is_template, name: repo.name }
options = ensure_api_media_type(:template_repositories, options)
patch "repos/#{repo}", options
end
alias :template_repo :template_repository
EricPickup marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
3 changes: 2 additions & 1 deletion lib/octokit/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module Preview
:topics => 'application/vnd.github.mercy-preview+json'.freeze,
:community_profile => 'application/vnd.github.black-panther-preview+json'.freeze,
:strict_validation => 'application/vnd.github.speedy-preview+json'.freeze,
:drafts => 'application/vnd.github.shadow-cat-preview'.freeze
:drafts => 'application/vnd.github.shadow-cat-preview'.freeze,
:template_repositories => 'application/vnd.github.baptiste-preview+json'.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.

49 changes: 49 additions & 0 deletions spec/octokit/client/repositories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,55 @@
end
end

describe ".clone_template_repository", :vcr do
before do
@client.template_repository(@repo.full_name, true)
end

it "clones a repository" do
@client.clone_template_repository(@repo.id, "Cloned repo")
assert_requested :post, github_url("/repositories/#{@repo.id}/generate")
end
end

describe ".template_repository?", :vcr do
context "repository is a template repository" do
before(:each) do
@client.template_repository(@repo.full_name, true)
end

it "returns true" do
expect(@client.template_repository?(@repo.full_name)).to be true
end
end

context "repository is not a template repository" do
before(:each) do
@client.template_repository(@repo.full_name, false)
end

it "returns false" do
expect(@client.template_repository?(@repo.full_name)).to be false
end
end
end

describe ".template_repository", :vcr do
context "is_template is true" do
it "makes repo a template repository" do
@client.template_repository(@repo.full_name, true)
expect(@client.template_repository?(@repo.full_name)).to be true
end
end

context "is_template is false" do
it "makes repo not a template repository" do
@client.template_repository(@repo.full_name, false)
expect(@client.template_repository?(@repo.full_name)).to be false
end
end
end

describe ".create_repository", :vcr do
it "creates a repository" do
expect(@repo.name).to eq("an-repo")
Expand Down