-
-
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 11 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
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
139 changes: 139 additions & 0 deletions
139
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,139 @@ | ||
"""A command to perform fuzzy and exact matching of leaders/slack members with User model.""" | ||
|
||
from django.core.management.base import BaseCommand | ||
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 | ||
from apps.slack.models import Member | ||
|
||
MIN_NO_OF_WORDS = 2 | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Process raw leaders or Slack members and match with GitHub users." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"model_name", | ||
type=str, | ||
choices=["chapter", "committee", "project", "member"], | ||
help="Model name to process: chapter, committee, project, or member", | ||
) | ||
parser.add_argument( | ||
"--threshold", | ||
type=int, | ||
default=85, | ||
help="Threshold for fuzzy matching (0-100)", | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
model_name = kwargs["model_name"].lower() | ||
threshold = max(0, min(kwargs["threshold"], 100)) | ||
|
||
model_map = { | ||
"chapter": (Chapter, "suggested_leaders"), | ||
"committee": (Committee, "suggested_leaders"), | ||
"project": (Project, "suggested_leaders"), | ||
"member": (Member, "suggested_users"), | ||
} | ||
|
||
if model_name not in model_map: | ||
self.stdout.write( | ||
self.style.ERROR( | ||
"Invalid model name! Choose from: chapter, committee, project, member" | ||
) | ||
) | ||
return | ||
|
||
model_class, relation_field = model_map[model_name] | ||
|
||
# Pre-fetch GitHub users | ||
all_users = User.objects.values("id", "login", "name") | ||
filtered_users = { | ||
u["id"]: u for u in all_users if self._is_valid_user(u["login"], u["name"]) | ||
} | ||
|
||
instances = model_class.objects.prefetch_related(relation_field) | ||
for instance in instances: | ||
self.stdout.write(f"Processing {model_name} {instance.id}...") | ||
if model_name == "member": | ||
leaders_raw = [field for field in [instance.username, instance.real_name] if field] | ||
else: | ||
leaders_raw = instance.leaders_raw | ||
|
||
exact_matches, fuzzy_matches, unmatched = self.process_leaders( | ||
leaders_raw, threshold, filtered_users | ||
) | ||
|
||
matched_user_ids = {user["id"] for user in exact_matches + fuzzy_matches} | ||
getattr(instance, relation_field).set(matched_user_ids) | ||
|
||
if unmatched: | ||
self.stdout.write(f"Unmatched for {instance}: {unmatched}") | ||
|
||
def _is_valid_user(self, login, name): | ||
"""Check if GitHub user meets minimum requirements.""" | ||
return len(login) >= MIN_NO_OF_WORDS and name and len(name) >= MIN_NO_OF_WORDS | ||
|
||
def process_leaders(self, leaders_raw, threshold, filtered_users): | ||
"""Process leaders with optimized matching.""" | ||
if not leaders_raw: | ||
return [], [], [] | ||
|
||
exact_matches = [] | ||
fuzzy_matches = [] | ||
unmatched_leaders = [] | ||
processed_leaders = set() | ||
|
||
user_list = list(filtered_users.values()) | ||
|
||
for leader in leaders_raw: | ||
if not leader or leader in processed_leaders: | ||
continue | ||
|
||
processed_leaders.add(leader) | ||
leader_lower = leader.lower() | ||
|
||
try: | ||
exact_match = next( | ||
( | ||
u | ||
for u in user_list | ||
if u["login"].lower() == leader_lower | ||
or (u["name"].lower() == leader_lower) | ||
), | ||
None, | ||
) | ||
|
||
if exact_match: | ||
exact_matches.append(exact_match) | ||
self.stdout.write(f"Exact match found for {leader}: {exact_match['login']}") | ||
continue | ||
|
||
matches = [ | ||
u | ||
for u in user_list | ||
if (fuzz.partial_ratio(leader_lower, u["login"].lower()) >= threshold) | ||
or ( | ||
u["name"] | ||
and fuzz.partial_ratio(leader_lower, u["name"].lower()) >= threshold | ||
) | ||
] | ||
|
||
new_fuzzy_matches = [m for m in matches if m not in exact_matches] | ||
if new_fuzzy_matches: | ||
fuzzy_matches.extend(new_fuzzy_matches) | ||
for match in new_fuzzy_matches: | ||
self.stdout.write(f"Fuzzy match found for {leader}: {match['login']}") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 5.1.7 on 2025-03-20 20:01 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("owasp", "0015_chapter_leaders_chapter_suggested_leaders_and_more"), | ||
("owasp", "0030_chapter_is_leaders_policy_compliant_and_more"), | ||
] | ||
|
||
operations = [] |
73 changes: 73 additions & 0 deletions
73
backend/apps/owasp/migrations/0032_alter_chapter_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.7 on 2025-03-23 07:04 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("github", "0018_alter_issue_managers_alter_pullrequest_managers"), | ||
("owasp", "0031_merge_20250320_2001"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="chapter", | ||
name="leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="assigned_%(class)s", | ||
to="github.user", | ||
verbose_name="Assigned leaders", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="chapter", | ||
name="suggested_leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="matched_%(class)s", | ||
to="github.user", | ||
verbose_name="Matched Users", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="committee", | ||
name="leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="assigned_%(class)s", | ||
to="github.user", | ||
verbose_name="Assigned leaders", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="committee", | ||
name="suggested_leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="matched_%(class)s", | ||
to="github.user", | ||
verbose_name="Matched Users", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="project", | ||
name="leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="assigned_%(class)s", | ||
to="github.user", | ||
verbose_name="Assigned leaders", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="project", | ||
name="suggested_leaders", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="matched_%(class)s", | ||
to="github.user", | ||
verbose_name="Matched 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.
Add validation for
leaders_raw
attribute existence.The code assumes all model instances have a
leaders_raw
attribute, but there's no validation to ensure it exists. This could lead to AttributeError exceptions.📝 Committable suggestion