-
-
Notifications
You must be signed in to change notification settings - Fork 209
Fix #2019: Link OWASP entities to related Slack channels #2049
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
Open
anurag2787
wants to merge
11
commits into
OWASP:main
Choose a base branch
from
anurag2787:feature/slack-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe55a26
link OWASP entities to related Slack channels
anurag2787 b91fa14
Fixed coderabbitai review
anurag2787 af6e4fe
clean imports and remove unused code
anurag2787 7151bae
update command options and filters
anurag2787 149b18a
Merge branch 'main' into feature/slack-link
anurag2787 087531b
Merge branch 'main' into feature/slack-link
anurag2787 435b969
Merge branch 'main' into feature/slack-link
anurag2787 bc7b313
Update code
arkid15r 43b77d4
Merge branch 'main' into feature/slack-link
anurag2787 f62cbda
updated the code according to the EntityChannel model
anurag2787 61c2dfc
Merge branch 'main' into feature/slack-link
anurag2787 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Admin configuration for the EntityChannel model.""" | ||
|
||
from django.contrib import admin, messages | ||
|
||
from apps.slack.models import EntityChannel | ||
|
||
|
||
@admin.action(description="Mark selected EntityChannels as reviewed") | ||
def mark_as_reviewed(_modeladmin, request, queryset): | ||
"""Admin action to mark selected EntityChannels as reviewed.""" | ||
messages.success( | ||
request, | ||
f"Marked {queryset.update(is_reviewed=True)} EntityChannel(s) as reviewed.", | ||
) | ||
|
||
|
||
@admin.register(EntityChannel) | ||
class EntityChannelAdmin(admin.ModelAdmin): | ||
"""Admin interface for the EntityChannel model.""" | ||
|
||
actions = (mark_as_reviewed,) | ||
list_display = ( | ||
"entity", | ||
"channel", | ||
"is_default", | ||
"is_reviewed", | ||
"platform", | ||
) | ||
list_filter = ( | ||
"is_default", | ||
"is_reviewed", | ||
"platform", | ||
"entity_type", | ||
"channel_type", | ||
) | ||
search_fields = ("channel_id", "entity_id") |
42 changes: 42 additions & 0 deletions
42
backend/apps/slack/management/commands/owasp_match_channels.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,42 @@ | ||
"""A command to populate EntityChannel records from Slack data.""" | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
from django.core.management.base import BaseCommand | ||
from django.utils.text import slugify | ||
|
||
from apps.owasp.models.chapter import Chapter | ||
from apps.owasp.models.committee import Committee | ||
from apps.owasp.models.project import Project | ||
from apps.slack.models import Conversation, EntityChannel | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Populate EntityChannel links for Chapters, Committees, and Projects." | ||
|
||
def handle(self, *args, **options): | ||
created = 0 | ||
for model in (Chapter, Committee, Project): | ||
content_type = ContentType.objects.get_for_model(model) | ||
# Use .only and .iterator for memory efficiency | ||
for entity in model.objects.all().only("id", "name").iterator(): | ||
# Normalize the name for matching (e.g., "OWASP Lima" -> "owasp-lima") | ||
needle = slugify(entity.name or "") | ||
if not needle: | ||
continue | ||
qs = Conversation.objects.all() | ||
conversations = qs.filter(name__icontains=needle) | ||
for conv in conversations: | ||
_, was_created = EntityChannel.objects.get_or_create( | ||
entity_id=entity.pk, | ||
entity_type=content_type, | ||
channel_id=conv.pk, | ||
channel_type=ContentType.objects.get_for_model(Conversation), | ||
defaults={ | ||
"is_default": False, | ||
"is_reviewed": False, | ||
"platform": EntityChannel.Platform.SLACK, | ||
}, | ||
) | ||
if was_created: | ||
created += 1 | ||
self.stdout.write(self.style.SUCCESS(f"Created {created} EntityChannel records.")) |
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,70 @@ | ||
# Generated by Django 5.2.5 on 2025-08-15 22:35 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("contenttypes", "0002_remove_content_type_name"), | ||
("slack", "0018_conversation_sync_messages"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="EntityChannel", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("channel_id", models.PositiveBigIntegerField()), | ||
("entity_id", models.PositiveBigIntegerField()), | ||
( | ||
"is_default", | ||
models.BooleanField( | ||
default=False, | ||
help_text="Indicates if this is the main channel for the entity", | ||
), | ||
), | ||
( | ||
"is_reviewed", | ||
models.BooleanField( | ||
default=False, help_text="Indicates if the channel has been reviewed" | ||
), | ||
), | ||
( | ||
"platform", | ||
models.CharField( | ||
choices=[("slack", "Slack")], | ||
default="slack", | ||
help_text="Platform of the channel (e.g., Slack)", | ||
max_length=32, | ||
), | ||
), | ||
( | ||
"channel_type", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="+", | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
( | ||
"entity_type", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="+", | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "Entity channel", | ||
"verbose_name_plural": "Entity channels", | ||
"unique_together": {("channel_id", "channel_type", "entity_id", "entity_type")}, | ||
}, | ||
), | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Model for linking OWASP entities (chapter, committee, project) to communication channels.""" | ||
|
||
from django.contrib.contenttypes.fields import GenericForeignKey | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.db import models | ||
|
||
|
||
class EntityChannel(models.Model): | ||
"""Model representing a link between an entity and a channel.""" | ||
|
||
class Platform(models.TextChoices): | ||
SLACK = "slack", "Slack" | ||
|
||
class Meta: | ||
unique_together = ( | ||
"channel_id", | ||
"channel_type", | ||
"entity_id", | ||
"entity_type", | ||
) | ||
verbose_name = "Entity channel" | ||
verbose_name_plural = "Entity channels" | ||
|
||
# Channel. | ||
channel = GenericForeignKey("channel_type", "channel_id") | ||
channel_id = models.PositiveBigIntegerField() | ||
channel_type = models.ForeignKey( | ||
ContentType, | ||
on_delete=models.CASCADE, | ||
related_name="+", | ||
) | ||
|
||
# Entity. | ||
entity = GenericForeignKey("entity_type", "entity_id") | ||
entity_id = models.PositiveBigIntegerField() | ||
entity_type = models.ForeignKey( | ||
ContentType, | ||
on_delete=models.CASCADE, | ||
related_name="+", | ||
) | ||
|
||
is_default = models.BooleanField( | ||
default=False, | ||
help_text="Indicates if this is the main channel for the entity", | ||
) | ||
is_reviewed = models.BooleanField( | ||
default=False, | ||
help_text="Indicates if the channel has been reviewed", | ||
) | ||
platform = models.CharField( | ||
max_length=32, | ||
default=Platform.SLACK, | ||
choices=Platform.choices, | ||
help_text="Platform of the channel (e.g., Slack)", | ||
) | ||
|
||
def __str__(self): | ||
"""Return a readable string representation of the EntityChannel.""" | ||
return f"{self.entity} - {self.channel} ({self.platform})" |
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.