-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.py
53 lines (43 loc) · 1.46 KB
/
create.py
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
#!/usr/bin/env python3
import argparse
import os
import requests
from dotenv import load_dotenv
load_dotenv()
GITHUB_TOKEN =os.getenv("GITHUB_TOKEN")
GITHUB_USER = os.getenv("GITHUB_USER")
REPO_PATH = os.getenv("REPO_PATH")
parser = argparse.ArgumentParser()
parser.add_argument("--name", "-n", type=str, dest="name", required=True)
parser.add_argument("--private", "-p", dest="is_private", action="store_true")
args = parser.parse_args()
repo_name = args.name
is_private = args.is_private
GITHUB_URL = "https://api.github.com"
if is_private:
payload = '{"name": "' + repo_name + '", "private": true }'
else:
payload = '{"name": "' + repo_name + '", "private": false }'
headers = {
"Authorization": "token " + GITHUB_TOKEN,
"Accept": "application/vnd.github.v3+json"
}
try:
r = requests.post(GITHUB_URL + "/user/repos", data=payload, headers=headers)
r.raise_for_status()
except requests.exceptions.RequestException as err:
raise SystemExit(err)
try:
os.chdir(REPO_PATH)
os.system("mkdir " + repo_name)
os.chdir(repo_name)
os.system('git init')
os.system('git remote add origin https://github.com/' + GITHUB_USER + '/' + repo_name + '.git')
os.system(f'echo # {repo_name} >> README.md')
os.system('git add README.md')
os.system('git commit -m "Initial Commit" ')
os.system('git branch -M main')
os.system('git push origin main')
os.system('code .')
except FileExistsError as err:
raise SystemExit(err)