generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create slack user integration * feat: create reusable slack get_channels function * fix: fmt * chore: remove unused configs * feat: add get user id from slack request * feat: use new get user locale function * feat: move incident feature to own module * fix: incident imports * feat: move get stale channels to slack integration * feat: use slack integrations to get stale channels * feat: use slack integrations get stale channels * fix: use slack integration instead of utils * chore: cleanup unused functions * feat: move google docs function to matching module * fix: fmt * fix: google_docs import * fix: google docs import and test * fix: fmt * feat: transfer parse_command to slack integration * fix: parse_command imports * feat: transfer slack emoji functions to incident module * fix: emoji functions imports * feat: transfer user id to handle fn to slack integration * fix: imports * feat: transfer log to sentinel fn to sentinel integration * fix: update log to sentinel imports * fix: fmt * feat: transfer remaining function to server module * fix: log_ops imports * chore: cleanup unused folder and files * fix: add register function and move emoji fns to incident * chore: cleanup comments * fix: import incident pattern from incident_helper
- Loading branch information
Showing
39 changed files
with
1,073 additions
and
831 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
Empty file.
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
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
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 @@ | ||
"""Slack Integration Package. | ||
This package contains the Slack integration modules. Contains: | ||
- channels: Module containing the channel related functionality for the Slack integration. | ||
- users: Module containing the user related functionality for the Slack integration. | ||
""" |
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,77 @@ | ||
"""Slack Channels Module. | ||
This module contains the channel related functionality for the Slack integration.""" | ||
|
||
import re | ||
import time | ||
from datetime import datetime, timedelta | ||
|
||
|
||
def get_channels(client, pattern=None): | ||
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 | ||
|
||
if pattern is not None: | ||
# filter the channels to only include channels that match the pattern | ||
for channel in response.get("channels", []): | ||
if re.match(pattern, channel["name"]): | ||
channels.append(channel) | ||
else: | ||
channels.extend(response.get("channels", [])) | ||
|
||
# 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 | ||
|
||
|
||
def get_stale_channels(client, pattern=None): | ||
STALE_PERIOD = timedelta(days=14) | ||
now = datetime.now() | ||
stale_channels = [] | ||
channels = list( | ||
filter( | ||
lambda x: x["created"] < time.mktime((now - STALE_PERIOD).timetuple()), | ||
get_channels(client, pattern=pattern), | ||
) | ||
) | ||
stale_channels = list( | ||
filter( | ||
lambda x: len(get_messages_in_time_period(client, x["id"], STALE_PERIOD)) | ||
== 0, | ||
channels, | ||
) | ||
) | ||
return stale_channels | ||
|
||
|
||
def get_messages_in_time_period(client, channel_id, time_delta): | ||
client.conversations_join(channel=channel_id) | ||
messages = client.conversations_history( | ||
channel=channel_id, | ||
limit=10, | ||
oldest=time.mktime((datetime.now() - time_delta).timetuple()), | ||
) | ||
if messages["ok"]: | ||
return list( | ||
filter(lambda x: "team" in x, messages["messages"]) | ||
) # Return only messages from users | ||
else: | ||
return [] |
Oops, something went wrong.