Skip to content
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

Fix: bug in github username for reviewers) #153

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![PyPI](https://img.shields.io/pypi/v/pyosmeta.svg)](https://pypi.org/project/pyosmeta/)

## [Unreleased]
- Fix: bug where if a github username has a name after it returns a invalid cleaned github username (@lwasser)

## [v0.2.4] - 2024-03-29

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This repo contains a small module and several CLI scripts, including:

_Since pyOpenSci uses this tool for its website, we expect this package to have infrequent releases._


## Installation

Using pip:
Expand Down
11 changes: 4 additions & 7 deletions src/pyosmeta/cli/update_review_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
from pyosmeta.file_io import clean_export_yml, load_pickle
from pyosmeta.github_api import GitHubAPI
from pyosmeta.models import PersonModel


def get_clean_user(username: str) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this to the utils module as it makes more sense there.

"""A small helper that removes whitespace and ensures username is
lower case"""
return username.lower().strip()
from pyosmeta.utils_clean import get_clean_user


def main():
Expand All @@ -61,7 +56,9 @@ def main():

if gh_user not in contribs.keys():
print("Found a new contributor!", gh_user)
new_contrib = process_contribs.get_user_info(gh_user)
new_contrib = process_contribs.return_user_info(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the cause of the actual first bug in CI

gh_user
)
new_contrib["date_added"] = datetime.now().strftime(
"%Y-%m-%d"
)
Expand Down
33 changes: 33 additions & 0 deletions src/pyosmeta/utils_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@
from typing import Any


def get_clean_user(username: str) -> str:
"""Cleans a GitHub username provided in a review issue by removing any
additional text after a space and converting to lowercase.

This function assumes that a valid username should not contain spaces. If a
space is detected, only the part before the first space is considered the
username. The resulting string is then trimmed of whitespace and converted
to lowercase.

Parameters
----------
username : str
The input username string which may contain extra text or spaces.

Returns
-------
str
The cleaned username in lowercase without any extra text or spaces.

Examples
--------
>>> get_clean_user("@githubusername")
'githubusername'

>>> get_clean_user("@githubusername name after text")
'githubusername'
"""

if len(username.split()) > 1:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the above bug was fixed another bug popped up related to a github username entered with a name after (and only a space).

username = username.split()[0]
return username.lower().strip()


def clean_date(source_date: str | None) -> datetime | str:
"""Cleans up a date string to a consistent datetime format.

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_get_token(mock_github_token):


def test_missing_token(mock_missing_github_token, tmpdir):
"""Test that a keyerror is raised when the token is missing."""
"""Test that a keyerror is raised when the token is missing.
If you have a token in your temporary environment, this will
fail and not return a keyerror."""
os.chdir(tmpdir)
github_api = GitHubAPI()

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_update_review_teams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def get_clean_user(username: str) -> str:
"""A small helper that removes whitespace and ensures username is
lower case"""
# If the github username has spaces there is an error which might be
# that someone added a name after the username. Only return the github
# username
if len(username.split()) > 1:
username = username.split()[0]
return username.lower().strip()
17 changes: 17 additions & 0 deletions tests/unit/test_utils_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
clean_date_accepted_key,
clean_markdown,
clean_name,
get_clean_user,
)


Expand Down Expand Up @@ -84,3 +85,19 @@ def test_clean_name(input_name, expected_output):
)
def test_clean_date_accepted_key(input_dict, expected_output):
assert clean_date_accepted_key(input_dict) == expected_output


@pytest.mark.parametrize(
"input_username, expected_output",
[
("githubusername", "githubusername"),
("githubusername name after text", "githubusername"),
("username (full name here)", "username"),
("githubusername", "githubusername"),
("githubusername extra text", "githubusername"),
("username (just the username)", "username"),
],
)
def test_get_clean_user(input_username, expected_output):
print(input_username)
assert get_clean_user(input_username) == expected_output