-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0f3f92
commit 0733baa
Showing
4 changed files
with
50 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[Core] | ||
Name = Github | ||
Module = github | ||
DependsOn = Archive | ||
|
||
[Documentation] | ||
Description = Plugin to recieve webhooks from Github and route it to Zulip | ||
|
||
[Python] | ||
Version = 3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
from itertools import chain | ||
|
||
import requests | ||
import zulip | ||
from errbot import BotPlugin, webhook | ||
|
||
CONFIG_TEMPLATE = { | ||
|
@@ -19,24 +18,21 @@ | |
} | ||
|
||
|
||
class Githubzulip(BotPlugin): | ||
def get_configuration_template(self): | ||
class Github(BotPlugin): | ||
@staticmethod | ||
def get_configuration_template(): | ||
return CONFIG_TEMPLATE | ||
|
||
def configure(self, configuration): | ||
if configuration is not None and configuration != {}: | ||
config = dict(chain(CONFIG_TEMPLATE.items(), configuration.items())) | ||
else: | ||
config = CONFIG_TEMPLATE | ||
super(Githubzulip, self).configure(config) | ||
super(Github, self).configure(config) | ||
|
||
def get_user(self, gh): | ||
gh_u = "" | ||
client = zulip.Client( | ||
site="https://cern-rcs-sis.zulipchat.com", | ||
email="[email protected]", | ||
api_key=os.environ["BOT_ZULIP_KEY"], | ||
) | ||
client = self._bot.client() | ||
result = client.get_members() | ||
result = client.get_members({"client_gravatar": False}) | ||
result = client.get_members({"include_custom_profile_fields": True}) | ||
|
@@ -76,26 +72,37 @@ def stream(self, org, repo): | |
else: | ||
return org | ||
|
||
def topic(self, repo, event, ref): | ||
return f"{repo} / {event} / {ref}" | ||
@staticmethod | ||
def topic(repo, item, ref): | ||
return f"{repo}/{item}/{ref}" | ||
|
||
def room(self, payload, event_header): | ||
event_type = self.event_type[event_header] | ||
if event_type is None: | ||
item = self.item(event_header) | ||
if item is None: | ||
return None, None | ||
|
||
org, repo = payload["repository"]["full_name"].split("/") | ||
stream = self.stream(org, repo) | ||
ref = str(payload[event_type]["number"]) | ||
topic = self.topic(repo, payload, ref) | ||
ref = str(payload[item]["number"]) | ||
topic = self.topic(repo, item, ref) | ||
return stream, topic | ||
|
||
def event_type(self, event): | ||
if event.starswith("issue"): | ||
@staticmethod | ||
def item(event): | ||
if event.startswith("issue"): | ||
return "issue" | ||
elif event.starswith("pull_request"): | ||
elif event.startswith("pull_request"): | ||
return "pull_request" | ||
return None | ||
else: | ||
return None | ||
|
||
@staticmethod | ||
def has_been_closed(payload): | ||
return payload["action"] == "closed" | ||
|
||
@staticmethod | ||
def has_been_reopened(payload): | ||
return payload["action"] == "reopened" | ||
|
||
@webhook("/github", raw=True) | ||
def github(self, request): | ||
|
@@ -108,6 +115,10 @@ def github(self, request): | |
if stream is None: | ||
return None | ||
|
||
if payload["action"] == "reopened": | ||
archive_topic = self.get_plugin("Archive").archived_topic(stream, topic) | ||
self.get_plugin("Archive").restore_topic(archive_topic) | ||
|
||
params = { | ||
"api_key": os.environ["BOT_GITHUB_KEY"], | ||
"stream": stream, | ||
|
@@ -120,3 +131,6 @@ def github(self, request): | |
data=request.get_data(), | ||
) | ||
self.log.info(response.status_code) | ||
|
||
if payload["action"] == "closed": | ||
self.get_plugin("Archive").archive_topic(stream, topic) |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
certifi==2022.12.7 | ||
charset-normalizer==3.0.1 | ||
idna==3.4 | ||
packaging==23.0 | ||
pip-review==1.3.0 | ||
requests==2.28.2 | ||
urllib3==1.26.14 |