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
24 changes: 24 additions & 0 deletions lib/octokit/client/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ def repository(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 the repository a template, `false` makes it not a template.
# @option options [String] :has_downloads `true` enables downloads for this repo, `false` disables downloads.
# @option options [String] :default_branch Update the default branch for this repository.
# @return [Sawyer::Resource] Repository information
def edit_repository(repo, options = {})
repo = Repository.new(repo)
if options.include? :is_template
options = ensure_api_media_type(:template_repositories, options)
end
options[:name] ||= repo.name
patch "repos/#{repo}", options
end
Expand Down Expand Up @@ -144,6 +148,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 +160,9 @@ def create_repository(name, options = {})
opts = options.dup
organization = opts.delete :organization
opts.merge! :name => name
if opts.include? :is_template
opts = ensure_api_media_type(:template_repositories, opts)
end

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

# Create a repository for a user or organization generated from a template repository
#
# @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 the new repository will belong to.
# @option options [String] :description Description of the repo
# @option options [String] :private `true` makes the repository private, and `false` makes it public.
# @option options [Boolean] :include_all_branches `true` copies all branches from the template repository, `false` (default) makes it only copy the master branch.
# @return [Sawyer::Resource] Repository info for the new repository
def create_repository_from_template(repo, name, options = {})
options.merge! :name => name
options = ensure_api_media_type(:template_repositories, options)
post "#{Repository.path repo}/generate", options
end
alias :create_repo_from_template :create_repository_from_template

# Hide a public repository
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
Expand Down
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.

30 changes: 29 additions & 1 deletion spec/octokit/client/repositories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
expect(repository.topics).to be_kind_of Array
expect(repository.topics).to include("syntax-highlighting")
end
end # .repository
end # .repository

describe ".set_private" do
it "sets a repository private" do
Expand Down Expand Up @@ -60,6 +60,23 @@
end
end

describe ".edit_repository", :vcr do
before(:each) do
@repo = @client.create_repository(test_github_repository)
end

after(:each) do
@client.delete_repository(@repo.full_name)
end

context "is_template is passed in params", :vcr do
it "uses the template repositories preview flag and succeeds" do
@client.edit_repository(@repo.full_name, is_template: true)
expect(@client.repository(@repo.full_name).is_template).to be true
end
end
end

describe ".add_deploy_key" do
it "adds a repository deploy keys" do
request = stub_post(github_url("/repos/#{@test_repo}/keys"))
Expand Down Expand Up @@ -107,6 +124,17 @@
end
end

describe ".create_repository_from_template", :vcr do
before do
@client.edit_repository(@repo.full_name, is_template: true)
end

it "generates a repository from the template" do
@client.create_repository_from_template(@repo.id, "Cloned repo")
assert_requested :post, github_url("/repositories/#{@repo.id}/generate")
end
end

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