Skip to content

Commit 43079bd

Browse files
Script for fork_clone.py
1 parent 6c8e6f1 commit 43079bd

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

scripts/fork_clone.py

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

0 commit comments

Comments
 (0)