-
-
Notifications
You must be signed in to change notification settings - Fork 663
Add ActivityEvent model and builder #5233
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
12
commits into
OWASP:feature/owasp-pulse
Choose a base branch
from
anurag2787:pulse-activityevent-model
base: feature/owasp-pulse
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 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ecd8bb6
Implemented data model
anurag2787 6c15d09
adress review
anurag2787 3c115ce
Merge branch 'feature/owasp-pulse' into pulse-activityevent-model
anurag2787 154b03c
added actitivty builder
anurag2787 0ef2ff5
Merge branch 'pulse-activityevent-model' of github.com:anurag2787/Nes…
anurag2787 ee30f90
Merge branch 'feature/owasp-pulse' into pulse-activityevent-model
anurag2787 3758d68
Address review
anurag2787 5244db5
updated order
anurag2787 5264756
Fixed Published Issue
anurag2787 80e1c5f
adress review
anurag2787 19a7374
update
anurag2787 023e9b4
fixed review
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
Some comments aren't visible on the classic Files Changed page.
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
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,32 @@ | ||
| """OWASP app ActivityEvent model admin.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.activity_event import ActivityEvent | ||
|
|
||
|
|
||
| class ActivityEventAdmin(admin.ModelAdmin): | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| """Admin for ActivityEvent model.""" | ||
|
|
||
| autocomplete_fields = ( | ||
| "github_user", | ||
| "github_repository", | ||
| ) | ||
| list_display = ( | ||
| "activity_type", | ||
| "github_repository", | ||
| "github_user", | ||
| "occurred_at", | ||
| ) | ||
| list_filter = ( | ||
| "activity_type", | ||
| "occurred_at", | ||
| ) | ||
| search_fields = ( | ||
| "activity_type", | ||
| "github_repository__name", | ||
| "github_user__login", | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(ActivityEvent, ActivityEventAdmin) | ||
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,98 @@ | ||
| # Generated by Django 6.0.7 on 2026-07-28 07:54 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("contenttypes", "0002_remove_content_type_name"), | ||
| ("github", "0044_user_indexes"), | ||
| ("owasp", "0072_project_project_name_gin_idx_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="ActivityEvent", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "activity_type", | ||
| models.CharField( | ||
| choices=[ | ||
| ("issue_closed", "Issue Closed"), | ||
| ("issue_opened", "Issue Opened"), | ||
| ("pr_closed", "PR Closed"), | ||
| ("pr_merged", "PR Merged"), | ||
| ("pr_opened", "PR Opened"), | ||
| ("release_published", "Release Published"), | ||
| ], | ||
| max_length=32, | ||
| verbose_name="Activity Type", | ||
| ), | ||
| ), | ||
| ("object_id", models.PositiveBigIntegerField()), | ||
| ( | ||
| "occurred_at", | ||
| models.DateTimeField( | ||
| help_text="Timestamp when the activity event occurred on GitHub", | ||
| verbose_name="Occurred at", | ||
| ), | ||
| ), | ||
| ( | ||
| "content_type", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, to="contenttypes.contenttype" | ||
| ), | ||
| ), | ||
| ( | ||
| "github_repository", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="activity_events", | ||
| to="github.repository", | ||
| verbose_name="GitHub Repository", | ||
| ), | ||
| ), | ||
| ( | ||
| "github_user", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="activity_events", | ||
| to="github.user", | ||
| verbose_name="GitHub User", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name_plural": "Activity Events", | ||
| "db_table": "github_activity_events", | ||
| "indexes": [ | ||
| models.Index(fields=["activity_type"], name="activity_event_type_idx"), | ||
| models.Index(fields=["github_user"], name="activity_event_github_user_idx"), | ||
| models.Index( | ||
| fields=["content_type", "object_id"], name="activity_event_source_idx" | ||
| ), | ||
| models.Index(fields=["occurred_at"], name="activity_event_occurred_at_idx"), | ||
| models.Index( | ||
| fields=["github_repository"], name="activity_event_github_repo_idx" | ||
| ), | ||
| ], | ||
| "constraints": [ | ||
| models.UniqueConstraint( | ||
| fields=("activity_type", "content_type", "object_id", "occurred_at"), | ||
| name="unique_activity_event", | ||
| ) | ||
| ], | ||
| }, | ||
| ), | ||
| ] | ||
|
coderabbitai[bot] marked this conversation as resolved.
anurag2787 marked this conversation as resolved.
|
||
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,154 @@ | ||
| """OWASP app activity event model.""" | ||
|
|
||
| import logging | ||
|
|
||
| from django.contrib.contenttypes.fields import GenericForeignKey | ||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.db import models | ||
|
|
||
| from apps.common.models import BulkSaveModel, TimestampedModel | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ActivityEvent(BulkSaveModel, TimestampedModel): | ||
| """Represents a discrete GitHub activity event linked to a single source object. | ||
|
|
||
| Uses a polymorphic GenericForeignKey to reference the source object. | ||
| """ | ||
|
|
||
| class Meta: | ||
| """Model options.""" | ||
|
|
||
| db_table = "github_activity_events" | ||
| verbose_name_plural = "Activity Events" | ||
|
|
||
| constraints = [ | ||
| models.UniqueConstraint( | ||
| fields=[ | ||
| "activity_type", | ||
| "content_type", | ||
| "object_id", | ||
| "occurred_at", | ||
| ], | ||
| name="unique_activity_event", | ||
| ), | ||
| ] | ||
|
|
||
| indexes = [ | ||
| models.Index(fields=["activity_type"], name="activity_event_type_idx"), | ||
| models.Index(fields=["github_user"], name="activity_event_github_user_idx"), | ||
| models.Index( | ||
| fields=["content_type", "object_id"], | ||
| name="activity_event_source_idx", | ||
| ), | ||
| models.Index(fields=["occurred_at"], name="activity_event_occurred_at_idx"), | ||
| models.Index(fields=["github_repository"], name="activity_event_github_repo_idx"), | ||
| ] | ||
|
|
||
| class ActivityType(models.TextChoices): | ||
| """Activity type choices.""" | ||
|
|
||
| ISSUE_CLOSED = "issue_closed", "Issue Closed" | ||
| ISSUE_OPENED = "issue_opened", "Issue Opened" | ||
| PR_CLOSED = "pr_closed", "PR Closed" | ||
| PR_MERGED = "pr_merged", "PR Merged" | ||
| PR_OPENED = "pr_opened", "PR Opened" | ||
| RELEASE_PUBLISHED = "release_published", "Release Published" | ||
|
|
||
| activity_type = models.CharField( | ||
| verbose_name="Activity Type", | ||
| max_length=32, | ||
| choices=ActivityType.choices, | ||
| ) | ||
| github_user = models.ForeignKey( | ||
| "github.User", | ||
| verbose_name="GitHub User", | ||
| on_delete=models.SET_NULL, | ||
| blank=True, | ||
| null=True, | ||
| related_name="activity_events", | ||
| ) | ||
| content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) | ||
| object_id = models.PositiveBigIntegerField() | ||
| occurred_at = models.DateTimeField( | ||
| verbose_name="Occurred at", | ||
| help_text="Timestamp when the activity event occurred on GitHub", | ||
| ) | ||
| github_repository = models.ForeignKey( | ||
| "github.Repository", | ||
| verbose_name="GitHub Repository", | ||
| on_delete=models.CASCADE, | ||
| related_name="activity_events", | ||
| ) | ||
| source_object = GenericForeignKey("content_type", "object_id") | ||
|
|
||
| HANDLERS: dict[str, str] = { | ||
| "Issue": "build_for_issue", | ||
| "PullRequest": "build_for_pull_request", | ||
| "Release": "build_for_release", | ||
| } | ||
|
|
||
| def __str__(self) -> str: | ||
| """Return human-readable representation.""" | ||
| return f"{self.activity_type} by {self.github_user} in {self.github_repository}" | ||
|
|
||
| @staticmethod | ||
| def bulk_save(activity_events, fields=None) -> None: # type: ignore[override] | ||
| """Bulk save activity events.""" | ||
| BulkSaveModel.bulk_save(ActivityEvent, activity_events, fields=fields) | ||
|
|
||
| @staticmethod | ||
| def build_for_issue(issue) -> list[tuple]: | ||
| """Return event tuples for an Issue.""" | ||
| events = [(ActivityEvent.ActivityType.ISSUE_OPENED, issue.created_at, issue.author)] | ||
| if issue.state == "closed" and issue.closed_at: | ||
| events.append((ActivityEvent.ActivityType.ISSUE_CLOSED, issue.closed_at, issue.author)) | ||
| return events | ||
|
|
||
| @staticmethod | ||
| def build_for_pull_request(pr) -> list[tuple]: | ||
| """Return event tuples for a PullRequest.""" | ||
| events = [(ActivityEvent.ActivityType.PR_OPENED, pr.created_at, pr.author)] | ||
| if pr.merged_at: | ||
| events.append((ActivityEvent.ActivityType.PR_MERGED, pr.merged_at, pr.author)) | ||
| elif pr.state == "closed" and pr.closed_at: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't follow the approach consistently -- see |
||
| events.append((ActivityEvent.ActivityType.PR_CLOSED, pr.closed_at, pr.author)) | ||
| return events | ||
|
|
||
| @staticmethod | ||
| def build_for_release(release) -> list[tuple]: | ||
| """Return event tuples for a Release.""" | ||
| occurred_at = release.published_at or release.created_at | ||
| return [(ActivityEvent.ActivityType.RELEASE_PUBLISHED, occurred_at, release.author)] | ||
|
anurag2787 marked this conversation as resolved.
Outdated
anurag2787 marked this conversation as resolved.
Outdated
|
||
|
|
||
| @staticmethod | ||
| def update_data(obj) -> None: | ||
| """Create ActivityEvent row(s) for a saved GitHub model instance if they do not exist.""" | ||
| handler_name = ActivityEvent.HANDLERS.get(type(obj).__name__) | ||
| if handler_name is None: | ||
| logger.error( | ||
| "ActivityEvent.update_data received unsupported model type: %s", | ||
| type(obj).__name__, | ||
| ) | ||
| message = f"Unsupported model type: {type(obj)}" | ||
| raise TypeError(message) | ||
|
|
||
| handler = getattr(ActivityEvent, handler_name) | ||
| events = handler(obj) | ||
| content_type = ContentType.objects.get_for_model(obj) | ||
|
|
||
| for activity_type, occurred_at, github_user in events: | ||
| if occurred_at is None: | ||
| continue | ||
|
|
||
| ActivityEvent.objects.get_or_create( | ||
| activity_type=activity_type, | ||
| content_type=content_type, | ||
| object_id=obj.pk, | ||
| occurred_at=occurred_at, | ||
| defaults={ | ||
| "github_user": github_user, | ||
| "github_repository": obj.repository, | ||
| }, | ||
| ) | ||
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,13 @@ | ||
| volumes: | ||
| backend-venv: | ||
| name: backend-venv-pulse | ||
| cache-data: | ||
| name: cache-data-pulse | ||
| db-data: | ||
| name: db-data-pulse | ||
| docs-venv: | ||
| name: docs-venv-pulse | ||
| frontend-next: | ||
| name: frontend-next-pulse | ||
| frontend-node-modules: | ||
| name: frontend-node-modules-pulse | ||
|
anurag2787 marked this conversation as resolved.
|
||
Oops, something went wrong.
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.