Skip to content

Commit 211ccef

Browse files
authored
Merge pull request #32 from jmeridth/jm-handle-organization-env-var-requirement
fix: handle if ORGANIZATION not provided
2 parents 7fc8f14 + ed7822d commit 211ccef

File tree

5 files changed

+71
-8
lines changed

5 files changed

+71
-8
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ lint:
1313
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
1414
flake8 . --count --exit-zero --max-complexity=100 --max-line-length=150 --statistics --exclude=venv,.venv,.git,__pycache__
1515
pylint --rcfile=.pylintrc --fail-under=9.0 *.py
16+
black .

cleanowners.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
import github3
88

99

10+
def get_org(github_connection, organization):
11+
"""Get the organization object"""
12+
try:
13+
return github_connection.organization(organization)
14+
except github3.exceptions.NotFoundError:
15+
print(f"Organization {organization} not found")
16+
return None
17+
18+
1019
def main(): # pragma: no cover
1120
"""Run the main program"""
1221

@@ -36,6 +45,17 @@ def main(): # pragma: no cover
3645
codeowners_count = 0
3746
users_count = 0
3847

48+
if organization and not repository_list:
49+
gh_org = get_org(github_connection, organization)
50+
if not gh_org:
51+
raise ValueError(
52+
f"""Organization {organization} is not an organization and
53+
REPOSITORY environment variable was not set.
54+
Please set valid ORGANIZATION or set REPOSITORY environment
55+
variable
56+
"""
57+
)
58+
3959
# Get the repositories from the organization or list of repositories
4060
repos = get_repos_iterator(organization, repository_list, github_connection)
4161

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

92112
for username in usernames:
113+
org = organization if organization else repo.owner.login
114+
gh_org = get_org(github_connection, org)
115+
if not gh_org:
116+
print(f"Owner {org} of repo {repo} is not an organization.")
117+
break
118+
93119
# Check to see if the username is a member of the organization
94-
if not github_connection.organization(organization).is_member(username):
120+
if not gh_org.is_member(username):
95121
print(
96-
f"\t{username} is not a member of {organization}. Suggest removing them from {repo.full_name}"
122+
f"\t{username} is not a member of {org}. Suggest removing them from {repo.full_name}"
97123
)
98124
users_count += 1
99125
if not dry_run:
@@ -148,10 +174,10 @@ def get_repos_iterator(organization, repository_list, github_connection):
148174
repos = github_connection.organization(organization).repositories()
149175
else:
150176
# Get the repositories from the repository_list
151-
for repo in repository_list:
152-
repos.append(
153-
github_connection.repository(repo.split("/")[0], repo.split("/")[1])
154-
)
177+
for full_repo_path in repository_list:
178+
org = full_repo_path.split("/")[0]
179+
repo = full_repo_path.split("/")[1]
180+
repos.append(github_connection.repository(org, repo))
155181

156182
return repos
157183

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
black==24.2.0
12
flake8==7.0.0
23
pylint==3.1.0
34
pytest==8.1.1

test_auth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test cases for the auth module."""
2+
23
import unittest
34
from unittest.mock import MagicMock, patch
45

test_cleanowners.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import uuid
55
from unittest.mock import MagicMock, patch
66

7+
import github3
78
from cleanowners import (
89
commit_changes,
10+
get_org,
911
get_repos_iterator,
1012
get_usernames_from_codeowners,
1113
)
@@ -81,6 +83,38 @@ def test_get_usernames_from_codeowners(self):
8183
self.assertEqual(result, expected_usernames)
8284

8385

86+
class TestGetOrganization(unittest.TestCase):
87+
"""Test the get_org function in cleanowners.py"""
88+
89+
@patch("github3.login")
90+
def test_get_organization_succeeds(self, mock_github):
91+
"""Test the organization is valid."""
92+
organization = "my_organization"
93+
github_connection = mock_github.return_value
94+
95+
mock_organization = MagicMock()
96+
github_connection.organization.return_value = mock_organization
97+
98+
result = get_org(github_connection, organization)
99+
100+
github_connection.organization.assert_called_once_with(organization)
101+
self.assertEqual(result, mock_organization)
102+
103+
@patch("github3.login")
104+
def test_get_organization_fails(self, mock_github):
105+
"""Test the organization is not valid."""
106+
organization = "my_organization"
107+
github_connection = mock_github.return_value
108+
109+
github_connection.organization.side_effect = github3.exceptions.NotFoundError(
110+
resp=MagicMock(status_code=404)
111+
)
112+
result = get_org(github_connection, organization)
113+
114+
github_connection.organization.assert_called_once_with(organization)
115+
self.assertIsNone(result)
116+
117+
84118
class TestGetReposIterator(unittest.TestCase):
85119
"""Test the get_repos_iterator function in evergreen.py"""
86120

@@ -111,7 +145,7 @@ def test_get_repos_iterator_with_organization(self, mock_github):
111145
def test_get_repos_iterator_with_repository_list(self, mock_github):
112146
"""Test the get_repos_iterator function with a repository list"""
113147
organization = None
114-
repository_list = ["org/repo1", "org/repo2"]
148+
repository_list = ["org/repo1", "org2/repo2"]
115149
github_connection = mock_github.return_value
116150

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

0 commit comments

Comments
 (0)