Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/google scheduler #476

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/integrations/google_workspace/google_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from integrations.google_workspace.google_service import (
handle_google_api_errors,
execute_google_api_call,
DEFAULT_DELEGATED_ADMIN_EMAIL,
)
from integrations.utils.api import convert_string_to_camel_case

Expand Down Expand Up @@ -35,13 +36,13 @@ def get_freebusy(time_min, time_max, items, **kwargs):
"items": items,
}
body.update({convert_string_to_camel_case(k): v for k, v in kwargs.items()})

return execute_google_api_call(
"calendar",
"v3",
"freebusy",
"query",
scopes=["https://www.googleapis.com/auth/calendar.readonly"],
delegated_user_email=DEFAULT_DELEGATED_ADMIN_EMAIL,
scopes=["https://www.googleapis.com/auth/calendar"],
body=body,
)

Expand Down
6 changes: 0 additions & 6 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from slack_bolt import App
from dotenv import load_dotenv
from modules import (
google_service,
secret,
atip,
aws,
Expand Down Expand Up @@ -67,11 +66,6 @@ def main(bot):
stop_run_continuously = scheduled_tasks.run_continuously()
server_app.add_event_handler("shutdown", lambda: stop_run_continuously.set())

# Register Google Service command for dev purposes only
if PREFIX == "dev-":
bot.command(f"/{PREFIX}google")(google_service.google_service_command)
bot.view("google_service_view")(google_service.open_modal)


def get_bot():
SLACK_TOKEN = os.environ.get("SLACK_TOKEN", None)
Expand Down
93 changes: 93 additions & 0 deletions app/modules/dev/google_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Testing new google service (will be removed)"""
import os
from datetime import datetime, timedelta, timezone
from integrations.google_workspace import (
google_calendar,
)
from dotenv import load_dotenv

load_dotenv()

SRE_DRIVE_ID = os.environ.get("SRE_DRIVE_ID")
SRE_INCIDENT_FOLDER = os.environ.get("SRE_INCIDENT_FOLDER")
INCIDENT_TEMPLATE = os.environ.get("INCIDENT_TEMPLATE")


def open_modal(client, body, folders):
if not folders:
return
folder_names = [i["name"] for i in folders]
blocks = [
{"type": "section", "text": {"type": "mrkdwn", "text": f"*{name}*"}}
for name in folder_names
]
view = {
"type": "modal",
"title": {"type": "plain_text", "text": "Folder List"},
"blocks": blocks,
}
client.views_open(trigger_id=body["trigger_id"], view=view)


def google_service_command(ack, client, body, respond):
# ack()

# folders = google_drive.list_folders_in_folder(SRE_INCIDENT_FOLDER)

# if not folders:
# respond("The folder ID is invalid. Please check the environment variables.")
# return

# respond(f"Found {len(folders)} folders.")

# files = google_drive.list_files_in_folder(SRE_INCIDENT_FOLDER)

# if not files:
# respond("The folder ID is invalid. Please check the environment variables.")
# return

# respond(f"Found {len(files)} files.")

# find_docs = google_drive.get_file_by_name('incidents', SRE_INCIDENT_FOLDER)
# if not find_docs:
# respond("The folder ID is invalid. Please check the environment variables.")
# return

# respond(f"Found {len(find_docs)} files.")
items = [
{"id": "[email protected]"},
{"id": "[email protected]"},
]
now = datetime.now(timezone.utc)
days = 1
# time_min is the current time + days and time_max is the current time + 60 days + days
time_min = (now + timedelta(days=days)).isoformat()
time_max = (now + timedelta(days=(60 + days))).isoformat()
freebusy_results = google_calendar.get_freebusy(time_min, time_max, items)
# respond(freebusy_results)
(
first_available_start,
first_available_end,
) = google_calendar.find_first_available_slot(freebusy_results, days)
respond(f"First available slot: {first_available_start} to {first_available_end}")

# respond(f"Healthcheck status: {google_drive.healthcheck()}")
# folders = google_drive.list_folders_in_folder(SRE_INCIDENT_FOLDER)
# if not folders:
# respond("The folder ID is invalid. Please check the environment variables.")
# return
# users = google_directory.list_users()
# if not users:
# respond("There was an error retrieving the users.")
# return
# respond(f"Found {len(users)} users.")
# groups = google_directory.list_groups()
# if not groups:
# respond("There was an error retrieving the groups.")
# return
# respond(f"Found {len(groups)} groups.")
# group_members = google_directory.list_group_members(groups[0]["id"])
# if not group_members:
# respond("There was an error retrieving the group members.")
# return
# respond(f"Found {len(group_members)} group members in group {groups[0]['name']}.")
50 changes: 0 additions & 50 deletions app/modules/google_service.py

This file was deleted.

7 changes: 3 additions & 4 deletions app/modules/sre/sre.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import os

from modules.incident import incident_helper
from modules import google_service
from modules.sre import geolocate_helper, webhook_helper
from modules.dev import aws_dev
from modules.dev import aws_dev, google_service
from integrations.slack import commands as slack_commands

help_text = """
Expand Down Expand Up @@ -62,13 +61,13 @@ def sre_command(ack, command, logger, respond, client, body):
respond(f"SRE Bot version: {os.environ.get('GIT_SHA', 'unknown')}")
case "google":
if PREFIX == "dev-":
google_service.google_service_command(client, body, respond)
google_service.google_service_command(ack, client, body, respond)
else:
respond("This command is only available in the dev environment.")
return
case "aws":
if PREFIX == "dev-":
aws_dev.aws_dev_command(client, body, respond)
aws_dev.aws_dev_command(ack, client, body, respond)
else:
respond("This command is only available in the dev environment.")
return
Expand Down
14 changes: 12 additions & 2 deletions app/tests/integrations/google_workspace/test_google_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def items():
return [{"id": "calendar1"}, {"id": "calendar2"}]


@patch(
"integrations.google_workspace.google_calendar.DEFAULT_DELEGATED_ADMIN_EMAIL",
"test_email",
)
@patch("integrations.google_workspace.google_calendar.execute_google_api_call")
def test_get_freebusy_required_args_only(mock_execute_google_api_call, items):
mock_execute_google_api_call.return_value = {}
Expand All @@ -77,7 +81,8 @@ def test_get_freebusy_required_args_only(mock_execute_google_api_call, items):
"v3",
"freebusy",
"query",
scopes=["https://www.googleapis.com/auth/calendar.readonly"],
delegated_user_email="test_email",
scopes=["https://www.googleapis.com/auth/calendar"],
body={
"timeMin": time_min,
"timeMax": time_max,
Expand All @@ -86,6 +91,10 @@ def test_get_freebusy_required_args_only(mock_execute_google_api_call, items):
)


@patch(
"integrations.google_workspace.google_calendar.DEFAULT_DELEGATED_ADMIN_EMAIL",
"test_email",
)
@patch("integrations.google_workspace.google_calendar.execute_google_api_call")
def test_get_freebusy_optional_args(mock_execute_google_api_call, items):
mock_execute_google_api_call.return_value = {}
Expand All @@ -109,7 +118,8 @@ def test_get_freebusy_optional_args(mock_execute_google_api_call, items):
"v3",
"freebusy",
"query",
scopes=["https://www.googleapis.com/auth/calendar.readonly"],
delegated_user_email="test_email",
scopes=["https://www.googleapis.com/auth/calendar"],
body={
"timeMin": time_min,
"timeMax": time_max,
Expand Down
Loading