Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

phab: add cache handler and warn on old config #280

Merged
merged 7 commits into from
Aug 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 73 additions & 12 deletions MirahezeBots/plugins/phab.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""phab.by - Phabricator Task Information Plugin"""

import requests # FIX THIS
from sopel.module import commands, example, interval, rule
from sopel.module import commands, example, interval, rule, require_admin
from sopel.config.types import StaticSection, ValidatedAttribute
from json import JSONDecodeError
from sopel.tools import get_logger, SopelMemory
from sopel.config import ConfigurationError
from MirahezeBots.utils import jsonparser as jp
LOGGER = get_logger('phab')


class PhabricatorSection(StaticSection):
Expand All @@ -12,10 +16,17 @@ class PhabricatorSection(StaticSection):
querykey = ValidatedAttribute('querykey', str)
highpri_notify = ValidatedAttribute('highpri_notify', bool)
highpri_channel = ValidatedAttribute('highpri_channel', str)
datafile = ValidatedAttribute('datafile', str)


def setup(bot):
bot.config.define_section('phabricator', PhabricatorSection)
bot.memory["phab"] = SopelMemory()
bot.memory["phab"]["jdcache"] = jp.createdict(bot.settings.phab.datafile)
if bot.settings.phabricator.host and bot.settings.phabricator.datafile:
raise ConfigurationError("Use of host and datafile together is not supported")
elif bot.settings.phabricator.host:
LOGGER.warn("Use of the host option was deceprated in 9.0.0 and will be removed in 10.0.0")


def configure(config):
Expand All @@ -26,6 +37,7 @@ def configure(config):
config.phabricator.configure_setting('highpri_notify', 'Would you like to enable automatic notification of high priority tasks? (true/false)')
config.phabricator.configure_setting('highpri_channel',
'If you enabled high priority notifications, what channel would you like them sent to? (notifications will be sent once every week.')
config.phabricator.configure_setting('datafile', 'File to read from to get channel specific data from')


BOLD = '\x02'
Expand All @@ -37,12 +49,25 @@ def configure(config):


def searchphab(bot, channel, task=1):
if bot.settings.phabricator.host:
host = 'https://{0}/api/'.format(bot.settings.phabricator.host)
apikey = bot.settings.phabricator.api_token[0]
elif bot.settings.phabricator.datafile:
if channel in bot.memory["phab"]["jdcache"]:
host = bot.memory["phab"]["jdcache"][channel]["host"]
arraypos = int(bot.memory["phab"]["jdcache"][host]["arraypos"])
apikey = bot.settings.phabricator.api_token[arraypos]
else:
host = bot.memory["phab"]["jdcache"]["default"]["host"]
arraypos = int(bot.memory["phab"]["jdcache"][host]["arraypos"])
apikey = bot.settings.phabricator.api_token[arraypos]

data = {
'api.token': bot.settings.phabricator.api_token,
'api.token': apikey,
'constraints[ids][0]': task
}
response = requests.post(
url='https://{0}/api/maniphest.search'.format(bot.settings.phabricator.host),
url='{0}/maniphest.search'.format(host),
data=data)
response = response.json()
go = 0
Expand All @@ -57,23 +82,23 @@ def searchphab(bot, channel, task=1):
bot.say("An unknown error occured.", channel)
if go == 1:
params = {
'api.token': bot.settings.phabricator.api_token,
'api.token': apikey,
'constraints[phids][0]': result.get("fields").get("ownerPHID")
}
response2 = requests.post(
url='https://{0}/api/user.search'.format(bot.settings.phabricator.host),
url='{0}/user.search'.format(host),
data=params)
try:
response2 = response2.json()
except JSONDecodeError as e:
bot.say(response2.text, bot.settings.core.logging_channel)
bot.say(str(e), bot.settings.core.logging_channel)
params2 = {
'api.token': bot.settings.phabricator.api_token,
'api.token': apikey,
'constraints[phids][0]': result.get("fields").get("authorPHID")
}
response3 = requests.post(
url='https://{0}/api/user.search'.format(bot.settings.phabricator.host),
url='{0}/user.search'.format(host),
data=params2)
response3 = response3.json()
if result.get("fields").get("ownerPHID") is None:
Expand All @@ -93,12 +118,25 @@ def searchphab(bot, channel, task=1):


def gethighpri(limit=True, channel='#miraheze', bot=None):
if bot.settings.phabricator.host:
host = '{0}/'.format(bot.settings.phabricator.host)
apikey = bot.settings.phabricator.api_token[0]
elif bot.settings.phabricator.datafile:
if channel in bot.memory["phab"]["jdcache"]:
host = bot.memory["phab"]["jdcache"][channel]["host"]
arraypos = int(bot.memory["phab"]["jdcache"][host]["arraypos"])
apikey = bot.settings.phabricator.api_token[arraypos]
else:
host = bot.memory["phab"]["jdcache"]["default"]["host"]
arraypos = int(bot.memory["phab"]["jdcache"][host]["arraypos"])
apikey = bot.settings.phabricator.api_token[arraypos]
querykey = bot.settings.phabricator.querykey[arraypos]
data = {
'api.token': bot.settings.phabricator.api_token,
'queryKey': bot.settings.phabricator.querykey, # mFzMevK.KRMZ for mhphab
'api.token': apikey,
'queryKey': querykey, # mFzMevK.KRMZ for mhphab
}
response = requests.post(
url='https://{0}/api/maniphest.search'.format(bot.settings.phabricator.host),
url='{0}/maniphest.search'.format(host),
data=data)
response = response.json()
result = response.get("result")
Expand All @@ -113,8 +151,7 @@ def gethighpri(limit=True, channel='#miraheze', bot=None):
while x < len(data):
currdata = data[x]
if x > 5 and limit:
bot.say("They are more than 5 tasks. Please see {0} for the rest or use .highpri".format(
bot.settings.phabricator.host), channel)
bot.say("They are more than 5 tasks. Please see {0} for the rest or use .highpri".format(host), channel)
break
else:
searchphab(bot=bot, channel=channel, task=currdata.get("id"))
Expand Down Expand Up @@ -152,3 +189,27 @@ def high_priority_tasks_notification(bot):
@example('.highpri')
def forcehighpri(bot, trigger):
gethighpri(limit=False, channel=trigger.sender, bot=bot)


@require_admin(message="Only admins may purge cache.")
@commands('resetphabcache')
def reset_phab_cache(bot, trigger):
"""
Reset the cache of the channel management data file
"""
bot.reply("Refreshing Cache...")
bot.memory["phab"]["jdcache"] = jp.createdict(bot.settings.phab.datafile)
bot.reply("Cache refreshed")


@require_admin(message="Only admins may check cache")
@commands('checkphabcache')
def check_phab_cache(bot, trigger):
"""
Validate the cache matches the copy on disk
"""
result = jp.validatecache(bot.settings.phab.datafile, bot.memory["phab"]["jdcache"])
if result:
bot.reply("Cache is correct.")
else:
bot.reply("Cache does not match on-disk copy")