0

I have generated a personal access token and configured Single Sign-On (SSO), providing full authorization to the token. The issue I'm facing is that while I can successfully create GitHub repositories, retrieve repository details, and list repositories using the same token, I am unable to create a repository from a template repository. I wrote a Python script to accomplish this, but I am encountering a 404 error. Below is the dummy code I used for this task:

import requests
import json

# Replace with your actual access token and template repository details
ACCESS_TOKEN = 'your_github_token'  # Your GitHub token
ORG_NAME = 'DummyWorld'               # Organization name
TEMPLATE_OWNER = 'DummyOwner'         # Owner of the template repository
TEMPLATE_REPO = 'DummyTemplateRepo'   # Name of the template repository
NEW_REPO_OWNER = 'DummyWorld'         # Owner of the new repository (organization)
NEW_REPO_NAME = 'DummyWorld-TestRepo' # Name of the new repository
DESCRIPTION = 'This is a test repository created from a template.'  # Description for the new repository
INCLUDE_ALL_BRANCHES = False          # Include all branches from the template
PRIVATE = True                        # Set to True for a private repository

# GitHub API endpoint to generate a new repository from a template
url = f'https://api.github.com/repos/{TEMPLATE_OWNER}/{TEMPLATE_REPO}/generate'

# Headers for the request
headers = {
    'Accept': 'application/vnd.github+json',
    'Authorization': f'Bearer {ACCESS_TOKEN}',
    'X-GitHub-Api-Version': '2022-11-28',
}

# Data for the request
data = {
    'owner': NEW_REPO_OWNER,
    'name': NEW_REPO_NAME,
    'description': DESCRIPTION,
    'include_all_branches': INCLUDE_ALL_BRANCHES,
    'private': PRIVATE,
}

# Make the POST request to generate the repository
response = requests.post(url, headers=headers, data=json.dumps(data))

# Check if the request was successful
if response.status_code == 201:
    print("\nRepository created successfully:")
    created_repo_info = response.json()
    print("-" * 50)
    print(f"Repository Name: {created_repo_info['name']}")
    print(f"Full Name: {created_repo_info['full_name']}")
    print(f"Description: {created_repo_info['description']}")
    print(f"Private: {created_repo_info['private']}")
    print("-" * 50)
else:
    print(f"\nFailed to create repository: {response.status_code}")
    print("Error Response:")
    print(json.dumps(response.json(), indent=4))  # Pretty print the error response
    enter code here

error code:
Failed to create repository: 404
Error Response:
{
    "message": "Not Found",
    "documentation_url": "https://docs.github.com/rest",
    "status": "404"
}
1
  • The above issue has been solved by adding "organisation name" as template owner and NEW_REPO_OWNER TEMPLATE_OWNER = 'DummyOwner' # Owner of the template repository TEMPLATE_REPO = 'DummyTemplateRepo' # Name of the template repository NEW_REPO_OWNER = 'DummyOwner' Commented Dec 18, 2024 at 7:06

0

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.