Skip to content

Commit 2e8cac1

Browse files
authored
fix: Fix several small bugs.
Fixes #33 and #37.
2 parents 46f0777 + 0da78a1 commit 2e8cac1

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

models/github/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Import all trivial models
66
from .commit import Commit
7-
from .event_type import EventType, convert_str_to_event_type
7+
from .event_type import EventType, convert_keywords_to_events
88
from .issue import Issue
99
from .pull_request import PullRequest
1010
from .ref import Ref

models/github/event_type.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class EventType(Enum):
2121
PULL_MERGED = ("prm", "A Pull Request was merged")
2222
PULL_OPENED = ("pro", "A Pull Request was opened")
2323
PULL_READY = ("prr", "A Pull Request is ready")
24-
ISSUE_OPENED = ("io", "An Issue was opened")
25-
ISSUE_CLOSED = ("ic", "An Issue was closed")
24+
ISSUE_OPENED = ("iso", "An Issue was opened")
25+
ISSUE_CLOSED = ("isc", "An Issue was closed")
2626

2727
# Review
2828
REVIEW = ("rv", "A Review was given on a Pull Request")
@@ -44,15 +44,30 @@ def __init__(self, keyword, docs):
4444
self.docs = docs
4545

4646

47-
def convert_str_to_event_type(event_keyword: str) -> EventType | None:
47+
def convert_keywords_to_events(keywords: list[str]) -> set[EventType]:
4848
"""
49-
Returns the `EventType` member corresponding to the passed keyword.
50-
If no `EventType` is matched, returns `None`.
51-
:param event_keyword: Short string representing the event.
52-
:return: `EventType` member corresponding to the keyword.
49+
Returns a set of `EventType` members corresponding to the passed keywords.
50+
If no `EventType` is matched, returns an empty set.
51+
:param keywords: List of short strings representing the events.
52+
:return: Set of `EventType` members corresponding to the keywords.
5353
"""
54-
for event_type in EventType:
55-
if event_type.keyword == event_keyword:
56-
return event_type
57-
print("Event not in enum")
58-
return None
54+
if len(keywords) == 0 or "default" in keywords:
55+
return {
56+
EventType.BRANCH_CREATED,
57+
EventType.TAG_CREATED,
58+
EventType.PULL_OPENED,
59+
EventType.ISSUE_OPENED,
60+
EventType.REVIEW,
61+
EventType.COMMIT_COMMENT,
62+
EventType.ISSUE_COMMENT,
63+
EventType.PUSH,
64+
EventType.STAR_ADDED,
65+
}
66+
if "all" in keywords or "*" in keywords:
67+
return set(EventType)
68+
return {
69+
event_type
70+
for event_type in EventType
71+
for keyword in keywords
72+
if event_type.keyword == keyword
73+
}

slack_bot/runner.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from bottle import MultiDict
88

9-
from models.github import EventType, convert_str_to_event_type
9+
from models.github import EventType, convert_keywords_to_events
1010
from models.slack import Channel
1111
from utils.json import JSON
1212
from utils.storage import Storage
@@ -60,11 +60,7 @@ def run_subscribe_command(
6060
:param args: `list` of events to subscribe to.
6161
"""
6262
repo: str = args[0]
63-
new_events: set[EventType] = {
64-
convert_str_to_event_type(arg) for arg in args[1:]
65-
}
66-
# Remove all the entries which do not correspond to a correct [EventType].
67-
new_events -= {None}
63+
new_events = convert_keywords_to_events(args[1:])
6864
if repo in self.subscriptions:
6965
channels: set[Channel] = self.subscriptions[repo]
7066
channel: Channel | None = next(
@@ -129,21 +125,21 @@ def run_unsubscribe_command(
129125
if channel is not None:
130126
# If this channel has subscribed to some events
131127
# from this repo, update the list of events.
132-
events = channel.events
133-
for arg in args[1:]:
134-
event: EventType | None = convert_str_to_event_type(arg)
128+
current_events = channel.events
129+
chosen_events = convert_keywords_to_events(args[1:])
130+
for event in chosen_events:
135131
try:
136-
events.remove(event)
132+
current_events.remove(event)
137133
except KeyError:
138134
# This means that the user tried to unsubscribe from
139135
# an event that wasn't subscribed to in the first place.
140136
pass
141137
self.subscriptions[repo].remove(channel)
142-
if len(events) != 0:
138+
if len(current_events) != 0:
143139
self.subscriptions[repo].add(
144140
Channel(
145141
name=current_channel,
146-
events=events,
142+
events=current_events,
147143
)
148144
)
149145
return self.run_list_command(current_channel=current_channel, ephemeral=True)
@@ -186,6 +182,17 @@ def run_list_command(
186182
"type": "divider",
187183
},
188184
]
185+
if len(blocks) == 0:
186+
prompt = "This channel has not yet subscribed to anything."
187+
prompt += "You can subscribe to your favorite repositories "
188+
prompt += "using the `/subscribe` command. For more info, use the `/help` command."
189+
190+
blocks = [
191+
{
192+
"type": "mrkdwn",
193+
"text": prompt,
194+
},
195+
]
189196
return {
190197
"response_type": "ephemeral" if ephemeral else "in_channel",
191198
"blocks": blocks,
@@ -222,9 +229,12 @@ def run_help_command() -> dict[str, Any]:
222229
"text": (
223230
"*Events*\n"
224231
"GitHub events are abbreviated as follows:\n"
232+
"0. `default` or no arguments: Subscribe "
233+
"to the most common and important events.\n"
234+
"1. `all` or `*`: Subscribe to every supported event.\n"
225235
+ " ".join(
226236
[
227-
f"{i + 1}. `{event.keyword}`: {event.docs}\n"
237+
f"{i + 2}. `{event.keyword}`: {event.docs}\n"
228238
for i, event in enumerate(EventType)
229239
]
230240
)

utils/storage.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
import os
77

8-
from models.github import EventType, convert_str_to_event_type
8+
from models.github import EventType, convert_keywords_to_events
99
from models.slack import Channel
1010

1111

@@ -45,10 +45,7 @@ def import_subscriptions() -> dict[str, set[Channel]]:
4545
repo: {
4646
Channel(
4747
name=channel,
48-
events={
49-
convert_str_to_event_type(event_keyword)
50-
for event_keyword in events
51-
},
48+
events=convert_keywords_to_events(events),
5249
)
5350
for channel, events in channels.items()
5451
}
@@ -58,7 +55,7 @@ def import_subscriptions() -> dict[str, set[Channel]]:
5855
else:
5956
# Default subscriptions, for dev and testing
6057
return {
61-
"fake-rdrive-flutter": {
58+
"github-slack-bot": {
6259
Channel("#github-slack-bot", set(EventType)),
6360
}
6461
}

0 commit comments

Comments
 (0)