Create repository using template API

I’m trying to understand how Create repository using a repository template API permissions work when being used by a Github App.

Currently the app has Administration and Contents Repository permissions. However, I see the following error when trying to use that API endpoint either via an installation token or a user access token:

curl -X POST https://api.github.com/repos/react-boilerplate/react-boilerplate/generate -d '{"owner": "scouredimage", "name": "testrepo"}' -H 'Accept: application/vnd.github.baptiste-preview+json' -H 'Content-Type: application/json' -H 'Authorization: token TOKEN'
{
  "message": "Resource not accessible by integration",
  "documentation_url": "https://developer.github.com/v3/repos/#create-repository-using-a-repository-template"
}

Does this mean that the API endpoint is just not accessible to Github Apps? Or, am I missing a required App permission?

Thanks in advance!

4 Likes

There’s nothing inherently different from what the GitHub API sees when you make a request with your personal access token vs when your GitHub app makes the request. So the issue is with your OAuth scope. Check out https://developer.github.com/v3/repos/#oauth-scope-requirements-1 to make sure you have it all set up correctly.

One quick side note: Given that this API endpoint is in preview, it may be restricting access to your GitHub app potentially. I suggest reaching out to support to clarify if that is the case. 

Reviving this topic because this was one of the things that I came upon in a Google search about this subject.

I tried to do this with a “GitHub App” (i.e. the specific app type defined here) and was unsuccessful. I was, however, able to create a repo using a template using an “OAuth App”.

The sketch below applies to creating a repo from a template within an organization. I hope it’s applicable to creating repos outside of an organization as well.

const { Octokit } = require('@octokit/rest')
const { createOAuthDeviceAuth } = require('@octokit/auth-oauth-device')
const repoName = 'my-repo-name'
const orgName = 'my-org-name'
const templateRepo = 'serverless-framework-aws-typescript-template'

// OAuth
const clientId = 'my-client-id' // will be a long hex string

// because Node still doesn't have top level await
;(async function () {

  /*
  * BEGIN Device Flow section
  * This section authorizes the sketch with the GitHub Device Flow 
  * (https://docs.github.com/en/developers/apps/authorizing-oauth-apps) 
  */
  const auth = createOAuthDeviceAuth({
    clientType: 'oauth-app',
    clientId,
    onVerification (verification) {
      console.log('Open %s', verification.verification_uri)
      console.log('Enter code: %s', verification.user_code)
    },
    scopes: ['repo', 'workflow', 'delete_repo'],
  })

  const tokenAuthentication = await auth({ type: 'oauth' })
  const octokit = new Octokit({ auth: tokenAuthentication.token })
  /*
  * END Device Flow section
  */  

  await octokit.repos.createUsingTemplate({
    template_owner: orgName,
    template_repo: templateRepo,
    owner: orgName,
    name: repoName,
    private: true
  })
  console.info(`Repository "${repoName}" created`)

  await octokit.repos.update({
    owner: orgName,
    repo: repoName,
    visibility: 'internal',
    has_issues: true,
  })
  console.info(`Repository "${repoName}" updated`)
})()
1 Like

@kpeters-cbsi I’m trying to figure it out also but can’t make it works with Github App. I normally just want to create a repo that doesn’t necessarily from a template.

But seems guys from Stackbit could do that with their Github App. Do you have any idea on that?

No clue - I did it using OAuth.