-
-
Notifications
You must be signed in to change notification settings - Fork 95
implemented matching model and Added slack models #913
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
Rajgupta36
wants to merge
13
commits into
OWASP:main
Choose a base branch
from
Rajgupta36:matching_model
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 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c7d7766
implemented matching leader model
Rajgupta36 8d35a27
pre-commit
Rajgupta36 4d1a91b
spell-fix
Rajgupta36 9996fa8
Merge branch 'main' into matching_model
Rajgupta36 098f52e
Merge branch 'main' into matching_model
Rajgupta36 30dd2cf
Merge branch 'main' into matching_model
Rajgupta36 15f6112
fixes
Rajgupta36 635cf27
improve speed
Rajgupta36 b1b9c0d
Merge branch 'main' into matching_model
Rajgupta36 0234c83
slack models and commands
Rajgupta36 d0dff13
Merge branch 'main' into matching_model
Rajgupta36 3b1d422
bug fixes
Rajgupta36 965bd2d
Merge branch 'main' into matching_model
Rajgupta36 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
105 changes: 105 additions & 0 deletions
105
backend/apps/common/management/commands/matching_users.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,105 @@ | ||
"""A command to perform fuzzy and exact matching of leaders with GitHub users models.""" | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import models | ||
from django.db.utils import DatabaseError | ||
from thefuzz import fuzz | ||
|
||
from apps.github.models.user import User | ||
from apps.owasp.models.chapter import Chapter | ||
from apps.owasp.models.committee import Committee | ||
from apps.owasp.models.project import Project | ||
|
||
MIN_NO_OF_WORDS = 2 | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Process raw leaders for multiple models and suggest leaders." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"model_name", | ||
type=str, | ||
help="Model name to process leaders for (e.g., Chapter, Committee, Project)", | ||
) | ||
parser.add_argument( | ||
"--threshold", type=int, default=95, help="Threshold for fuzzy matching" | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
model_name = kwargs["model_name"] | ||
threshold = kwargs["threshold"] | ||
|
||
model_map = { | ||
"chapter": Chapter, | ||
"committee": Committee, | ||
"project": Project, | ||
} | ||
|
||
model_class = model_map.get(model_name.lower()) | ||
|
||
if not model_class: | ||
self.stdout.write( | ||
self.style.ERROR("Invalid model name! Choose from: chapter, committee, project") | ||
) | ||
return | ||
|
||
all_users = User.objects.all() | ||
filtered_users = [ | ||
u | ||
for u in all_users | ||
if len(u.login) >= MIN_NO_OF_WORDS and (u.name and len(u.name) >= MIN_NO_OF_WORDS) | ||
] | ||
|
||
instances = model_class.objects.all() | ||
for instance in instances: | ||
self.stdout.write(f"Processing leaders for {model_name.capitalize()} {instance.id}...") | ||
exact_matches, fuzzy_matches, unmatched_leaders = self.process_leaders( | ||
instance.leaders_raw, threshold, filtered_users | ||
) | ||
instance.suggested_leaders.set(list(set(exact_matches + fuzzy_matches))) | ||
instance.save() | ||
|
||
if unmatched_leaders: | ||
self.stdout.write(f"Unmatched leaders for {instance.name}: {unmatched_leaders}") | ||
|
||
def process_leaders(self, leaders_raw, threshold, filtered_users): | ||
"""Process leaders and return the suggested leaders with exact and fuzzy matching.""" | ||
if not leaders_raw: | ||
return [], [], [] | ||
|
||
exact_matches = [] | ||
fuzzy_matches = [] | ||
unmatched_leaders = [] | ||
|
||
for leader in leaders_raw: | ||
try: | ||
leaders_data = User.objects.filter( | ||
models.Q(login__iexact=leader) | models.Q(name__iexact=leader) | ||
).first() | ||
if leaders_data: | ||
exact_matches.append(leaders_data) | ||
self.stdout.write(f"Exact match found for {leader}: {leaders_data}") | ||
continue | ||
|
||
matches = [ | ||
u | ||
for u in filtered_users | ||
if (fuzz.partial_ratio(leader, u.login) >= threshold) | ||
or (fuzz.partial_ratio(leader, u.name if u.name else "") >= threshold) | ||
] | ||
|
||
new_fuzzy_matches = [m for m in matches if m not in exact_matches] | ||
fuzzy_matches.extend(new_fuzzy_matches) | ||
|
||
if matches: | ||
for match in new_fuzzy_matches: | ||
self.stdout.write(f"Fuzzy match found for {leader}: {match}") | ||
else: | ||
unmatched_leaders.append(leader) | ||
|
||
except DatabaseError as e: | ||
unmatched_leaders.append(leader) | ||
self.stdout.write(self.style.ERROR(f"Error processing leader {leader}: {e}")) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return exact_matches, fuzzy_matches, unmatched_leaders |
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
73 changes: 73 additions & 0 deletions
73
backend/apps/owasp/migrations/0015_chapter_leaders_chapter_suggested_leaders_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,73 @@ | ||
# Generated by Django 5.1.6 on 2025-02-22 21:43 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("github", "0015_alter_release_author"), | ||
("owasp", "0014_project_custom_tags"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="chapter", | ||
name="leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="normal_%(class)s", | ||
to="github.user", | ||
verbose_name="Leaders", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="chapter", | ||
name="suggested_leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="exact_matched_%(class)s", | ||
to="github.user", | ||
verbose_name="Exact Match Users", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="committee", | ||
name="leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="normal_%(class)s", | ||
to="github.user", | ||
verbose_name="Leaders", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="committee", | ||
name="suggested_leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="exact_matched_%(class)s", | ||
to="github.user", | ||
verbose_name="Exact Match Users", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="project", | ||
name="leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="normal_%(class)s", | ||
to="github.user", | ||
verbose_name="Leaders", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="project", | ||
name="suggested_leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="exact_matched_%(class)s", | ||
to="github.user", | ||
verbose_name="Exact Match Users", | ||
), | ||
), | ||
] |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve user filtering logic.
The current logic doesn't match the apparent intent:
Consider this more efficient approach:
Or if you meant to filter by character length:
And query directly in the database:
📝 Committable suggestion