Skip to content

Commit

Permalink
Merge branch 'main' into fresh-team-overview
Browse files Browse the repository at this point in the history
  • Loading branch information
DonnieBLT authored Jan 19, 2025
2 parents 891c973 + b59a909 commit c17b94f
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 21 deletions.
2 changes: 2 additions & 0 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
distribute_bacon,
select_contribution,
)
from website.views.slack_handlers import slack_events
from website.views.teams import (
TeamOverview,
add_member,
Expand Down Expand Up @@ -857,6 +858,7 @@
),
path("projects/create/", create_project, name="create_project"),
path("project/<slug:slug>/", ProjectsDetailView.as_view(), name="projects_detail"),
path("slack/events", slack_events, name="slack_events"),
]

if settings.DEBUG:
Expand Down
40 changes: 20 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ newrelic = "^10.4.0"
[tool.poetry.group.dev.dependencies]
black = "^24.8.0"
isort = "^5.13.2"
ruff = "^0.9.1"
ruff = "^0.9.2"
pre-commit = "^3.8.0"

[tool.isort]
Expand Down
23 changes: 23 additions & 0 deletions website/management/commands/run_daily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

from django.core.management.base import BaseCommand

# from django.core import management
from django.utils import timezone

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "Runs commands scheduled to execute daily"

def handle(self, *args, **options):
try:
logger.info(f"Starting daily scheduled tasks at {timezone.now()}")

# Add commands to be executed daily
# management.call_command('daily_command1')
# management.call_command('daily_command2')
except Exception as e:
logger.error(f"Error in daily tasks: {str(e)}")
raise
23 changes: 23 additions & 0 deletions website/management/commands/run_hourly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

from django.core.management.base import BaseCommand

# from django.core import management
from django.utils import timezone

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "Runs commands scheduled to execute hourly"

def handle(self, *args, **options):
try:
logger.info(f"Starting hourly scheduled tasks at {timezone.now()}")

# We can add hourly commands here
# management.call_command('hourly_command1')
# management.call_command('hourly_command2')
except Exception as e:
logger.error(f"Error in hourly tasks: {str(e)}")
raise
23 changes: 23 additions & 0 deletions website/management/commands/run_monthly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

from django.core.management.base import BaseCommand

# from django.core import management
from django.utils import timezone

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "Runs commands scheduled to execute monthly"

def handle(self, *args, **options):
try:
logger.info(f"Starting monthly scheduled tasks at {timezone.now()}")

# Add commands to be executed monthly
# management.call_command('monthly_command1')
# management.call_command('monthly_command2')
except Exception as e:
logger.error(f"Error in monthly tasks: {str(e)}")
raise
23 changes: 23 additions & 0 deletions website/management/commands/run_weekly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

from django.core.management.base import BaseCommand

# from django.core import management
from django.utils import timezone

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "Runs commands scheduled to execute weekly"

def handle(self, *args, **options):
try:
logger.info(f"Starting weekly scheduled tasks at {timezone.now()}")

# Add commands to be executed weekly
# management.call_command('weekly_command1')
# management.call_command('weekly_command2')
except Exception as e:
logger.error(f"Error in weekly tasks: {str(e)}")
raise
89 changes: 89 additions & 0 deletions website/test_slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from unittest.mock import MagicMock, patch

from django.test import TestCase

from website.views.slack_handlers import (
_handle_contribute_message,
_handle_team_join,
extract_text_from_blocks,
handle_message,
)


class SlackFunctionTests(TestCase):
def setUp(self):
self.mock_client = MagicMock()

def test_extract_text_from_blocks(self):
"""Test extracting text from Slack block format"""
# Test rich text blocks
blocks = [
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [{"type": "text", "text": "I want to contribute"}],
}
],
}
]

self.assertEqual(extract_text_from_blocks(blocks), "I want to contribute")

# Test empty blocks
self.assertEqual(extract_text_from_blocks([]), "")

# Test invalid blocks
self.assertEqual(extract_text_from_blocks(None), "")

@patch("website.views.slack_handlers.client")
def test_handle_contribute_message(self, mock_client):
"""Test contribute message handler"""
message = {
"user": "U123",
"channel": "C123",
"text": "How do I contribute?",
"subtype": None,
}

_handle_contribute_message(message)

mock_client.chat_postMessage.assert_called_once()

# Test message without contribute keyword
message["text"] = "Hello world"
mock_client.chat_postMessage.reset_mock()
_handle_contribute_message(message)
mock_client.chat_postMessage.assert_not_called()

@patch("website.views.slack_handlers.client")
def test_handle_team_join(self, mock_client):
"""Test team join handler"""
mock_client.conversations_open.return_value = {"ok": True, "channel": {"id": "D123"}}

_handle_team_join("U123")

# Should send welcome message in joins channel
mock_client.chat_postMessage.assert_called()

# Should try to open DM
mock_client.conversations_open.assert_called_once_with(users=["U123"])

@patch("website.views.slack_handlers.client")
def test_handle_message(self, mock_client):
"""Test main message handler"""
# Mock bot user ID
mock_client.auth_test.return_value = {"user_id": "BOT123"}

# Test normal user message
payload = {"user": "U123", "text": "contribute", "channel": "C123"}

handle_message(payload)
mock_client.chat_postMessage.assert_called()

# Test bot message (should be ignored)
payload["user"] = "BOT123"
mock_client.chat_postMessage.reset_mock()
handle_message(payload)
mock_client.chat_postMessage.assert_not_called()
Loading

0 comments on commit c17b94f

Please sign in to comment.