Add auto-github
setting to automatically match and resolve github issue/pr
refs
This commit is contained in:
parent
e0311c4b73
commit
20514cc871
1 changed files with 40 additions and 20 deletions
|
@ -1,4 +1,4 @@
|
||||||
import datetime, itertools, json, math, urllib.parse
|
import datetime, itertools, json, math, re, urllib.parse
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
|
||||||
COLOR_BRANCH = utils.consts.ORANGE
|
COLOR_BRANCH = utils.consts.ORANGE
|
||||||
|
@ -8,6 +8,8 @@ COLOR_NEUTRAL = utils.consts.LIGHTGREY
|
||||||
COLOR_NEGATIVE = utils.consts.RED
|
COLOR_NEGATIVE = utils.consts.RED
|
||||||
COLOR_ID = utils.consts.PINK
|
COLOR_ID = utils.consts.PINK
|
||||||
|
|
||||||
|
REGEX_ISSUE = re.compile("(\s+/\s+)?#\d+")
|
||||||
|
|
||||||
FORM_ENCODED = "application/x-www-form-urlencoded"
|
FORM_ENCODED = "application/x-www-form-urlencoded"
|
||||||
|
|
||||||
COMMIT_URL = "https://github.com/%s/commit/%s"
|
COMMIT_URL = "https://github.com/%s/commit/%s"
|
||||||
|
@ -102,6 +104,9 @@ CHECK_RUN_FAILURES = ["failure", "cancelled", "timed_out", "action_required"]
|
||||||
@utils.export("channelset", {"setting": "github-prevent-highlight",
|
@utils.export("channelset", {"setting": "github-prevent-highlight",
|
||||||
"help": "Enable/disable preventing highlights",
|
"help": "Enable/disable preventing highlights",
|
||||||
"validate": utils.bool_or_none})
|
"validate": utils.bool_or_none})
|
||||||
|
@utils.export("channelset", {"setting": "auto-github",
|
||||||
|
"help": "Enable/disable automatically getting github issue/PR info",
|
||||||
|
"validate": utils.bool_or_none})
|
||||||
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("#")
|
||||||
|
@ -118,7 +123,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
raise utils.EventError("Issue number must be a number")
|
raise utils.EventError("Issue number must be a number")
|
||||||
return username, repository, number
|
return username, repository, number
|
||||||
|
|
||||||
def _gh_issue(self, event, page, username, repository, number):
|
def _parse_issue(self, event, page, username, repository, number):
|
||||||
repo = utils.irc.color("%s/%s" % (username, repository), COLOR_REPO)
|
repo = utils.irc.color("%s/%s" % (username, repository), COLOR_REPO)
|
||||||
number = utils.irc.color("#%s" % number, COLOR_ID)
|
number = utils.irc.color("#%s" % number, COLOR_ID)
|
||||||
labels = [label["name"] for label in page.data["labels"]]
|
labels = [label["name"] for label in page.data["labels"]]
|
||||||
|
@ -134,9 +139,9 @@ class Module(ModuleManager.BaseModule):
|
||||||
elif state == "closed":
|
elif state == "closed":
|
||||||
state = utils.irc.color("closed", COLOR_NEGATIVE)
|
state = utils.irc.color("closed", COLOR_NEGATIVE)
|
||||||
|
|
||||||
event["stdout"].write("(%s issue%s, %s) %s %s%s" % (
|
return "(%s issue%s, %s) %s %s%s" % (
|
||||||
repo, number, state, page.data["title"], labels_str, url))
|
repo, number, state, page.data["title"], labels_str, url)
|
||||||
def _gh_get_issue(self, username, repository, number):
|
def _get_issue(self, username, repository, number):
|
||||||
return utils.http.request(
|
return utils.http.request(
|
||||||
API_ISSUE_URL % (username, repository, number),
|
API_ISSUE_URL % (username, repository, number),
|
||||||
json=True)
|
json=True)
|
||||||
|
@ -156,7 +161,7 @@ class Module(ModuleManager.BaseModule):
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Could not find issue")
|
event["stderr"].write("Could not find issue")
|
||||||
|
|
||||||
def _gh_pull(self, event, page, username, repository, number):
|
def _parse_pull(self, event, page, username, repository, number):
|
||||||
repo = utils.irc.color("%s/%s" % (username, repository), COLOR_REPO)
|
repo = utils.irc.color("%s/%s" % (username, repository), COLOR_REPO)
|
||||||
number = utils.irc.color("#%s" % number, COLOR_ID)
|
number = utils.irc.color("#%s" % number, COLOR_ID)
|
||||||
branch_from = page.data["head"]["label"]
|
branch_from = page.data["head"]["label"]
|
||||||
|
@ -173,11 +178,10 @@ class Module(ModuleManager.BaseModule):
|
||||||
elif state == "closed":
|
elif state == "closed":
|
||||||
state = utils.irc.color("closed", COLOR_NEGATIVE)
|
state = utils.irc.color("closed", COLOR_NEGATIVE)
|
||||||
|
|
||||||
event["stdout"].write(
|
return "(%s PR%s, %s) %s → %s [%s/%s] %s %s" % (
|
||||||
"(%s PR%s, %s) %s → %s [%s/%s] %s %s" % (
|
|
||||||
repo, number, state, branch_from, branch_to, added, removed,
|
repo, number, state, branch_from, branch_to, added, removed,
|
||||||
page.data["title"], url))
|
page.data["title"], url)
|
||||||
def _gh_get_pull(self, username, repository, number):
|
def _get_pull(self, username, repository, number):
|
||||||
return utils.http.request(
|
return utils.http.request(
|
||||||
API_PULL_URL % (username, repository, number),
|
API_PULL_URL % (username, repository, number),
|
||||||
json=True)
|
json=True)
|
||||||
|
@ -196,25 +200,41 @@ class Module(ModuleManager.BaseModule):
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Could not find pull request")
|
event["stderr"].write("Could not find pull request")
|
||||||
|
|
||||||
|
def _get_info(self, target, ref):
|
||||||
|
username, repository, number = self._parse_ref(target, ref)
|
||||||
|
page = self._gh_get_issue(username, repository, number)
|
||||||
|
if page and page.code == 200:
|
||||||
|
if "pull_request" in page.data:
|
||||||
|
pull = self._gh_get_pull(username, repository, number)
|
||||||
|
return self._gh_pull(event, pull, username, repository, number)
|
||||||
|
else:
|
||||||
|
return self._gh_issue(event, page, username, repository, number)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
@utils.hook("received.command.gh", alias_of="github")
|
@utils.hook("received.command.gh", alias_of="github")
|
||||||
@utils.hook("received.command.github", min_args=1)
|
@utils.hook("received.command.github", min_args=1)
|
||||||
def github(self, event):
|
def github(self, event):
|
||||||
if event["target"].get_setting("github-hide-prefix", False):
|
if event["target"].get_setting("github-hide-prefix", False):
|
||||||
event["stdout"].hide_prefix()
|
event["stdout"].hide_prefix()
|
||||||
event["stderr"].hide_prefix()
|
event["stderr"].hide_prefix()
|
||||||
|
result = self._get_info(event["target"], event["args_split"][0])
|
||||||
username, repository, number = self._parse_ref(
|
if not result == None:
|
||||||
event["target"], event["args_split"][0])
|
event["stdout"].write(result)
|
||||||
page = self._gh_get_issue(username, repository, number)
|
|
||||||
if page and page.code == 200:
|
|
||||||
if "pull_request" in page.data:
|
|
||||||
pull = self._gh_get_pull(username, repository, number)
|
|
||||||
self._gh_pull(event, pull, username, repository, number)
|
|
||||||
else:
|
|
||||||
self._gh_issue(event, page, username, repository, number)
|
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Issue/PR not found")
|
event["stderr"].write("Issue/PR not found")
|
||||||
|
|
||||||
|
@utils.hook("received.message.channel")
|
||||||
|
def channel_message(self, event):
|
||||||
|
match = REGEX_ISSUE.search(event["message"])
|
||||||
|
if match and event["channel"].get_setting("auto-github", False):
|
||||||
|
result = self._get_info(event["channel"], match.group(0))
|
||||||
|
if result:
|
||||||
|
hide_prefix = channel.get_setting("github-hide-prefix", False)
|
||||||
|
self.events.on("send.stdout").call(target=event["channel"],
|
||||||
|
module_name="Github", server=server, message=result,
|
||||||
|
hide_prefix=hide_prefix)
|
||||||
|
|
||||||
@utils.hook("received.command.ghwebhook", min_args=1, channel_only=True)
|
@utils.hook("received.command.ghwebhook", min_args=1, channel_only=True)
|
||||||
def github_webhook(self, event):
|
def github_webhook(self, event):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue