Created
November 10, 2017 16:40
-
-
Save wgordon17/8d6b5c189613f3ad1e383ba7b3730ce9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
""" | |
openshift_sre.py - Various helper modules for OpenShift Online SRE team | |
Copyright © 2017, Will Gordon <wgordon@redhat.com> | |
Licensed under the MIT license. | |
""" | |
from __future__ import unicode_literals, absolute_import, print_function, division | |
import re | |
from sopel.module import commands, example, rule, url, interval | |
from sopel.config.types import ( | |
StaticSection, FilenameAttribute, ValidatedAttribute | |
) | |
from pygsheets import authorize, SpreadsheetNotFound, NoValidUrlKeyFound | |
gsheet_url_regex = 'https://docs\.google\.com/spreadsheets/d/(?P<key>[a-zA-Z0-9]*)/edit#gid=(?P<gid>[0-9]*)&range=(?P<range>[A-Z0-9:]*)' | |
snow_ticket_regex = '.*(?P<ticket>(TASK|REQ|RITM|INC|PRB|CHG)[0-9]{7}).*' | |
channel_regex = '.*libra-ops.*' | |
class SreSection(StaticSection): | |
google_service_account_file = FilenameAttribute('google_service_account_file', | |
directory=False, | |
default='~/.sopel/service_account.json') | |
"""File generated from a Google Cloud project (https://console.developers.google.com). | |
In a project, generate a service_account file from APIs & services > Credentials > Create credentials > Service account key. | |
Also, be sure to enable Google Drive API. | |
""" | |
topic_channel = ValidatedAttribute('topic_channel', default='#wgordon') | |
"""The channel to automatically keep updated based on supplied google sheet""" | |
def configure(config): | |
config.define_section('openshift_sre', SreSection) | |
config.openshift_sre.configure_setting( | |
'google_service_account_file', | |
'Enter the path to your service_account.json file.' | |
) | |
config.openshift_sre.configure_setting( | |
'topic_channel', | |
'Enter the channel to automatically keep the topic updated based on supplied google sheet.' | |
) | |
def setup(bot): | |
global monitor_channel | |
bot.config.define_section('openshift_sre', SreSection) | |
monitor_channel = bot.config.openshift_sre.topic_channel | |
def read_google_sheet(bot, url): | |
match = re.search(gsheet_url_regex, url) | |
groups = match.groupdict() | |
try: | |
gc = authorize(service_file=bot.config.openshift_sre.google_service_account_file) | |
except ValueError: | |
bot.msg(monitor_channel, | |
'The Service Account JSON file doesn\'t appear to exist. See ' | |
'https://cloud.google.com/storage/docs/authentication#service_accounts for creating a service ' | |
'account. Then move to ' + bot.config.openshift_sre.google_service_account_file) | |
return False | |
# Open Spreadsheet for access | |
try: | |
s = gc.open_by_key(groups['key']) | |
except SpreadsheetNotFound: | |
bot.msg(monitor_channel, | |
'I can\'t find that spreadsheet. Are you sure {service_account_email} has been invited to that doc?'.format( | |
service_account_email=gc.oauth.service_account_email)) | |
return False | |
# Find worksheet based on `gid` in URL | |
for w in s.worksheets(): | |
if str(w.id) == groups['gid']: | |
break | |
else: | |
bot.msg(monitor_channel, | |
'The worksheet (gid={gid}) on that spreadsheet ({title}) doesn\'t seem to exist.'.format(gid=groups['gid'], | |
title=s.title)) | |
return False | |
if ':' in groups['range']: | |
values = [] | |
for row in w.range(str(groups['range'])): | |
values.append(' - '.join([col.value for col in row])) | |
return '\r\n'.join(values) | |
else: | |
return w.cell(str(groups['range'])).value | |
@url(gsheet_url_regex) | |
@example('https://docs.google.com/spreadsheets/d/11z8RMQbRiyxofT0AquN5MiDAJn8TVRXqcIaf15EMQuc/edit#gid=1717581866&range=B6:C11') | |
def gsheet(bot, trigger, match): | |
google_sheet_values = read_google_sheet(bot, bot.db.get_channel_value(monitor_channel, 'topic_source')) | |
if google_sheet_values is not False: | |
bot.db.set_channel_value(monitor_channel, 'topic_source', trigger.group(1)) | |
bot.say('Topic will be periodically updated based on {url}'.format(url=bot.db.get_channel_value(monitor_channel, 'topic_source'))) | |
bot.write(['TOPIC', monitor_channel], google_sheet_values) | |
# Update every 6 hours | |
@interval(21600) | |
def update_topic(bot): | |
if monitor_channel in bot.channels and bot.db.get_channel_value(monitor_channel, 'topic_source'): | |
google_sheet_values = read_google_sheet(bot, bot.db.get_channel_value(monitor_channel, 'topic_source')) | |
if google_sheet_values is not False: | |
bot.write(['TOPIC', monitor_channel], google_sheet_values) | |
else: | |
bot.db.set_channel_value(monitor_channel, 'topic_source', None) | |
@rule(snow_ticket_regex) | |
def snow_ticket(bot, trigger): | |
bot.say('https://redhat.service-now.com/surl.do?n=' + trigger.groupdict()['ticket']) | |
@rule(channel_regex) | |
def refer_to_topic(bot, trigger): | |
google_sheet_values = read_google_sheet(bot, bot.db.get_channel_value(monitor_channel, 'topic_source')) | |
if google_sheet_values is not False: | |
bot.say(google_sheet_values) | |
else: | |
bot.say('Refer to the channels topic') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment