-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandbox.py
235 lines (204 loc) · 8.91 KB
/
sandbox.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from urllib import response
import requests
import json
from requests.auth import HTTPBasicAuth
import pymysql
from datetime import datetime
def add_properties_file(GITHUB_TOKEN, REPO_NAME, REPO_OWNER, SONAR_ORGANISATION):
response = requests.post("https://ecph6290vl.execute-api.ap-south-1.amazonaws.com/dev/addcontentinrepo",
json={"GITHUB_OWNER": REPO_OWNER,
"GITHUB_REPO": REPO_NAME,
"GITHUB_TOKEN": GITHUB_TOKEN,
"PATH": "sonar-project.properties",
"COMMIT_MESSAGE": "Added sonar-project.properties",
"CONTENT": f"sonar.projectKey={REPO_NAME}\nsonar.organization={SONAR_ORGANISATION}"
}).json()
print(response)
return response
def add_xml_file(GITHUB_TOKEN, REPO_NAME, REPO_OWNER):
with open("build.yml", "r") as f:
content = f.read()
response = requests.post("https://ecph6290vl.execute-api.ap-south-1.amazonaws.com/dev/addcontentinrepo",
json={"GITHUB_OWNER": REPO_OWNER,
"GITHUB_REPO": REPO_NAME,
"GITHUB_TOKEN": GITHUB_TOKEN,
"PATH": ".github/workflows/build.yml",
"COMMIT_MESSAGE": "Added build file",
"CONTENT": content
}).json()
print(response)
return response
def lambda_handler(event, context):
connection = pymysql.connect(
host="rds-mysql-tutorial.cw5il3f2mv55.ap-south-1.rds.amazonaws.com",
user="admin",
password="9908rajesh",
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
USER_ID = event['USER_ID']
with connection.cursor() as cursor:
sql = "SELECT github_token,sonar_cloud_token,github_id,sonar_user_key FROM github_users.github_user_auth where user_id=%s "
cursor.execute(sql, (USER_ID))
result = cursor.fetchone()
GITHUB_TOKEN = result['github_token']
SONAR_TOKEN = result['sonar_cloud_token']
REPO_OWNER = result['github_id']
SONAR_ORGANISATION = result['sonar_user_key']
# theses will get from front end
APPLICANT_ID = event['APPLICANT_ID']
TASK = event.get('TASK', None)
TASK_LANG = event.get('TASK_LANG', None)
TASK_CLOSE_DATE = event.get('TASK_CLOSE_DATE', None)
TASK_TITLE = event.get('TASK_TITLE', None)
APPLICANT_GITHUB = event.get('APPLICANT_GITHUB', None)
AREA = event.get('AREA', None)
LEVEL = event.get('LEVEL', None)
JOB_ID = event.get('JOB_ID', None)
#repo_name
curr_dt = datetime.now()
REPO_NAME = f"{(APPLICANT_GITHUB).lower()}_{str(int(round(curr_dt.timestamp())))}"
try:
# creating github repo
print("starting")
create_repo_response = create_repo(GITHUB_TOKEN, REPO_NAME)
if(create_repo_response['statusCode'] != 200):
print("Repo already exists or is not able to be created")
return {
'statusCode': 400,
'message': 'Repo already exists or is not able to be created',
'body': json.dumps(create_repo_response)
}
# creating project on sonar cloud
create_sonar_project_respsone = create_sonar_project(
SONAR_TOKEN, REPO_NAME, SONAR_ORGANISATION)
try:
if(create_sonar_project_respsone['project']['key'] != REPO_NAME):
pass
except:
print("Unable to create sonar project")
return {
'statusCode': 400,
'message': 'Unable to create sonar project',
'body': json.dumps(create_sonar_project_respsone)
}
add_xml_file_response = add_xml_file(
GITHUB_TOKEN, REPO_NAME, REPO_OWNER)
if(add_xml_file_response['statusCode'] != 200):
print("Unable to add xml file")
return {
'statusCode': 400,
'message': 'Unable to add xml file',
'body': json.dumps(add_xml_file_response)
}
# changing the name of default branch from main to master
change_branch_name_to_master_response = change_branch_name_to_master(
REPO_OWNER, REPO_NAME, GITHUB_TOKEN)
print(change_branch_name_to_master_response)
# adding sonar properties in file for "others"
# adding sonar token in repo secret
add_secret_response = add_secret(
GITHUB_TOKEN, REPO_NAME, REPO_OWNER, SONAR_TOKEN)
if(add_secret_response['statusCode'] != 200):
print("Unable to add secret")
return {
'statusCode': 400,
'message': 'Unable to add secret',
'body': json.dumps(add_secret)
}
add_properties_file_response = add_properties_file(
GITHUB_TOKEN, REPO_NAME, REPO_OWNER, SONAR_ORGANISATION)
if(add_properties_file_response['statusCode'] != 200):
print("Unable to add properties file")
return {
'statusCode': 400,
'message': 'Unable to add properties file',
'body': json.dumps(add_properties_file_response)
}
# uploading yml file to github for "others"
assign_task_response = assign_task(
GITHUB_TOKEN, REPO_NAME, REPO_OWNER, APPLICANT_GITHUB)
if(assign_task_response['statusCode'] != 200):
print("Unable to assign task")
return {
'statusCode': 400,
'message': 'Unable to assign task',
'body': json.dumps(assign_task_response)
}
with connection.cursor() as cursor:
sql = """
INSERT INTO `github_data`.`applicant_tasks`
(
`user_id`,
`applicant_id`,
`create_date`,
`task`,
`task_lang`,
`repo_name`,
`sonarcloud_project`,
`github_token`,
`sonarcloud_token`,
`task_close_date`,
`checkin_status`,
github_owner,
task_title,
area,
level,
applicant_github_id,
sonar_organisation,
job_id
)
VALUES
(
%s,%s,NOW(),%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s
)
"""
cursor.execute(sql,
(
USER_ID,
APPLICANT_ID,
TASK,
TASK_LANG,
REPO_NAME,
REPO_NAME,
GITHUB_TOKEN,
SONAR_TOKEN,
TASK_CLOSE_DATE,
'CREATED',
REPO_OWNER,
TASK_TITLE,
AREA,
LEVEL,
APPLICANT_GITHUB,
SONAR_ORGANISATION,
JOB_ID
))
connection.commit()
response = requests.post(f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/hooks',
headers={
'Authorization': f'token {GITHUB_TOKEN}'},
json={"name": "web", "active": True, "events": ["push"], "config": {"url": "https://mr67jl4chavl6gu23ad52y2hty0txpea.lambda-url.ap-south-1.on.aws/", "content_type": "json", "insecure_ssl": "0"}}).json()
print(response)
return {
'statusCode': 200,
'body': json.dumps('Project created successfully')
}
except Exception as e:
print(e)
return {
'statusCode': 500,
'body': json.dumps('Internal Error'),
'message': json.dumps(str(e), default=str)
}
# ------------------------------------------------------------
# lambda_handler(
# {
# "REPO_NAME": "fianl_sonar_cloud",
# "REPO_OWNER": "siddhart1o1",
# "USER_ID": "11",
# "APPLICANT_ID": "1",
# "TASK": "This is a Test",
# "TASK_LANG": "OTHERS",
# "TASK_CLOSE_DATE": "2022-10-31",
# "TASK_TITLE": "TEST", "AREA": "TEST", "LEVEL": "TEST", "APPLICANT_GITHUB": "rajdeep1o1"
# }, None)
# -----------------------------------------------------------