Skip to content

Commit

Permalink
Refactor/incident module (#410)
Browse files Browse the repository at this point in the history
* 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
gcharest authored Feb 13, 2024
1 parent 3cd5796 commit e5d5f1c
Show file tree
Hide file tree
Showing 39 changed files with 1,073 additions and 831 deletions.
5 changes: 0 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"python.pylintPath": "/usr/local/py-utils/bin/pylint",
"mypy-type-checker.cwd": "${workspaceFolder}/app",
"pylint.cwd": "${workspaceFolder}/app",
"pylint.path": [
"[\"../../usr/local/py-utils/bin/pylint\"]"
],
"flake8.cwd": "${workspaceFolder}/app",
"python.analysis.extraPaths": [
"${workspaceFolder}/app"]
Expand Down
Empty file removed app/commands/helpers/__init__.py
Empty file.
237 changes: 0 additions & 237 deletions app/commands/utils.py

This file was deleted.

19 changes: 19 additions & 0 deletions app/integrations/google_workspace/google_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
get(document_id: str) -> dict:
Gets a document from Google Docs and returns the document resource.
"""
import logging
import re
from integrations.google_workspace.google_service import (
get_google_service,
handle_google_api_errors,
Expand Down Expand Up @@ -67,3 +69,20 @@ def get(document_id: str) -> dict:
service = get_google_service("docs", "v1")
result = service.documents().get(documentId=document_id).execute()
return result


def extract_google_doc_id(url):
# if the url is empty or None, then log an error
if not url:
logging.error("URL is empty or None")
return None

# Regular expression pattern to match Google Docs ID
pattern = r"https://docs.google.com/document/d/([a-zA-Z0-9_-]+)/"

# Search in the given text for all occurences of pattern
match = re.search(pattern, url)
if match:
return match.group(1)
else:
return None
8 changes: 8 additions & 0 deletions app/integrations/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ def post_data(customer_id, shared_key, body, log_type):
print(response.text)
logging.error(f"Response code: {response.status_code}, log type: {log_type}")
return False


def log_to_sentinel(event, message):
payload = {"event": event, "message": message}
if send_event(payload):
logging.info(f"Sentinel event sent: {payload}")
else:
logging.error(f"Sentinel event failed: {payload}")
7 changes: 7 additions & 0 deletions app/integrations/slack/__init__.py
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.
"""
77 changes: 77 additions & 0 deletions app/integrations/slack/channels.py
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 []
Loading

0 comments on commit e5d5f1c

Please sign in to comment.