-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
156 lines (135 loc) · 5.46 KB
/
app.py
File metadata and controls
156 lines (135 loc) · 5.46 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.templating import Jinja2Templates
from starlette.routing import Route
import os
import requests
import json
from uuid6 import uuid7
import base64
import secrets
from dotenv import load_dotenv
load_dotenv(verbose=True)
templates = Jinja2Templates(directory="templates")
GITHUB_OAUTH_CLIENT_ID = os.getenv("GITHUB_OAUTH_CLIENT_ID")
GITHUB_OAUTH_CLIENT_SECRET = os.getenv("GITHUB_OAUTH_CLIENT_SECRET")
GITHUB_OAUTH_REDIRECT_URI = os.getenv("GITHUB_OAUTH_REDIRECT_URI")
async def homepage(request):
"""Homepage and display connect Github link"""
body = await request.body()
print(body)
client_id = GITHUB_OAUTH_CLIENT_ID
state = secrets.token_urlsafe(30)
github_authorize_url = f"https://github.com/login/oauth/authorize?client_id={client_id}&state={state}&scope=repo%20user:email" # noqa: E501
return templates.TemplateResponse(
"index.html", {"github_authorize_url": github_authorize_url, "request": request}
)
async def githubcallback(request):
code = request.query_params.get("code")
state = request.query_params.get("state")
# Validate state token matches what we sent when
# generating the auth url
# TODO check if already authorised
# Get access token by POST'ing to github access token endpoint
data = {
"client_id": GITHUB_OAUTH_CLIENT_ID,
"client_secret": GITHUB_OAUTH_CLIENT_SECRET,
"code": code,
"redirect_uri": GITHUB_OAUTH_REDIRECT_URI,
}
headers = {"Accept": "application/json"}
req = requests.post(
"https://github.com/login/oauth/access_token", data=data, headers=headers
)
# Get access token from github
resp = req.json()
access_token = resp.get("access_token")
scope = resp.get("scope")
print(access_token)
# Use access token
headers = {"Authorization": f"token {access_token}"}
headers["Accept"] = "application/vnd.github+json"
# Create unique repo name
uuid = uuid7()
repo_name = f"container-hosting-{uuid}"
data = {
"name": repo_name,
"description": "This is your first repository",
"homepage": "https://container-hosting.anotherwebservice.com/",
"private": False,
"has_issues": True,
"has_projects": True,
"has_wiki": True,
}
req = requests.get("https://api.github.com/user", headers=headers).json()
# Get their GitHub username
username = req.get("login")
avatar_url = req.get("avatar_url")
# Get email address so can do git commits with correct author information
req = requests.get("https://api.github.com/user/emails", headers=headers)
email = req.json()[0].get("email", None)
# Create a repo for organisation user has access to
# req = requests.post("https://api.github.com/orgs/karmacomputing/repos", headers=headers, data=json.dumps(data))
# Create repo for authenticated user
req = requests.post(
"https://api.github.com/user/repos", headers=headers, data=json.dumps(data)
)
repo_url = req.json()["html_url"]
print(repo_url)
# Upload initial repo content
with open("./repo-template-files/.autorc") as fp:
autorc = fp.read()
autorc = autorc.replace("GITHUB_OWNER", username)
autorc = autorc.replace("GITHUB_REPO_NAME", repo_name)
autorc_b64 = base64.b64encode(autorc.encode("utf-8")).decode("utf-8")
data = {
"message": "create .autorc",
"committer": {"name": username, "email": email},
"content": autorc_b64,
}
req = requests.put(
f"https://api.github.com/repos/{username}/{repo_name}/contents/.autorc",
headers=headers,
data=json.dumps(data),
)
with open("./repo-template-files/.github/workflows/release.yml") as fp:
release_yml = fp.read()
release_yml = release_yml.replace("GITHUB_OWNER", username)
release_yml = release_yml.replace("GITHUB_REPO_NAME", repo_name)
release_yml_b64 = base64.b64encode(release_yml.encode("utf-8")).decode("utf-8")
data = {
"message": "create .release_yml",
"committer": {"name": username, "email": email},
"content": release_yml_b64,
}
req = requests.put(
f"https://api.github.com/repos/{username}/{repo_name}/contents/.github/workflows/release.yml",
headers=headers,
data=json.dumps(data),
)
with open("./repo-template-files/.github/ISSUE_TEMPLATE/feature_request.md") as fp:
feature_request_md_b64 = base64.b64encode(release_yml.encode("utf-8")).decode(
"utf-8"
)
data = {
"message": "create feature_request_md",
"committer": {"name": username, "email": email},
"content": feature_request_md_b64,
}
req = requests.put(
f"https://api.github.com/repos/{username}/{repo_name}/contents/.github/ISSUE_TEMPLATE/feature_request.md",
headers=headers,
data=json.dumps(data),
)
return templates.TemplateResponse(
"welcome.html", {"repo_url": repo_url, "request": request}
)
async def health(request):
print(request)
return PlainTextResponse("OK")
routes = [
Route("/", homepage, methods=["GET", "POST"]),
Route("/health", health, methods=["GET"]),
Route("/githubcallback", githubcallback, methods=["GET"]),
]
app = Starlette(debug=True, routes=routes)