Skip to content

Commit 477167e

Browse files
authored
Merge pull request #21 from github/allow-private-repos
Allow private repos and update test
2 parents 5c04e6f + 4fb1ab7 commit 477167e

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

stale_repos.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def get_inactive_repos(github_connection, inactive_days_threshold, organization)
6868
6969
"""
7070
inactive_repos = []
71-
for repo in github_connection.repositories_by(organization):
71+
org = github_connection.organization(organization)
72+
73+
for repo in org.repositories():
7274
last_push_str = repo.pushed_at # type: ignore
7375
if last_push_str is None:
7476
continue

test_stale_repos.py

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,37 +138,57 @@ class PrintInactiveReposTestCase(unittest.TestCase):
138138
"""
139139

140140
def test_print_inactive_repos_with_inactive_repos(self):
141-
"""Test printing inactive repos that exceed the threshold.
141+
"""Test that get_inactive_repos returns the expected list of inactive repos.
142142
143-
This test verifies that the print_inactive_repos() function correctly prints the URL and
144-
days inactive for repositories that have been inactive for more than the specified
145-
threshold.
143+
This test uses a MagicMock object to simulate a GitHub API connection with a list
144+
of repositories with varying levels of inactivity. It then calls the get_inactive_repos
145+
function with the mock GitHub API connection and a threshold of 30 days. Finally, it
146+
checks that the function returns the expected list of inactive repos.
146147
147148
"""
148-
# Create a mock GitHub connection object
149-
github_connection = MagicMock()
149+
# Create a MagicMock object to simulate a GitHub API connection
150+
mock_github = MagicMock()
151+
152+
# Create a MagicMock object to simulate the organization object returned by the
153+
# GitHub API connection
154+
mock_org = MagicMock()
155+
156+
# Create MagicMock objects to simulate the repositories returned by the organization object
157+
forty_days_ago = datetime.now(timezone.utc) - timedelta(days=40)
158+
twenty_days_ago = datetime.now(timezone.utc) - timedelta(days=20)
159+
mock_repo1 = MagicMock(
160+
html_url="https://github.com/example/repo1",
161+
pushed_at=twenty_days_ago.isoformat(),
162+
archived=False,
163+
)
164+
mock_repo2 = MagicMock(
165+
html_url="https://github.com/example/repo2",
166+
pushed_at=forty_days_ago.isoformat(),
167+
archived=False,
168+
)
169+
mock_repo3 = MagicMock(
170+
html_url="https://github.com/example/repo3",
171+
pushed_at=forty_days_ago.isoformat(),
172+
archived=True,
173+
)
150174

151-
# Create a mock repository object with a last push time of 30 days ago
152-
thirty_days_ago = datetime.now(timezone.utc) - timedelta(days=30)
153-
mock_repo = MagicMock()
154-
mock_repo.pushed_at = thirty_days_ago.isoformat()
155-
mock_repo.html_url = "https://github.com/example/repo"
156-
mock_repo.archived = False
157-
github_connection.repositories_by.return_value = [mock_repo]
175+
# Set up the MagicMock objects to return the expected values when called
176+
mock_github.organization.return_value = mock_org
177+
mock_org.repositories.return_value = [
178+
mock_repo1,
179+
mock_repo2,
180+
mock_repo3,
181+
]
158182

159-
# Call the function with a threshold of 20 days
160-
inactive_days_threshold = 20
161-
organization = "example"
162-
with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
163-
get_inactive_repos(github_connection, inactive_days_threshold, organization)
164-
output = mock_stdout.getvalue()
183+
# Call the get_inactive_repos function with the mock GitHub API
184+
# connection and a threshold of 30 days
185+
inactive_repos = get_inactive_repos(mock_github, 30, "example")
165186

166-
# Check that the output contains the expected repo URL and days inactive
167-
expected_output = (
168-
f"{mock_repo.html_url}: 30 days inactive\n"
169-
f"Found 1 stale repos in {organization}\n"
170-
)
171-
self.assertEqual(expected_output, output)
187+
# Check that the function returns the expected list of inactive repos
188+
expected_inactive_repos = [
189+
("https://github.com/example/repo2", 40),
190+
]
191+
assert inactive_repos == expected_inactive_repos
172192

173193
def test_print_inactive_repos_with_no_inactive_repos(self):
174194
"""Test printing no inactive repos.

0 commit comments

Comments
 (0)