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: handle if ORGANIZATION not provided #32

Merged
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ lint:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=100 --max-line-length=150 --statistics --exclude=venv,.venv,.git,__pycache__
pylint --rcfile=.pylintrc --fail-under=9.0 *.py
black .
zkoppert marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 32 additions & 6 deletions cleanowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
import github3


def get_org(github_connection, organization):
"""Get the organization object"""
try:
return github_connection.organization(organization)
except github3.exceptions.NotFoundError:
print(f"Organization {organization} not found")
return None


def main(): # pragma: no cover
"""Run the main program"""

Expand Down Expand Up @@ -36,6 +45,17 @@ def main(): # pragma: no cover
codeowners_count = 0
users_count = 0

if organization and not repository_list:
gh_org = get_org(github_connection, organization)
if not gh_org:
raise ValueError(
f"""Organization {organization} is not an organization and
REPOSITORY environment variable was not set.
Please set valid ORGANIZATION or set REPOSITORY environment
variable
"""
)

# Get the repositories from the organization or list of repositories
repos = get_repos_iterator(organization, repository_list, github_connection)

Expand Down Expand Up @@ -90,10 +110,16 @@ def main(): # pragma: no cover
usernames = get_usernames_from_codeowners(codeowners_file_contents)

for username in usernames:
org = organization if organization else repo.owner.login
gh_org = get_org(github_connection, org)
if not gh_org:
print(f"Owner {org} of repo {repo} is not an organization.")
break

# Check to see if the username is a member of the organization
if not github_connection.organization(organization).is_member(username):
if not gh_org.is_member(username):
print(
f"\t{username} is not a member of {organization}. Suggest removing them from {repo.full_name}"
f"\t{username} is not a member of {org}. Suggest removing them from {repo.full_name}"
)
users_count += 1
if not dry_run:
Expand Down Expand Up @@ -148,10 +174,10 @@ def get_repos_iterator(organization, repository_list, github_connection):
repos = github_connection.organization(organization).repositories()
else:
# Get the repositories from the repository_list
for repo in repository_list:
repos.append(
github_connection.repository(repo.split("/")[0], repo.split("/")[1])
)
for full_repo_path in repository_list:
org = full_repo_path.split("/")[0]
repo = full_repo_path.split("/")[1]
repos.append(github_connection.repository(org, repo))
zkoppert marked this conversation as resolved.
Show resolved Hide resolved

return repos

Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
black==24.2.0
flake8==7.0.0
pylint==3.1.0
pytest==8.1.1
Expand Down
1 change: 1 addition & 0 deletions test_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test cases for the auth module."""

import unittest
from unittest.mock import MagicMock, patch

Expand Down
38 changes: 36 additions & 2 deletions test_cleanowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import uuid
from unittest.mock import MagicMock, patch

import github3
from cleanowners import (
commit_changes,
get_org,
get_repos_iterator,
get_usernames_from_codeowners,
)
Expand Down Expand Up @@ -81,6 +83,38 @@ def test_get_usernames_from_codeowners(self):
self.assertEqual(result, expected_usernames)


class TestGetOrganization(unittest.TestCase):
"""Test the get_org function in cleanowners.py"""

@patch("github3.login")
def test_get_organization_succeeds(self, mock_github):
"""Test the organization is valid."""
organization = "my_organization"
github_connection = mock_github.return_value

mock_organization = MagicMock()
github_connection.organization.return_value = mock_organization

result = get_org(github_connection, organization)

github_connection.organization.assert_called_once_with(organization)
self.assertEqual(result, mock_organization)

@patch("github3.login")
def test_get_organization_fails(self, mock_github):
"""Test the organization is not valid."""
organization = "my_organization"
github_connection = mock_github.return_value

github_connection.organization.side_effect = github3.exceptions.NotFoundError(
resp=MagicMock(status_code=404)
)
result = get_org(github_connection, organization)

github_connection.organization.assert_called_once_with(organization)
self.assertIsNone(result)


class TestGetReposIterator(unittest.TestCase):
"""Test the get_repos_iterator function in evergreen.py"""

Expand Down Expand Up @@ -111,7 +145,7 @@ def test_get_repos_iterator_with_organization(self, mock_github):
def test_get_repos_iterator_with_repository_list(self, mock_github):
"""Test the get_repos_iterator function with a repository list"""
organization = None
repository_list = ["org/repo1", "org/repo2"]
repository_list = ["org/repo1", "org2/repo2"]
github_connection = mock_github.return_value

mock_repository = MagicMock()
Expand All @@ -123,7 +157,7 @@ def test_get_repos_iterator_with_repository_list(self, mock_github):
# Assert that the repository method was called with the correct arguments for each repository in the list
expected_calls = [
unittest.mock.call("org", "repo1"),
unittest.mock.call("org", "repo2"),
unittest.mock.call("org2", "repo2"),
]
github_connection.repository.assert_has_calls(expected_calls)

Expand Down