Skip to content

Commit

Permalink
Fixing up stale incidents not displaying (#359)
Browse files Browse the repository at this point in the history
* Fixing up the stale incidents commands. Stale incidents were not showing

* Removing a print statement
  • Loading branch information
sylviamclaughlin authored Jan 12, 2024
1 parent edc4463 commit 5487e06
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/commands/helpers/incident_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def stale_incidents(client, body, ack):
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Loading stale incident list ...",
"text": "Loading stale incident list ...(this may take a minute)",
},
}
],
Expand Down
33 changes: 27 additions & 6 deletions app/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@
logging.basicConfig(level=logging.INFO)


# Get all incident channels
def get_incident_channels(client):
channels = []
response = client.conversations_list(
exclude_archived=True, limit=1000, types="public_channel"
)
if response["ok"]:
channels = list(
filter(lambda x: x["name"].startswith("incident-20"), response["channels"])
cursor = None

# Execute while the next_cursor is not empty. We need to iterate through the collection limiting to a max of 100 channels
# at a time. The cursor is used to keep track of the current position in the collection. This is due to Slack's pagination
# and the way it hanles retrieval of channels.
while True:
response = client.conversations_list(
exclude_archived=True, limit=100, types="public_channel", cursor=cursor
)

# if we did not get a successful response, break out of the loop
if not response.get("ok"):
break

# filter the channels to only include incident channels
for channel in response.get("channels", []):
if channel["name"].startswith("incident-20"):
channels.append(channel)

# get the next cursor
cursor = response.get("response_metadata", {}).get("next_cursor")

# if the cursor is empty, break out of the loop
if not cursor:
break

# return the list of incident channels
return channels


Expand Down
36 changes: 36 additions & 0 deletions app/tests/commands/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_get_incident_channels():
"name": "incident-2022-channel",
},
],
"response_metadata": {"next_cursor": ""},
}
assert utils.get_incident_channels(client) == [
{
Expand All @@ -23,6 +24,41 @@ def test_get_incident_channels():
]


# Test get_incident_channels with multiple pages of results
def test_get_incident_channels_with_multiple_pages():
client = MagicMock()

# Define mock responses
mock_response_page_1 = {
"ok": True,
"channels": [
{"name": "incident-2021-alpha"},
{"name": "general"},
{"name": "incident-2020-beta"},
],
"response_metadata": {"next_cursor": "cursor123"},
}
mock_response_page_2 = {
"ok": True,
"channels": [{"name": "random"}, {"name": "incident-2022-gamma"}],
"response_metadata": {"next_cursor": ""},
}

# Set the side_effect of the conversations_list method
client.conversations_list.side_effect = [mock_response_page_1, mock_response_page_2]

# Call the function
result = utils.get_incident_channels(client)

# Verify results
expected_channels = [
{"name": "incident-2021-alpha"},
{"name": "incident-2020-beta"},
{"name": "incident-2022-gamma"},
]
assert result == expected_channels


def test_get_messages_in_time_period():
client = MagicMock()
client.conversations_history.return_value = {
Expand Down

0 comments on commit 5487e06

Please sign in to comment.