Skip to content

Commit

Permalink
Cleanup and add auto archiving
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-bergia committed Feb 22, 2023
1 parent d0f3f92 commit 0733baa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
10 changes: 10 additions & 0 deletions github.plug
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
52 changes: 33 additions & 19 deletions githubzulip.py → github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from itertools import chain

import requests
import zulip
from errbot import BotPlugin, webhook

CONFIG_TEMPLATE = {
Expand All @@ -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})
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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)
10 changes: 0 additions & 10 deletions githubzulip.plug

This file was deleted.

7 changes: 7 additions & 0 deletions requirements.txt
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

0 comments on commit 0733baa

Please sign in to comment.