-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
385 lines (343 loc) · 18.5 KB
/
bot.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import configparser
import urllib
from urllib.request import urlopen
from urllib.parse import quote_plus
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler, CallbackQueryHandler
from telegram import InlineQueryResultArticle, ChatAction, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup
from uuid import uuid4
import subprocess
import time
import logging
import json
from json import JSONDecoder
from functools import partial
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
config = configparser.ConfigParser()
config.read('bot.ini')
updater = Updater(token=config['KEYS']['bot_api'])
jenkins = config['JENKINS']['url']
user = config['JENKINS']['user']
password = config['JENKINS']['password']
token = config['JENKINS']['token']
job = config['JENKINS']['job']
gerrituser = config['GERRIT']['user']
gerriturl = config['GERRIT']['url']
protocol = config['GERRIT']['protocol']
dispatcher = updater.dispatcher
def pickopen(bot, update):
if update.message.from_user.id == int(config['ADMIN']['id']):
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
curl = "curl -H 'Accept-Type: application/json' " + protocol + "://" + gerrituser + "@" + gerriturl + "/changes/?q=status:open | sed '1d' > open.json"
command = subprocess.Popen(curl, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
with open('open.json', encoding='utf-8') as data_file:
data = json.load(data_file)
dict_length = len(data)
for i in range(dict_length):
try:
cnumbers
except NameError:
cnumbers = ""
cnumbers = cnumbers + " " + str(data[i]['_number'])
print(cnumbers)
text = "I will pick all open changes: " + cnumbers
bot.sendMessage(chat_id=update.message.chat_id,
text=text,
parse_mode="Markdown")
global rpick
cnumbers.replace(" ", "%20")
cnumbers_url = cnumbers.replace(" ", "%20")
rpick = cnumbers_url
def openchanges(bot, update):
if update.message.from_user.id == int(config['ADMIN']['id']):
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
curl = "rm open.json && curl -H 'Accept-Type: application/json' " + protocol + "://" + gerrituser + "@" + gerriturl + "/changes/?q=status:open | sed '1d' > open.json"
command = subprocess.Popen(curl, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
time.sleep(1)
with open('open.json', encoding='utf-8') as data_file:
data = json.load(data_file)
dict_length = len(data)
for i in range(dict_length):
try:
openc
except NameError:
openc = ""
openc = openc + "\n" + "<a href=" + '"' + protocol + "://" + gerriturl + "/#/c/" + str(data[i]['_number']) + "/" + '"' + ">" + str(data[i]['_number']) + "</a>" + " - " + str(data[i]['subject'])
for i in range(dict_length):
try:
cnum
except NameError:
cnum = "/repopick"
cnum = cnum + " " + str(data[i]['_number'])
print(openc)
print(cnum)
bot.sendMessage(chat_id=update.message.chat_id,
text=openc,
parse_mode="HTML")
if update.message.chat.type == "private":
bot.sendMessage(chat_id=update.message.chat_id,
text=cnum,
parse_mode="Markdown")
def start(bot, update):
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
bot.sendMessage(chat_id=update.message.chat_id,
text="Hi. I'm Hunter's Jenkins Bot! You can use me to start builds, assuming your name is @hunter_bruhh! If not, then I'm not much use to you right now! Maybe he'll implement some cool stuff later!")
if update.message.from_user.id != int(config['ADMIN']['id']):
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
time.sleep(1)
bot.sendMessage(chat_id=update.message.chat_id,
text="It seems like you aren't allowed to use me. :(")
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
else:
bot.sendMessage(chat_id=update.message.chat_id,
text="Sup @hunter_bruhh ! \nHere's a list of commands for you to use\n/build to start the build process\n/changelog 'text' to set the changelog\n/sync to set sync to on/off\n/clean to set clean to on/off\n/repopick " + "`" + "changes"+ "`" + " to pick from gerrit on build\n/repopick to set repopick on or off\n/open to see all open changes\n/pickopen to pick all open changes on gerrit\n/start to see this message :)")
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
def choosebuild(bot, update):
if update.message.from_user.id == int(config['ADMIN']['id']):
keyboard = [[InlineKeyboardButton("Without Paramaters", callback_data='build')],
[InlineKeyboardButton("With Parameters", callback_data='buildWithParameters')]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Please choose a build style:', reply_markup=reply_markup)
def sync(bot, update):
if update.message.from_user.id == int(config['ADMIN']['id']):
keyboard = [[InlineKeyboardButton("YES", callback_data='syncon')],
[InlineKeyboardButton("NO", callback_data='syncoff')]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Would you like to sync on a new build?:', reply_markup=reply_markup)
def clean(bot, update):
if update.message.from_user.id == int(config['ADMIN']['id']):
keyboard = [[InlineKeyboardButton("YES", callback_data='cleanon')],
[InlineKeyboardButton("NO", callback_data='cleanoff')]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Would you like to clean on a new build?:', reply_markup=reply_markup)
def buildwithparams(bot, update, query):
query = update.callback_query
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, this will include a custom changelog with your build, and will specify whether to sync & clean or not",
parse_mode="Markdown")
user_id = update.callback_query.from_user.id
try:
cg
except NameError:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but the changelog is empty. Please use /changelog + 'text' to provide a changlog for your users.",
parse_mode="Markdown")
return 1
try:
syncparam
except NameError:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but have not specified whether you would like to sync before building. Please use /sync to do so.",
parse_mode="Markdown")
return 1
try:
cleanparam
except NameError:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but have not specified whether you would like to clean before building. Please use /clean to do so.",
parse_mode="Markdown")
return 1
try:
repopickstatus
except NameError:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but repopick isn't turned on or off. Turn it on or off with /repopick",
parse_mode="Markdown")
return 1
if repopickstatus == "true":
try:
rpick
except NameError:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, repopick is on, but it's empty. Please use /repopick + 'changes' to pick changes from gerrit, or turn repopick off with /repopick",
parse_mode="Markdown")
return 1
if cg:
if syncparam:
if cleanparam:
global changelog
changelog = quote_plus('cg')
if repopickstatus == "true":
command_string = jenkins + "/job/" + job + "/buildWithParameters?token=" + token + "&changelog=" + cg + "&SYNC=" + syncparam + "&CLEAN=" + cleanparam + "&repopicks=" + rpick
if repopickstatus == "false":
command_string = jenkins + "/job/" + job + "/buildWithParameters?token=" + token + "&changelog=" + cg + "&SYNC=" + syncparam + "&CLEAN=" + cleanparam
command = "curl --user " + user + ":" + password + " " + "'" + command_string + "'"
print (command)
if user_id == int(config['ADMIN']['id']):
bot.sendChatAction(chat_id=query.message.chat_id,
action=ChatAction.TYPING)
output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = output.stdout.read().decode('utf-8')
output = '`{0}`'.format(output)
bot.sendMessage(chat_id=query.message.chat_id,
text=output,
parse_mode="Markdown")
else:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but have not specified whether you would like to clean before building. Please use /clean to do so.",
parse_mode="Markdown")
else:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but have not specified whether you would like to sync before building. Please use /sync to do so.",
parse_mode="Markdown")
else:
bot.sendMessage(chat_id=query.message.chat_id,
text="You have selected the 'buildWithParameters option, but the changelog is empty. Please use /changelog + 'text' to provide a changlog for your users.",
parse_mode="Markdown")
def buildwithoutparams(bot, update, query):
user_id = update.callback_query.from_user.id
command_string = jenkins + "/job/" + job + "/buildWithParameters?token=" + token
command = "curl --user " + user + ":" + password + " " + "'" + command_string + "'"
print (command)
if user_id == int(config['ADMIN']['id']):
bot.sendChatAction(chat_id=query.message.chat_id,
action=ChatAction.TYPING)
output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = output.stdout.read().decode('utf-8')
output = '`{0}`'.format(output)
bot.sendMessage(chat_id=query.message.chat_id,
text=output, parse_mode="Markdown")
def changelog(bot, update, args):
if update.message.from_user.id == int(config['ADMIN']['id']):
global cg
user = update.message.from_user
str_args = ' '.join(args)
if str_args != "":
update.message.reply_text('Changelog updated: ' + "'" + str_args + "'")
cgs = '%20'.join(args)
cg = cgs
print ("Changelog set to " + "'" + cg + "'")
else:
bot.sendMessage(chat_id=update.message.chat_id,
text="You cannot provide an empty changelog.",
parse_mode="Markdown")
def repopick(bot, update, args):
if update.message.from_user.id == int(config['ADMIN']['id']):
global rpick
user = update.message.from_user
str_args = ' '.join(args)
if str_args != "":
update.message.reply_text('I will pick changes: ' + "'" + str_args + "'")
rpicks = '%20'.join(args)
rpick = rpicks
print ("Repopick set to" + "'" + rpick + "'")
else:
keyboard = [[InlineKeyboardButton("ON", callback_data='repopickon')],
[InlineKeyboardButton("OFF", callback_data='repopickoff')]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Turn repopick ON or OFF:', reply_markup=reply_markup)
def button(bot, update, direct=True):
user_id = update.callback_query.from_user.id
if user_id == int(config['ADMIN']['id']):
query = update.callback_query
selected_button = query.data
global cleanparam
global syncparam
global repopickstatus
if selected_button == 'buildWithParameters':
bot.editMessageText(text="Selected option: With Paramaters",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
buildwithparams(bot, update, query)
if selected_button == 'build':
bot.editMessageText(text="Selected option: Without Paramaters",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
buildwithoutparams(bot, update, query)
if selected_button == 'syncon':
bot.editMessageText(text="Selected option: YES",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
syncparam = "true"
bot.sendMessage(chat_id=query.message.chat_id,
text="Sync set to true",
parse_mode="Markdown")
if selected_button == 'syncoff':
bot.editMessageText(text="Selected option: NO",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
syncparam = "false"
bot.sendMessage(chat_id=query.message.chat_id,
text="Sync set to false",
parse_mode="Markdown")
if selected_button == 'cleanon':
bot.editMessageText(text="Selected option: YES",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
cleanparam = "true"
bot.sendMessage(chat_id=query.message.chat_id,
text="Clean set to true",
parse_mode="Markdown")
if selected_button == 'cleanoff':
bot.editMessageText(text="Selected option: NO",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
cleanparam = "false"
bot.sendMessage(chat_id=query.message.chat_id,
text="Clean set to false",
parse_mode="Markdown")
if selected_button == 'repopickon':
bot.editMessageText(text="Selected option: ON",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
repopickstatus = "true"
bot.sendMessage(chat_id=query.message.chat_id,
text="repopick set to ON",
parse_mode="Markdown")
if selected_button == 'repopickoff':
bot.editMessageText(text="Selected option: OFF",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
repopickstatus = "false"
bot.sendMessage(chat_id=query.message.chat_id,
text="repopick set to OFF",
parse_mode="Markdown")
else:
bot.sendMessage(chat_id=query.message.chat_id,
text="You trying to spam me bro?",
parse_mode="Markdown")
return False
def inlinequery(bot, update):
query = update.inline_query.query
o = execute(query, update, direct=False)
results = list()
results.append(InlineQueryResultArticle(id=uuid4(),
title=query,
description=o,
input_message_content=InputTextMessageContent(
'*{0}*\n\n{1}'.format(query, o),
parse_mode="Markdown")))
bot.answerInlineQuery(update.inline_query.id, results=results, cache_time=10)
open_handler = CommandHandler('open', openchanges)
pickopen_handler = CommandHandler('pickopen', pickopen)
start_handler = CommandHandler('start', start)
sync_handler = CommandHandler('sync', sync)
clean_handler = CommandHandler('clean', clean)
build_handler = CommandHandler('build', choosebuild)
repopick_handler = CommandHandler('repopick', repopick, pass_args=True)
changelog_handler = CommandHandler('changelog', changelog, pass_args=True)
dispatcher.add_handler(open_handler)
dispatcher.add_handler(pickopen_handler)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(sync_handler)
dispatcher.add_handler(clean_handler)
dispatcher.add_handler(build_handler)
dispatcher.add_handler(repopick_handler)
dispatcher.add_handler(changelog_handler)
dispatcher.add_handler(CallbackQueryHandler(button))
dispatcher.add_handler(InlineQueryHandler(inlinequery))
dispatcher.add_error_handler(error)
updater.start_polling()
updater.idle()