Skip to content

Commit

Permalink
Merge pull request #55 from github/exempt-by-name
Browse files Browse the repository at this point in the history
Exempt by name
  • Loading branch information
zkoppert authored Aug 3, 2023
2 parents bba93b9 + ee40b22 commit 03dd905
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Below are the allowed configuration options:
| `ORGANIZATION` | false | | The organization to scan for stale repositories. If no organization is provided, this tool will search through repositories owned by the GH_TOKEN owner |
| `INACTIVE_DAYS` | true | | The number of days used to determine if repository is stale, based on `push` events |
| `EXEMPT_TOPICS` | false | | Comma separated list of topics to exempt from being flagged as stale |
| `EXEMPT_REPOS` | false | | Comma separated list of repositories to exempt from being flagged as stale. ie. `EXEMPT_TOPICS = "stale-repos,test-repo"` |
| `GH_ENTERPRISE_URL` | false | `""` | URL of GitHub Enterprise instance to use for auth instead of github.com |

### Example workflow
Expand Down
30 changes: 26 additions & 4 deletions stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ def main(): # pragma: no cover
print("No stale repos found")


def is_repo_exempt(repo, exempt_repos, exempt_topics):
"""Check if a repo is exempt from the stale repo check.
Args:
repo: The repository to check.
exempt_repos: A list of repos to exempt from the stale repo check.
exempt_topics: A list of topics to exempt from the stale repo check.
Returns:
True if the repo is exempt from the stale repo check, False otherwise.
"""
if exempt_repos and any(repo.name == exempt_repo for exempt_repo in exempt_repos):
print(f"{repo.html_url} is exempt from stale repo check")
return True
if exempt_topics and any(topic in exempt_topics for topic in repo.topics().names):
print(f"{repo.html_url} is exempt from stale repo check")
return True
return False


def get_inactive_repos(github_connection, inactive_days_threshold, organization):
"""Return and print out the repo url and days inactive if it's over
the threshold (inactive_days).
Expand All @@ -84,12 +104,14 @@ def get_inactive_repos(github_connection, inactive_days_threshold, organization)
exempt_topics = exempt_topics.split(",")
print(f"Exempt topics: {exempt_topics}")

exempt_repos = os.getenv("EXEMPT_REPOS")
if exempt_repos:
exempt_repos = exempt_repos.split(",")
print(f"Exempt repos: {exempt_repos}")

for repo in repos:
# check if repo is exempt from stale repo check
if exempt_topics and any(
topic in exempt_topics for topic in repo.topics().names
):
print(f"{repo.html_url} is exempt from stale repo check")
if is_repo_exempt(repo, exempt_repos, exempt_topics):
continue

# Get last push date
Expand Down
49 changes: 49 additions & 0 deletions test_stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from stale_repos import (
auth_to_github,
get_inactive_repos,
is_repo_exempt,
output_to_json,
write_to_markdown,
)
Expand Down Expand Up @@ -521,5 +522,53 @@ def test_json_file(self):
mock_file.__enter__.return_value.assert_has_calls(expected_calls)


class TestIsRepoExempt(unittest.TestCase):
"""
Test suite for the is_repo_exempt function.
"""

def test_exempt_repos(self):
"""
Test that a repo is exempt if its name is in the exempt_repos list.
"""
repo = MagicMock(name="repo", spec=["name", "html_url"])
repo.name = "exempt_repo"
exempt_repos = ["exempt_repo"]
exempt_topics = []

result = is_repo_exempt(repo, exempt_repos, exempt_topics)

self.assertTrue(result)

def test_exempt_topics(self):
"""
Test that a repo is exempt if one of its topics is in the exempt_topics list.
"""
repo = MagicMock(name="repo", spec=["name", "html_url", "topics"])
repo.name = "not_exempt_repo"
repo.topics.return_value.names = ["exempt_topic"]
exempt_repos = []
exempt_topics = ["exempt_topic"]

result = is_repo_exempt(repo, exempt_repos, exempt_topics)

self.assertTrue(result)

def test_not_exempt(self):
"""
Test that a repo is not exempt if it is not in the exempt_repos
list and none of its topics are in the exempt_topics list.
"""
repo = MagicMock(name="repo", spec=["name", "html_url", "topics"])
repo.name = "not_exempt_repo"
repo.topics.return_value.names = ["not_exempt_topic"]
exempt_repos = ["exempt_repo"]
exempt_topics = ["exempt_topic"]

result = is_repo_exempt(repo, exempt_repos, exempt_topics)

self.assertFalse(result)


if __name__ == "__main__":
unittest.main()

0 comments on commit 03dd905

Please sign in to comment.