-
-
Notifications
You must be signed in to change notification settings - Fork 656
Added Slack conversation list sync #1032
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
4c68447
fixed
Yashgupta9330 5aebd16
Merge branch 'main' into sync
Yashgupta9330 1abba39
Merge branch 'OWASP:main' into sync
Yashgupta9330 cc0e09e
fixed linting error
Yashgupta9330 d1b1da7
Merge branch 'main' into sync
Yashgupta9330 1d78412
Merge branch 'main' into sync
Yashgupta9330 7cf6528
did some minor changes
Yashgupta9330 67e4ad0
Merge branch 'main' into sync
Yashgupta9330 7fb112f
pulled
Yashgupta9330 342b359
pulled
Yashgupta9330 28ab7dc
Merge branch 'main' into sync
Yashgupta9330 92a1ca4
deleted some migrations
Yashgupta9330 f9bbf17
pulled
Yashgupta9330 c27f386
Merge branch 'main' into sync
arkid15r 3782aaf
Merge branch 'main' into sync
Yashgupta9330 c649a50
Refactor Slack conversation model and add sync command for Slack chan…
Yashgupta9330 4097bbd
Merge branch 'main' into sync
Yashgupta9330 a93b748
Merge branch 'main' into sync
Yashgupta9330 1424246
Merge branch 'main' into sync
Yashgupta9330 88963a9
did minor changes
Yashgupta9330 56d840b
Merge branch 'main' into sync
Yashgupta9330 aeb6f7d
Merge branch 'main' into sync
Yashgupta9330 55c04e8
Merge branch 'main' into sync
Yashgupta9330 cda007b
Merge branch 'main' into sync
Yashgupta9330 5dab8eb
Merge branch 'main' into sync
Yashgupta9330 3b96cfa
Merge branch 'main' into sync
Yashgupta9330 90ae352
Merge branch 'OWASP:main' into sync
Yashgupta9330 c5ee59d
Merge branch 'main' into sync
Yashgupta9330 8176ba7
Merge branch 'main' into pr/Yashgupta9330/1032
arkid15r bc0857d
Update code
arkid15r 0d65b9e
Merge branch 'main' into sync
Yashgupta9330 6bd79a0
Merge branch 'main' into sync
Yashgupta9330 62cbe49
added test
Yashgupta9330 421aa7f
Merge branch 'main' into sync
Yashgupta9330 292a32f
Merge branch 'main' into sync
Yashgupta9330 b9bcfe2
Merge branch 'main' into sync
Yashgupta9330 016ecfe
Merge branch 'main' into sync
Yashgupta9330 be5576f
Merge branch 'main' into sync
Yashgupta9330 5079694
Merge branch 'main' into sync
Yashgupta9330 c0112e9
Merge branch 'main' into sync
Yashgupta9330 c347926
Merge branch 'main' into sync
Yashgupta9330 703acc3
added
Yashgupta9330 c7eae97
Merge branch 'main' into sync
Yashgupta9330 c2936c8
Merge branch 'main' into sync
Yashgupta9330 77e96cc
Merge branch 'main' into sync
Yashgupta9330 be4fe5d
Merge branch 'main' into pr/Yashgupta9330/1032
arkid15r 97c1316
Update code
arkid15r a2765e1
Merge channels/conversations
arkid15r c4f1329
Merge branch 'main' into pr/Yashgupta9330/1032
arkid15r 92dc7e8
Update code
arkid15r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
backend/apps/owasp/management/commands/slack_sync_conversation_list.py
|
Yashgupta9330 marked this conversation as resolved.
Outdated
|
This file contains hidden or 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,137 @@ | ||
| """Command to sync Slack channels and groups from the OWASP Slack workspace.""" | ||
|
|
||
| import logging | ||
| import time | ||
| from datetime import datetime, timezone | ||
|
|
||
| import requests | ||
| from django.conf import settings | ||
| from django.core.management.base import BaseCommand | ||
| from django.db import transaction | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class SlackSyncError(Exception): | ||
| """Custom exception for Slack sync errors.""" | ||
|
|
||
| MISSING_TOKEN_ERROR = "SLACK BOT TOKEN not found in settings" # noqa: S105 | ||
| API_ERROR_FORMAT = "Slack API error: {}" | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Sync Slack channels and groups from the OWASP Slack workspace." | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--batch-size", | ||
| type=int, | ||
| default=200, | ||
| help="Number of conversations to retrieve per request", | ||
| ) | ||
| parser.add_argument( | ||
| "--delay", type=float, default=0.5, help="Delay between API requests in seconds" | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| from apps.owasp.models.conversation import Conversation | ||
|
|
||
| try: | ||
| batch_size = options["batch_size"] | ||
| delay = options["delay"] | ||
|
|
||
| count = self.sync_slack_conversations(Conversation, batch_size=batch_size, delay=delay) | ||
|
|
||
| self.stdout.write( | ||
| self.style.SUCCESS(f"Successfully synced {count} Slack conversations") | ||
| ) | ||
| except Exception as e: | ||
| error_msg = f"Failed to sync Slack conversations: {e}" | ||
| logger.exception(error_msg) | ||
| self.stdout.write(self.style.ERROR(error_msg)) | ||
| raise | ||
|
|
||
| def sync_slack_conversations(self, conversation_model, batch_size=200, delay=0.5): | ||
| slack_token = getattr(settings, "SLACK_BOT_TOKEN", None) | ||
| if not slack_token: | ||
| raise SlackSyncError(SlackSyncError.MISSING_TOKEN_ERROR) | ||
|
|
||
| url = "https://slack.com/api/conversations.list" | ||
|
Yashgupta9330 marked this conversation as resolved.
Outdated
|
||
| params = { | ||
| "limit": batch_size, | ||
| "exclude_archived": False, | ||
| "types": "public_channel,private_channel", | ||
| } | ||
| headers = {"Authorization": f"Bearer {slack_token}"} | ||
|
|
||
| next_cursor = None | ||
| total_processed = 0 | ||
|
|
||
| while True: | ||
| if next_cursor: | ||
| params["cursor"] = next_cursor | ||
|
|
||
| response = requests.get(url, headers=headers, params=params, timeout=10) | ||
| data = response.json() | ||
|
|
||
| if not data.get("ok"): | ||
| error_msg = f"Slack API error: {data.get('error', 'Unknown error')}" | ||
| logger.error(error_msg) | ||
| raise SlackSyncError(error_msg) | ||
|
|
||
| conversations = data.get("channels", []) | ||
| self.process_conversations(conversations, conversation_model) | ||
| total_processed += len(conversations) | ||
|
|
||
| self.stdout.write(f"Processed {len(conversations)} conversations...") | ||
|
|
||
| next_cursor = data.get("response_metadata", {}).get("next_cursor") | ||
| if not next_cursor: | ||
| break | ||
|
|
||
| # Add a small delay to avoid rate limiting | ||
| time.sleep(delay) | ||
|
|
||
| return total_processed | ||
|
|
||
| @transaction.atomic | ||
| def process_conversations(self, conversations, conversation_model): | ||
| conversation_objects = [] | ||
|
|
||
| for conversation in conversations: | ||
| try: | ||
| # Convert Unix timestamp to datetime | ||
| created_timestamp = int(conversation.get("created", 0)) | ||
| created_datetime = datetime.fromtimestamp(created_timestamp, tz=timezone.utc) | ||
|
|
||
| defaults = { | ||
| "name": conversation.get("name", ""), | ||
| "created_at": created_datetime, | ||
| "is_private": conversation.get("is_private", False), | ||
| "is_archived": conversation.get("is_archived", False), | ||
| "is_general": conversation.get("is_general", False), | ||
| "topic": conversation.get("topic", {}).get("value", ""), | ||
| "purpose": conversation.get("purpose", {}).get("value", ""), | ||
| "creator_id": conversation.get("creator", ""), | ||
| } | ||
|
|
||
| obj, created = conversation_model.objects.update_or_create( | ||
| entity_id=conversation["id"], defaults=defaults | ||
| ) | ||
| conversation_objects.append(obj) | ||
|
|
||
| if created: | ||
| logger.info("Created new conversation: %s", obj.name) | ||
| else: | ||
| logger.info("Updated conversation: %s", obj.name) | ||
|
|
||
| except KeyError: | ||
| logger.exception("Missing required field in conversation data") | ||
| continue | ||
| except Exception: | ||
| logger.exception( | ||
| "Error processing conversation %s", conversation.get("id", "unknown") | ||
| ) | ||
| continue | ||
|
|
||
| return conversation_objects | ||
This file contains hidden or 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,37 @@ | ||
| # Generated by Django 5.1.7 on 2025-03-07 12:22 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0022_sponsor"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="Conversation", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("entity_id", models.CharField(max_length=255, unique=True)), | ||
| ("name", models.CharField(max_length=255)), | ||
| ("created_at", models.DateTimeField()), | ||
| ("is_private", models.BooleanField(default=False)), | ||
| ("is_archived", models.BooleanField(default=False)), | ||
| ("is_general", models.BooleanField(default=False)), | ||
| ("topic", models.TextField(blank=True, default="")), | ||
| ("purpose", models.TextField(blank=True, default="")), | ||
| ("creator_id", models.CharField(max_length=255)), | ||
| ("updated_at", models.CharField(max_length=255)), | ||
| ], | ||
| options={ | ||
| "verbose_name_plural": "Conversations", | ||
| "db_table": "slack_conversations", | ||
| }, | ||
| ), | ||
| ] |
37 changes: 37 additions & 0 deletions
37
backend/apps/owasp/migrations/0024_remove_conversation_updated_at_and_more.py
This file contains hidden or 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,37 @@ | ||
| # Generated by Django 5.1.7 on 2025-03-07 13:25 | ||
|
|
||
| import django.utils.timezone | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0023_conversation"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RemoveField( | ||
| model_name="conversation", | ||
| name="updated_at", | ||
| ), | ||
| migrations.AddField( | ||
| model_name="conversation", | ||
| name="nest_created_at", | ||
| field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), | ||
| preserve_default=False, | ||
| ), | ||
| migrations.AddField( | ||
| model_name="conversation", | ||
| name="nest_updated_at", | ||
| field=models.DateTimeField(auto_now=True), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="conversation", | ||
| name="created_at", | ||
| field=models.DateTimeField(blank=True, null=True), | ||
| ), | ||
| migrations.AlterModelTable( | ||
| name="conversation", | ||
| table="owasp_slack_conversations", | ||
| ), | ||
| ] |
|
Yashgupta9330 marked this conversation as resolved.
Outdated
|
This file contains hidden or 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,27 @@ | ||
| """OWASP app conversation models.""" | ||
|
|
||
| from django.db import models | ||
|
|
||
| from apps.common.models import TimestampedModel | ||
|
|
||
|
|
||
| class Conversation(TimestampedModel): | ||
| """Model representing a Slack conversation (channel or group).""" | ||
|
|
||
| class Meta: | ||
| db_table = "owasp_slack_conversations" | ||
|
Yashgupta9330 marked this conversation as resolved.
Outdated
|
||
| verbose_name_plural = "Conversations" | ||
|
|
||
| entity_id = models.CharField(max_length=255, unique=True) | ||
| name = models.CharField(max_length=255) | ||
| created_at = models.DateTimeField(blank=True, null=True) | ||
| is_private = models.BooleanField(default=False) | ||
| is_archived = models.BooleanField(default=False) | ||
| is_general = models.BooleanField(default=False) | ||
| topic = models.TextField(blank=True, default="") | ||
| purpose = models.TextField(blank=True, default="") | ||
| creator_id = models.CharField(max_length=255) | ||
|
|
||
| def __str__(self): | ||
| """Return a string representation of the conversation.""" | ||
| return self.name | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.