Verified Commit 4d3b0bba authored by A. Wilcox's avatar A. Wilcox 🦊
Browse files

IRC: Drastically simplify code

DRY up the send message code.
parent 43a273aa
......@@ -83,10 +83,22 @@ class IRC:
def coros(self):
return [inst.connect() for inst in self.network_instances.values()]
def push(self, project, push):
def _determine_channels(self, project: str) -> set:
"""Determine the channels that are interested in an event."""
channels = set(self.projects[project] +
self.projects[project.split('/', 1)[0] + '/*'] +
self.projects['*/*'])
return channels
def _send_to_proj_channels(self, project: str, msgs: list):
"""Send a list of messages to channels interested in a project's events."""
channels = self._determine_channels(project)
for channel in channels:
network, irc_chan = channel.split(':', 1)
for msg in msgs:
self.network_instances[network].send('PRIVMSG', [irc_chan, msg])
def push(self, project, push):
commit_msgs = []
push_msg = '\x02{}\x02: {} pushed '.format(project, push['user']['name'])
......@@ -96,6 +108,7 @@ class IRC:
else:
push_msg += 'a commit '
push_msg += 'to the \x02{}\x02 branch:'.format(push['branch'])
commit_msgs.append(push_msg)
for commit in push['commits']:
try:
......@@ -114,30 +127,14 @@ class IRC:
commit_msgs.append(msg)
break
for channel in channels:
network, irc_chan = channel.split(':', 1)
self.network_instances[network].send('PRIVMSG', [irc_chan,
push_msg])
for msg in commit_msgs:
self.network_instances[network].send('PRIVMSG', [irc_chan, msg])
self._send_to_proj_channels(project, commit_msgs)
def merge(self, project, merge):
channels = set(self.projects[project] +
self.projects[project.split('/', 1)[0] + '/*'] +
self.projects['*/*'])
msg = '\x02{}\x02: {} opened a merge request for the \x02{}\x02 branch: {}'
msg = msg.format(project, merge['user']['name'], merge['branch'], merge['url'])
for channel in channels:
network, irc_chan = channel.split(':', 1)
self.network_instances[network].send('PRIVMSG', [irc_chan, msg])
self._send_to_proj_channels(project, [msg])
def pipe(self, project, pipe):
channels = set(self.projects[project] +
self.projects[project.split('/', 1)[0] + '/*'] +
self.projects['*/*'])
msg = '\x02{}\x02: Pipeline #{} for branch \x02{}\x02 status: {} - {}'
msg = msg.format(project, pipe['id'], pipe['branch'], pipe['status'], pipe['url'])
for channel in channels:
network, irc_chan = channel.split(':', 1)
self.network_instances[network].send('PRIVMSG', [irc_chan, msg])
self._send_to_proj_channels(project, [msg])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment