Skip to content

Commit

Permalink
Add GitHub username validation (mozilla#3433)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathjazz authored Nov 6, 2024
1 parent da516ae commit a7fa8f4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pontoon/base/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import re

from pathlib import Path

import bleach

from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils import timezone

from pontoon.base import utils
Expand Down Expand Up @@ -256,6 +259,25 @@ class Meta:
"bugzilla",
)

def clean_github(self):
github_username = self.cleaned_data.get("github")

# Define GitHub's username pattern
github_username_pattern = r"^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$"

# Validate using the pattern
if github_username and not re.match(
github_username_pattern, github_username, re.IGNORECASE
):
raise ValidationError(
"""
GitHub username may only contain alphanumeric characters and single hyphens,
and cannot begin or end with a hyphen. Maximum length is 39 characters.
"""
)

return github_username


class UserProfileVisibilityForm(forms.ModelForm):
"""
Expand Down

0 comments on commit a7fa8f4

Please sign in to comment.