|
| 1 | +# Fork and Clone by Python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import requests |
| 5 | +import json |
| 6 | +import os |
| 7 | +from github_secrets import GITHUB_API_TOKEN |
| 8 | + |
| 9 | +# Parse command-line arguments |
| 10 | +parser = argparse.ArgumentParser(description='Fork a repo') |
| 11 | +parser.add_argument('owner', type=str, help='Repository owner') |
| 12 | +parser.add_argument('repo', type=str, help='Repository name') |
| 13 | +# parser.add_argument('token', type=str, help='GitHub API token') |
| 14 | +parser.add_argument('name', type=str, help='Enter name for forked repo') |
| 15 | +args = parser.parse_args() |
| 16 | + |
| 17 | +# Get the command-line arguments |
| 18 | +owner = args.owner |
| 19 | +repo = args.repo |
| 20 | +# token = args.token |
| 21 | +token = GITHUB_API_TOKEN |
| 22 | +name = args.name |
| 23 | + |
| 24 | +# Set the GitHub API endpoint for forking a repo |
| 25 | +url = f"https://api.github.com/repos/{owner}/{repo}/forks" |
| 26 | + |
| 27 | +# Set the headers with the API token for authentication |
| 28 | +headers = { |
| 29 | + 'Authorization': f'token {token}', |
| 30 | + 'Accept': 'application/vnd.github.v3+json' |
| 31 | +} |
| 32 | + |
| 33 | +# Set the fork repo name |
| 34 | +data = { |
| 35 | + "name" : name |
| 36 | +} |
| 37 | + |
| 38 | +# Send a POST request to fork |
| 39 | +response = requests.post(url, headers=headers, json=data) |
| 40 | + |
| 41 | +# Check the response status code |
| 42 | +if response.ok: |
| 43 | + # Forked successfully |
| 44 | + fork_data = response.json() |
| 45 | + print("Forked successfully!") |
| 46 | + print(fork_data.get('clone_url')) |
| 47 | +else: |
| 48 | + # Fork failed |
| 49 | + error_message = response.json().get('message', 'Unknown error') |
| 50 | + error_status = response.status_code |
| 51 | + error_response = json.dumps(response.json(), indent=4) |
| 52 | + print(f"Failed to fork a repo. Error status: {error_status}") |
| 53 | + print(f"Error message: {error_message}") |
| 54 | + print("Error response:") |
| 55 | + print(error_response) |
| 56 | + |
| 57 | +# Code to Clone |
| 58 | +try: |
| 59 | + cmd = "git clone {}".format(fork_data.get('clone_url')) |
| 60 | + print("Starting to clone {}".format(fork_data.get('clone_url'))) |
| 61 | + print("Running command '{}'".format(cmd)) |
| 62 | + os.system(cmd) |
| 63 | + print("Finshed cloning {}".format(fork_data.get('clone_url'))) |
| 64 | + print("#####################################") |
| 65 | + print("") |
| 66 | + print("Thank you!") |
| 67 | +except NameError: |
| 68 | + print("Error cloning") |
| 69 | +# Forked and cloned successfully |
0 commit comments