From 2d346e73bc7ebc07d28a500716e82b169c49fa5a Mon Sep 17 00:00:00 2001 From: jesopo Date: Thu, 10 Jan 2019 13:32:19 +0000 Subject: [PATCH] Seperate out issue/pr logic into their own functions and add !gh that will get info on either an issue or a pull request (github.py) --- modules/github.py | 67 +++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/modules/github.py b/modules/github.py index bf4472ac..ed60e3a8 100644 --- a/modules/github.py +++ b/modules/github.py @@ -52,46 +52,69 @@ class Module(ModuleManager.BaseModule): raise utils.EventError("Issue number must be a number") return username, repository, number + def _gh_issue(self, event, page): + labels = [label["name"] for label in page.data["labels"]] + url = self._short_url(page.data["html_url"]) + + event["stdout"].write("(%s/%s issue#%s, %s) %s [%s] %s" % ( + username, repository, number, page.data["state"], + page.data["title"], ", ".join(labels), url)) + def _gh_get_issue(self, username, repository, number): + return utils.http.request( + API_ISSUE_URL % (username, repository, number), + json=True) + @utils.hook("received.command.ghissue", min_args=1) def github_issue(self, event): username, repository, number = self._parse_ref( event["target"], event["args_split"][0]) - page = utils.http.request( - API_ISSUE_URL % (username, repository, number), - json=True) + page = self._gh_get_issue(username, repository, number) if page and page.code == 200: - labels = [label["name"] for label in page.data["labels"]] - url = self._short_url(page.data["html_url"]) - - event["stdout"].write("(%s/%s issue#%s, %s) %s [%s] %s" % ( - username, repository, number, page.data["state"], - page.data["title"], ", ".join(labels), url)) + self._gh_issue(event, page) else: event["stderr"].write("Could not find issue") + def _gh_pull(self, event, page): + repo_from = page.data["head"]["label"] + repo_to = page.data["base"]["label"] + added = self._added(page.data["additions"]) + removed = self._removed(page.data["deletions"]) + url = self._short_url(page.data["html_url"]) + + event["stdout"].write( + "(%s/%s pull#%s, %s) [%s/%s] %s→%s - %s %s" % ( + username, repository, number, page.data["state"], + added, removed, repo_from, repo_to, page.data["title"], url)) + def _gh_get_pull(self, username, repository, number): + return utils.http.request( + API_PULL_URL % (username, repository, number), + json=True) @utils.hook("received.command.ghpull", min_args=1) def github_pull(self, event): username, repository, number = self._parse_ref( event["target"], event["args_split"][0]) + page = self._gh_get_pull(username, repository, number) - page = utils.http.request( - API_PULL_URL % (username, repository, number), - json=True) if page and page.code == 200: - repo_from = page.data["head"]["label"] - repo_to = page.data["base"]["label"] - added = self._added(page.data["additions"]) - removed = self._removed(page.data["deletions"]) - url = self._short_url(page.data["html_url"]) - - event["stdout"].write( - "(%s/%s pull#%s, %s) [%s/%s] %s→%s - %s %s" % ( - username, repository, number, page.data["state"], - added, removed, repo_from, repo_to, page.data["title"], url)) + self._gh_pull(event, page) else: event["stderr"].write("Could not find pull request") + @utils.hook("received.command.gh", min_args=1) + def github(self, event): + username, repository, number = self._parse_ref( + event["target"], event["args_split"][0]) + 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) + else: + self._gh_issue(event, page) + else: + event["stderr"].write("Issue/PR not found") + @utils.hook("api.post.github") def webhook(self, event): payload = event["data"].decode("utf8")