forked from sahil-sagwekar2652/GitHub-Automation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_repo
58 lines (47 loc) · 1.58 KB
/
create_repo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import argparse
import os
import http.client
import json
from github_secrets import GITHUB_API_TOKEN, USERNAME
if not GITHUB_API_TOKEN:
raise ValueError("Please set the environment variable GITHUB_API_TOKEN in the github_secrets.py file") # noqa: E501
URL = "https://api.github.com/user/repos"
parser = argparse.ArgumentParser(description='creates a local repository linked with a remote repository') # noqa: E501
parser.add_argument('path',
metavar='PATH',
type=str,
help='Enter the path for the new repository')
parser.add_argument('name',
metavar='NAME',
type=str,
help='Enter a name for the new repository')
args = parser.parse_args()
name = args.name
path = args.path
os.chdir(path)
os.mkdir(name)
os.chdir(name)
os.system('git init -b main')
os.system('touch README.md')
os.system('git add . && git commit -m "initial commit"')
# os.system('git status')
conn = http.client.HTTPSConnection("api.github.com")
payload = json.dumps({
"name": name,
"description": "made with the GitHub API"
})
headers = {
'Authorization': f'Bearer {GITHUB_API_TOKEN}',
'Content-Type': 'application/json',
'User-Agent': f'{USERNAME}'
}
conn.request("POST", "/user/repos", payload, headers)
res = conn.getresponse()
data = res.read().decode("utf-8")
response = json.loads(data)
print(response)
remote_url = response['svn_url']
os.system(f'git remote add origin {remote_url}')
os.system('git push origin main')
print(f"\nREMOTE URL FOR \"{name}\" is: {remote_url}")