3

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?

3 Answers 3

3

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}'
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

Can you elaborate on what user-to-server requests are?
@supersaiyajin87 user-to-server requests are when a GitHub app acts on behalf of a user. The request must be authenticated with a user's access token. More info here developer.github.com/apps/building-github-apps/…
I recently got an update from GitHub that GitHub apps authenticated as an installation are able to create a repository from a repository template within the same organization. Meaning the template repository and repository being created must belong to the same organization.
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.