Skip to content

Commit

Permalink
Refactor/google drive module (#392)
Browse files Browse the repository at this point in the history
* fix: render commands available in dev only

* fix: move google-service command to bottom

* feat: make test command available in dev only

* fix: fix mypy path for current project structure

* fix: update pylint cwd

* fix: ignore google.oauth2 missing stub warning

* fix: config flake8 instead of pylint

* feat: add a decorator to handle api calls errors

* feat: refactor basic drive functions

* feat: add get_file_by_name function

* feat: add list metadata function

* feat: use new drive module functions

* fix: shorten test command

* fix: handle invalid folder

* feat: modify wrapper to provide context

* fix: add missing parameter to list_metadata

* fix: surround function name w/ single quotes

* fix: google drive healthcheck

* fix: matching error message

* feat: add healtcheck message

* fix: update module docstrings w/ more details

* fix: remove import from google_service

* fix: update module docstring

* feat: add basic docstring for Docs module

* fix: replicate current tests for conformity

* fix: add loop to handle pagination

Co-authored-by: Pat Heard <[email protected]>

* fix: ensure all files are returned

* fix: add test to handle pagination

* fix: log errors

* fix: remove unused variables

* feat: include ValueError logging in the wrapper function

* fix: adjust tests to handle logging errors

* fix: test names

* fix: lint

---------

Co-authored-by: Pat Heard <[email protected]>
  • Loading branch information
gcharest and patheard authored Feb 2, 2024
1 parent 9a0aaab commit 7504b42
Show file tree
Hide file tree
Showing 12 changed files with 650 additions and 44 deletions.
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.flake8",
"ms-python.pylint",
"ms-python.mypy-type-checker",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg",
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"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"
}
37 changes: 12 additions & 25 deletions app/commands/google_service.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Testing new google service (will be removed)"""
import os

from integrations.google_workspace.google_service import get_google_service
from integrations.google_workspace import google_drive
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 = list_folders()
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}*"}}
Expand All @@ -25,25 +27,10 @@ def open_modal(client, body):
client.views_open(trigger_id=body["trigger_id"], view=view)


def google_service_command(client, body):
open_modal(client, body)


def list_folders():
service = get_google_service("drive", "v3")
results = (
service.files()
.list(
pageSize=25,
supportsAllDrives=True,
includeItemsFromAllDrives=True,
corpora="drive",
q="parents in '{}' and mimeType = 'application/vnd.google-apps.folder' and trashed=false and not name contains '{}'".format(
SRE_INCIDENT_FOLDER, "Templates"
),
driveId=SRE_DRIVE_ID,
fields="nextPageToken, files(id, name)",
)
.execute()
)
return results.get("files", [])
def google_service_command(client, body, respond):
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
open_modal(client, body, folders)
10 changes: 8 additions & 2 deletions app/commands/sre.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
\n - show the version of the SRE Bot
\n - montre la version du bot SRE"""

PREFIX = os.environ.get("PREFIX", "")


def sre_command(ack, command, logger, respond, client, body):
ack()
Expand All @@ -47,8 +49,12 @@ def sre_command(ack, command, logger, respond, client, body):
webhook_helper.handle_webhook_command(args, client, body, respond)
case "version":
respond(f"SRE Bot version: {os.environ.get('GIT_SHA', 'unknown')}")
case "google-service":
google_service.google_service_command(client, body)
case "google":
if PREFIX == "dev-":
google_service.google_service_command(client, body, respond)
else:
respond("This command is only available in the dev environment.")
return
case _:
respond(
f"Unknown command: `{action}`. Type `/sre help` to see a list of commands. \nCommande inconnue: `{action}`. Entrez `/sre help` pour une liste des commandes valides"
Expand Down
4 changes: 4 additions & 0 deletions app/integrations/google_workspace/google_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Google Docs module.
This module provides functions to create and manipulate Google Docs.
"""
Loading

0 comments on commit 7504b42

Please sign in to comment.