Add auto-github-timeout to prevent duplicate auto-githubs

This commit is contained in:
jesopo 2019-06-16 16:57:22 +01:00
parent b4d61ecd55
commit 002e1333a4

View file

@ -113,6 +113,9 @@ CHECK_RUN_FAILURES = ["failure", "cancelled", "timed_out", "action_required"]
@utils.export("channelset", {"setting": "auto-github", @utils.export("channelset", {"setting": "auto-github",
"help": "Enable/disable automatically getting github issue/PR info", "help": "Enable/disable automatically getting github issue/PR info",
"validate": utils.bool_or_none, "example": "on"}) "validate": utils.bool_or_none, "example": "on"})
@utils.export("channelset", {"setting": "auto-github-timeout",
"help": "Set amount of seconds between auto-github duplicates",
"validate": utils.int_or_none, "example": "300"})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _parse_ref(self, channel, ref): def _parse_ref(self, channel, ref):
repo, _, number = ref.rpartition("#") repo, _, number = ref.rpartition("#")
@ -235,6 +238,20 @@ class Module(ModuleManager.BaseModule):
else: else:
event["stderr"].write("Issue/PR not found") event["stderr"].write("Issue/PR not found")
def _cache_ref(self, ref):
return "auto-github-%s" % ref.lower()
def _auto_github_timeout(self, channel, ref):
timeout = channel.get_setting("auto-github-timeout", None)
if not timeout == None:
cache = self._cache_ref(ref)
if not self.bot.cache.has_item(cache):
self.bot.cache.temporary_cache(cache, timeout)
return True
else:
return False
else:
return True
@utils.hook("command.regex") @utils.hook("command.regex")
def url_regex(self, event): def url_regex(self, event):
""" """
@ -245,6 +262,7 @@ class Module(ModuleManager.BaseModule):
event.eat() event.eat()
ref = "%s/%s#%s" % (event["match"].group(1), ref = "%s/%s#%s" % (event["match"].group(1),
event["match"].group(2), event["match"].group(4)) event["match"].group(2), event["match"].group(4))
if self._auto_github_timeout(event["target"], ref):
try: try:
result = self._get_info(event["target"], ref) result = self._get_info(event["target"], ref)
except utils.EventError: except utils.EventError:
@ -262,6 +280,8 @@ class Module(ModuleManager.BaseModule):
""" """
if event["target"].get_setting("auto-github", False): if event["target"].get_setting("auto-github", False):
event.eat() event.eat()
ref = event["match"].group(0)
if self._auto_github_timeout(event["target"], ref):
try: try:
result = self._get_info(event["target"], result = self._get_info(event["target"],
event["match"].group(0)) event["match"].group(0))