-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub2s.py
More file actions
142 lines (113 loc) · 4.13 KB
/
github2s.py
File metadata and controls
142 lines (113 loc) · 4.13 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
import json
import os
import uuid
import requests
from dotenv import load_dotenv
from lambda_function import github2s_worker
from supabase import create_client, Client
import threading
load_dotenv()
url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_KEY")
database: str = os.environ.get("SUPABASE_DATABASE")
storage_bucket: str = os.environ.get("SUPABASE_STORAGE")
supabase: Client = create_client(url, key)
def send_message(msg, status):
print(msg, status)
return {
"statusCode": status,
"headers": {
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,GET,POST"
},
"body": msg
}
def get_latest_commit_hash(owner, repo):
url = f"https://api.github.com/repos/{owner}/{repo}/commits"
response = requests.get(url)
if response.status_code == 200:
commits = response.json()
if commits:
return commits[0]['sha']
return None
def download_file_from_storage(bucket_key):
jsonData = {}
print(bucket_key)
with open(bucket_key, 'wb+') as f:
res = supabase.storage.from_(storage_bucket).download(bucket_key)
f.write(res)
with open(bucket_key, 'r') as f:
json_content = f.read()
jsonData = json.loads(json_content)
os.remove(bucket_key)
return jsonData
def github2sCode(event):
result = {}
owner = event.get('owner')
repo = event.get('repo')
task_id = event.get('task_id')
branch = event.get('branch')
ignore_folders = event.get('ignore_folders')
if task_id:
##################################
get_task_id_result = supabase.table(database).select("*").eq('task_id',task_id).execute()
##################################
detail = get_task_id_result.data
data = get_task_id_result.data[0]
if not len(detail):
result['status'] = 'NOT_FOUND'
return send_message(result, 404)
if data.get('status') == 'IN_PROGRESS':
result['status'] = 'IN_PROGRESS'
return send_message(result, 200)
if data.get('status') == 'FAILED':
result['status'] = 'FAILED'
result['error'] = detail.get('error', {}).get('error')
return send_message(result, 404)
bucket_key = f'{task_id}.json'
jsonData = download_file_from_storage(bucket_key)
result['data'] = jsonData
result['status'] = 'COMPLETED'
return send_message(result, 200)
latest_commit_hash = get_latest_commit_hash(owner, repo)
if latest_commit_hash is None:
result['status'] = 'FAILED'
result['error'] = 'Unable to fetch the latest commit hash'
return send_message(result, 500)
print(latest_commit_hash, owner, repo)
# Query db for existing task with matching owner, repo, and commit hash
responseData = supabase.table(database).select("*").eq('task_id',latest_commit_hash).execute()
items = responseData.data
print(items)
if items and (branch == None and ignore_folders == None):
print("#######################")
existing_task = items[0]
task_id = existing_task['task_id']
bucket_key = f'{task_id}.json'
jsonData = download_file_from_storage(bucket_key)
result['data'] = jsonData
result['status'] = 'COMPLETED'
return send_message(result, 200)
print(latest_commit_hash, owner, repo)
# Insert initial task details in db
supabase.table(database).insert(
{
"task_id": latest_commit_hash,
"githubOwner": owner,
"githubRepo": repo,
"status": "IN_PROGRESS",
"lastCommit": latest_commit_hash,
"error": {},
}
).execute()
event['task_id'] = latest_commit_hash
print(event)
thread = threading.Thread(target=github2s_worker, kwargs={'event': event})
thread.start()
result['task_id'] = latest_commit_hash
result['status'] = 'IN_PROGRESS'
print(result)
# Return the task ID to the client
return send_message(result, 200)
export = github2sCode