-
-
Notifications
You must be signed in to change notification settings - Fork 537
Align badge css classes and auto-sync badges during user update #3725
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
Isha-upadhyay
wants to merge
6
commits into
OWASP:main
Choose a base branch
from
Isha-upadhyay:feature/badge-sync-alignment
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
6 commits
Select commit
Hold shift + click to select a range
7519b5b
feat: align badge css classes and auto-sync badges during user update
Isha-upadhyay 9468e6b
resolve frontend backend test
Isha-upadhyay 4834829
coderabbit request resolved
Isha-upadhyay f2371a9
update migration of db change
Isha-upadhyay a16058f
badge documentation
Isha-upadhyay aa4ec62
lint-fix acc to coderabbit
Isha-upadhyay 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
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
26 changes: 26 additions & 0 deletions
26
backend/apps/nest/migrations/0009_rename_bug_slash_css_class.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,26 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| def update_bug_slash_css_class(apps, _schema_editor): | ||
| """Rename stored css_class from 'bug_slash' to 'bugSlash'.""" | ||
| Badge = apps.get_model("nest", "Badge") | ||
| Badge.objects.filter(css_class="bug_slash").update(css_class="bugSlash") | ||
|
|
||
|
|
||
| def reverse_bug_slash_css_class(apps, _schema_editor): | ||
| """Rollback css_class from 'bugSlash' to 'bug_slash'.""" | ||
| Badge = apps.get_model("nest", "Badge") | ||
| Badge.objects.filter(css_class="bugSlash").update(css_class="bug_slash") | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("nest", "0008_alter_badge_css_class"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| update_bug_slash_css_class, | ||
| reverse_bug_slash_css_class, | ||
| ), | ||
| ] | ||
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,283 @@ | ||
| # Badge System | ||
|
|
||
| This document explains how user badges work in OWASP Nest, how they are synced, | ||
| and what contributors should consider when adding or modifying badges. | ||
|
|
||
| Badges are automatically managed by the system. Contributors should not need to | ||
| manually assign or update badges in the database. | ||
|
|
||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| Badges represent user roles, achievements, or responsibilities inside the platform. | ||
|
|
||
| Examples: | ||
| - OWASP Staff | ||
| - Project Leaders | ||
| - Special contributors | ||
| - Future recognition roles | ||
|
|
||
| Badges are: | ||
| - Stored in the database | ||
| - Automatically synced using management commands | ||
| - Rendered on the frontend using icons | ||
|
|
||
| Each badge contains: | ||
|
|
||
| | Field | Purpose | | ||
| |-------|---------| | ||
| | name | Display name shown in UI | | ||
| | description | Optional explanation | | ||
| | weight | Controls ordering/priority | | ||
| | css_class | Used by frontend to choose the icon | | ||
|
|
||
| --- | ||
|
|
||
| ## Architecture | ||
|
|
||
| The badge system has two layers that **must always stay in sync**: | ||
|
|
||
| ### Backend (source of truth) | ||
| Location: `apps/nest/models/badge.py` | ||
|
|
||
| `BadgeCssClass` enum defines all valid stored values. | ||
|
|
||
| Example: | ||
|
|
||
| ```python | ||
| class BadgeCssClass(models.TextChoices): | ||
| AWARD = "award" | ||
| MEDAL = "medal" | ||
| BUG_SLASH = "bugSlash" | ||
| ``` | ||
|
|
||
| Only these values should ever be stored in the database. | ||
|
|
||
| ### Frontend (icon rendering) | ||
| Location: `frontend/utils/data.ts` | ||
|
|
||
| ```typescript | ||
| export const BADGE_CLASS_MAP = { | ||
| award: FaAward, | ||
| medal: FaMedal, | ||
| bugSlash: FaBug, | ||
| } | ||
| ``` | ||
|
|
||
| ⚠️ **IMPORTANT** | ||
|
|
||
| Backend `css_class` values must exactly match frontend keys. | ||
|
|
||
| If they differ: | ||
| - Icon not rendered | ||
| - Fallback icon used | ||
| - Tests may fail | ||
|
|
||
| Example: | ||
| - ✅ `bugSlash` → works | ||
| - ❌ `bug_slash` → icon missing | ||
|
|
||
| --- | ||
|
|
||
| ## Badge Sync Flow | ||
|
|
||
| Badges are refreshed automatically during GitHub user updates. | ||
|
|
||
| Main command: | ||
| ```bash | ||
| python manage.py github_update_users | ||
| ``` | ||
|
|
||
| Flow: | ||
| 1. Fetch users | ||
| 2. Calculate GitHub contributions | ||
| 3. Bulk save users | ||
| 4. Sync badges | ||
|
|
||
| After updating users, the command internally calls: | ||
| - `nest_update_staff_badges` | ||
| - `nest_update_project_leader_badges` | ||
|
|
||
| These commands: | ||
| - Create badges if missing | ||
| - Assign badges to eligible users | ||
| - Remove outdated badges | ||
| - Keep roles consistent | ||
|
|
||
| So badge sync is **automatic** — no manual steps required. | ||
|
|
||
| ### Where sync happens in code | ||
| Location: `apps/github/management/commands/github_update_users.py` | ||
|
|
||
| Inside `handle()`: | ||
|
|
||
| ```python | ||
| call_command("nest_update_staff_badges") | ||
| call_command("nest_update_project_leader_badges") | ||
| ``` | ||
|
|
||
| This guarantees badges stay updated whenever users are refreshed. | ||
|
|
||
| --- | ||
|
|
||
| ## Manual Sync (for debugging) | ||
|
|
||
| You can run badge updates manually: | ||
|
|
||
| ```bash | ||
| python manage.py nest_update_staff_badges | ||
| python manage.py nest_update_project_leader_badges | ||
| ``` | ||
|
|
||
| Useful when: | ||
| - Testing locally | ||
| - Verifying new logic | ||
| - Debugging assignments | ||
|
|
||
| --- | ||
|
|
||
| ## When are badges updated? | ||
|
|
||
| Badges update when: | ||
| - `github_update_users` runs | ||
| - Sync commands are run manually | ||
| - New badge logic is added | ||
|
|
||
| --- | ||
|
|
||
| ## Adding a new badge | ||
|
|
||
| Steps: | ||
| 1. Add enum value in `BadgeCssClass` | ||
| 2. Add icon mapping in frontend `BADGE_CLASS_MAP` | ||
| 3. Add assignment logic in management command | ||
| 4. Add tests | ||
| 5. Run checks | ||
|
|
||
| Example: | ||
|
|
||
| **Backend:** | ||
| ```python | ||
| NEW_BADGE = "newBadge" | ||
| ``` | ||
|
|
||
| **Frontend:** | ||
| ```typescript | ||
| newBadge: FaStar | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Changing an existing css_class value | ||
|
|
||
| If you rename a `css_class`, you **MUST** create a data migration. | ||
|
|
||
| Reason: Existing rows already store the old value. | ||
|
|
||
| Example: `bug_slash` → `bugSlash` | ||
|
|
||
| Migration: | ||
| ```python | ||
| Badge.objects.filter(css_class='bug_slash').update(css_class='bugSlash') | ||
| ``` | ||
|
|
||
| Without migration: | ||
| - Icons break | ||
| - Validation errors | ||
| - Inconsistent data | ||
|
|
||
| --- | ||
|
|
||
| ## How to verify locally | ||
|
|
||
| Run: | ||
| ```bash | ||
| python manage.py github_update_users | ||
| ``` | ||
|
|
||
| Expected output: | ||
| ```text | ||
| Syncing badges... | ||
| Syncing OWASP Staff... | ||
| Syncing Project Leaders... | ||
| ``` | ||
|
|
||
| Then: | ||
| - Check admin panel | ||
| - Check UI | ||
| - Confirm badges appear | ||
|
|
||
| --- | ||
|
|
||
| ## Testing | ||
|
|
||
| **Backend:** | ||
| ```bash | ||
| make check-test | ||
| ``` | ||
|
|
||
| **Frontend:** | ||
| ```bash | ||
| pnpm test | ||
| ``` | ||
|
|
||
| Verify: | ||
| - Badges created | ||
| - Icons render correctly | ||
| - Sync commands execute successfully | ||
|
|
||
| --- | ||
|
|
||
| ## Common Issues | ||
|
|
||
| ### Icon not showing | ||
| → css_class mismatch with frontend map | ||
|
|
||
| ### Badge not assigned | ||
| → Sync command not executed | ||
|
|
||
| ### CI failing | ||
| → Missing migration or tests | ||
|
|
||
| ### Many files modified | ||
| → Run pre-commit | ||
|
|
||
| --- | ||
|
|
||
| ## Best Practices | ||
|
|
||
| Always: | ||
| - Keep backend + frontend keys identical | ||
| - Create migration when renaming values | ||
| - Add tests | ||
| - Run `make check-test` | ||
|
|
||
| Never: | ||
| - Manually edit DB values | ||
| - Rename enums without migration | ||
| - Hardcode icons | ||
|
|
||
| --- | ||
|
|
||
| ## Mental Model | ||
|
|
||
| Think of badges as: | ||
|
|
||
| **Database → Sync Commands → Frontend Icons** | ||
|
|
||
| If you update one layer, update the others. | ||
|
|
||
| --- | ||
|
|
||
| ## Summary | ||
|
|
||
| Badges in OWASP Nest are: | ||
| - Automatic | ||
| - Synced | ||
| - Role-based | ||
| - Icon-driven | ||
|
|
||
| Running `github_update_users` keeps everything consistent. | ||
|
|
||
| If something breaks, check: **model → sync → frontend mapping**. |
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.