-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
executable file
·162 lines (135 loc) · 4.63 KB
/
main.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
#!/usr/bin/python3
import os
import re
import time
import telebot
from requests import get, post
from requests.exceptions import ReadTimeout
from config import (
allowed_user_ids,
github_repo,
github_token,
tg_token,
workflow_id,
)
bot = telebot.TeleBot(tg_token)
def is_valid_url(url):
regex = re.compile(
r'^(https?:\/\/)?(www\.)?([^\s.]+\.\S{2,}|localhost[\:?\d]*)\S*$'
)
return re.match(regex, url)
@bot.message_handler(commands=['alive'])
def check_alive(message):
chat_id = message.chat.id
bot.send_message(chat_id, "I'm alive and ready to go!")
@bot.message_handler(commands=['cancel'])
def cancel_run(message):
chat_id = message.chat.id
user_id = message.from_user.id
if user_id not in allowed_user_ids:
bot.send_message(chat_id, 'You are not authorized to use this command.')
return
# Extract the run ID from the message
run_id = (
message.text.split(' ', 1)[1].strip()
if len(message.text.split(' ', 1)) > 1
else ''
)
if not run_id.isdigit():
bot.send_message(chat_id, 'Invalid run ID provided.')
return
# Prepare the request to cancel the workflow run
url = f'https://api.github.com/repos/{github_repo}/actions/runs/{run_id}/cancel'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {github_token}',
}
response = post(url, headers=headers)
if response.status_code == 202:
bot.send_message(
chat_id,
f'Run `{run_id}` cancellation initiated successfully.',
parse_mode='Markdown',
disable_web_page_preview=True,
)
else:
bot.send_message(
chat_id,
f'Failed to cancel the run {run_id}: {response.text}',
parse_mode='Markdown',
disable_web_page_preview=True,
)
@bot.message_handler(commands=['dump'])
def dump(message):
chat_id = message.chat.id
user_id = message.from_user.id
if user_id not in allowed_user_ids:
bot.send_message(chat_id, 'You are not authorized to use this command.')
return
# Extract the URL from the message
dump_url = (
message.text.split(' ', 1)[1].strip()
if len(message.text.split(' ', 1)) > 1
else ''
)
if not is_valid_url(dump_url):
bot.send_message(chat_id, 'Invalid URL provided.')
return
# Prepare the request to trigger the workflow_dispatch event
url = f'https://api.github.com/repos/{github_repo}/actions/workflows/{workflow_id}/dispatches'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {github_token}',
}
payload = {
'ref': 'master',
'inputs': {'urls': dump_url},
}
response = post(url, headers=headers, json=payload)
if response.status_code == 204:
time.sleep(3) # hack
bot.send_message(chat_id, 'Dump started successfully!')
# Fetch the latest workflow run to get the run_id
runs_url = f'https://api.github.com/repos/{github_repo}/actions/runs'
runs_response = get(runs_url, headers=headers)
if runs_response.status_code == 200:
runs_data = runs_response.json()
if (
'workflow_runs' in runs_data
and len(runs_data['workflow_runs']) > 0
):
run_id = runs_data['workflow_runs'][0]['id']
run_url = (
f'https://github.com/{github_repo}/actions/runs/{run_id}'
)
bot.send_message(
chat_id,
f'Check the dump progress [here]({run_url})\nTo cancel this dump run: `/cancel {run_id}`',
parse_mode='Markdown',
disable_web_page_preview=True,
)
else:
bot.send_message(chat_id, 'Failed to retrieve the run ID.')
else:
bot.send_message(
chat_id,
f'Failed to retrieve workflow runs: {runs_response.text}',
disable_web_page_preview=True,
)
else:
bot.send_message(
chat_id,
f'Failed to start the dump: {response.text}',
disable_web_page_preview=True,
)
if __name__ == '__main__':
while True:
try:
bot.polling(none_stop=True, interval=0, timeout=35)
break
except ReadTimeout as e:
print(f'ReadTimeout occurred: {e}')
time.sleep(1)
except Exception as e:
print(f'An unexpected error occurred: {e}')
time.sleep(1)