kwigs
kwigs

Reputation: 43

GitHub API: create a repository from template authenticated as installation

I'm attempting to create a repository from a repository template via GitHub's API for an organization. https://api.github.com/repos/{template_org_name}/{template_repo}/generate. I'm trying to do so with a GitHub app authenticated as an installation with administrative permissions on the organization. I'm unable to successfully create a repository and get a Resource not accessible by integration response. I am, however, able to create a repository via this same endpoint using my own GitHub user's personal access token. I'm inclined to think that this endpoint is only available as a user-to-server request, but have not had any luck looking at docs (https://developer.github.com/v3/repos/#create-repository-using-a-repository-template). I understand that it is technically a beta endpoint, so maybe that is my answer.

I checked that I am using the right "Accept" header as well in the request (Accept: application/vnd.github.baptiste-preview+json). Anyone have any luck with this endpoint?

Upvotes: 3

Views: 6882

Answers (3)

tj-cappelletti
tj-cappelletti

Reputation: 1864

The answers here have become outdated in a good way. At some point between when everyone answered this post to my answer now, GitHub updated the API to allow for this.

There is an API endpoint that provides this functionality: https://docs.github.com/en/rest/repos/repos#create-a-repository-using-a-template

Template repos have a /generate endpoint in the API now which allows you send a JSON body with the details of the new repo you want to create.

curl \
  -X POST \
  -H "Accept: application/vnd.github+json" \ 
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  https://api.github.com/repos/TEMPLATE_OWNER/TEMPLATE_REPO/generate \
  -d '{"owner":"NEW-REPO-OWNER","name":"NEW-REPO-NAME","description":"Repo created from a template","include_all_branches":false,"private":false}'

Upvotes: 3

Paul Odeon
Paul Odeon

Reputation: 4524

I struggled with this also. It turns out it is only available as a user-to-server request but can be done with Github Apps as documented here: https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps

At a high level:

  1. Select "Request user authorization (OAuth) during installation" in your apps settings and add a callback URL in your app
  2. Get the code from the redirect (on installation) which looks something like this: https://xxx.ngrok.io/?code=xxx&installation_id=xxx&setup_action=install
  3. Exchange the code for a regular access token at POST https://github.com/login/oauth/access_token or exchange_code_for_token in octokit.rb. This needs: client_id, client_secret and the code from the redirect.
  4. Use the returned access code to access the API directly as a user. i.e. github = Octokit::Client.new(access_token: "xxx")
  5. Create a repo for the user github.create_repository('xxx', private: true)

A additional point of confusion for me was the difference between app_id and client_id. Both are listed on your app page but you need the client_id!

Upvotes: 1

kwigs
kwigs

Reputation: 43

Turns out that the endpoint to create a repository from a template is only available as a user-to-server request and is not enabled for GitHub apps. It is very subtle, but only API actions that have an information icon next to the name in the documentation are available to GitHub apps. Example of information icon

Upvotes: 1

Related Questions