From b470180710bbdea661a00d1a1e51037381817930 Mon Sep 17 00:00:00 2001 From: htrgouvea Date: Mon, 8 Apr 2024 19:43:47 -0300 Subject: [PATCH] new feature --- scripts/search_files.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/scripts/search_files.py b/scripts/search_files.py index 4f14f3a..b6a406f 100644 --- a/scripts/search_files.py +++ b/scripts/search_files.py @@ -3,6 +3,7 @@ import re import requests import argparse +from datetime import datetime, timedelta def repositories(org, token): response = requests.get( @@ -27,16 +28,40 @@ def search_file(repository, token): return False +def last_commit_date(repository, token): + response = requests.get( + f'https://api.github.com/repos/{repository}/commits', + headers={'Authorization': f'Bearer {token}'} + ) + + if response.status_code == 200: + if len(response.json()) > 0: + last_commit_date_str = response.json()[0]['commit']['committer']['date'] + last_commit_date = datetime.strptime(last_commit_date_str, "%Y-%m-%dT%H:%M:%SZ") + return last_commit_date + return None + if __name__ == '__main__': parse = argparse.ArgumentParser(description='Search for specific files in the repositories of an organization.') parse.add_argument('--org', help='Specify the name of the organization\n', required=True) parse.add_argument('--token', help='Set the Github Token to use during actions.', required=True) + parse.add_argument('--maintained', help='Check last commit date of repositories.', action='store_true') + parse.add_argument('--dependency', help='Check for dependabot.yaml file in repositories.', action='store_true') args = parse.parse_args() if args.org: repos = repositories(args.org, args.token) - for repository in repos: - dependabot = search_file(repository, args.token) + if args.dependency: + for repository in repos: + dependabot = search_file(repository, args.token) + + if dependabot: + print(dependabot) + + if args.maintained: + for repository in repos: + last_commit = last_commit_date(repository, args.token) - print (dependabot) if dependabot else None \ No newline at end of file + if last_commit and datetime.utcnow() - last_commit > timedelta(days=90): + print(f'The repository {repository} has not been updated for more than 90 days.') \ No newline at end of file