added urls to results for get open prs, get open issues. added --json option

This commit is contained in:
kenna-bmcdevitt 2024-08-27 13:56:30 -05:00
parent faa5591c21
commit aac7c02167

View file

@ -1,6 +1,7 @@
import os
import argparse
import logging
import json
from github import Github, Auth, GithubException
# Configure logging
@ -34,7 +35,7 @@ class GithubSearcher():
logging.error(f"Error searching users: {e}")
self.result = None
def search_in_name(self):
def search_in_repo_name(self):
try:
self.result = self.g.search_repositories('in:name ' + self.query)
except GithubException as e:
@ -57,10 +58,7 @@ class GithubSearcher():
def get_repo_contents(self, repo):
try:
contents = repo.get_contents("")
for content_file in contents:
print(content_file)
return contents
return repo.get_contents("")
except GithubException as e:
logging.error(f"Error getting repository contents: {e}")
return None
@ -71,10 +69,11 @@ class GithubSearcher():
def main():
parser = argparse.ArgumentParser(description="Search GitHub repositories and users for PoC exploits and CVEs.")
parser.add_argument("--query", type=str, required=True, help="The search query.")
parser.add_argument("--search_type", type=str, required=True, choices=["repo", "users", "name"], help="The type of search to perform: 'repo', 'users', or 'name'.")
parser.add_argument("--search_type", type=str, required=True, choices=["repo", "users", "in-repo-name"], help="The type of search to perform: 'repo', 'users', or 'in-repo-name'.")
parser.add_argument("--get_file_contents", action="store_true", help="Get the contents of repo results.")
parser.add_argument("--get_open_issues", action="store_true", help="Get the open issues of repo results.")
parser.add_argument("--get_open_pull_requests", action="store_true", help="Get the open pull requests of repo results.")
parser.add_argument("--json", action="store_true", help="Output results in JSON format.")
args = parser.parse_args()
@ -84,33 +83,45 @@ def main():
searcher.search_repo()
elif args.search_type == "users":
searcher.search_users()
elif args.search_type == "name":
searcher.search_in_name()
elif args.search_type == "in-repo-name":
searcher.search_in_repo_name()
result = searcher.get_result()
if result is None:
print("No results found.")
return
output = []
for item in result:
if args.search_type == "users":
user_repos = item.get_repos()
for repo in user_repos:
print(repo.html_url)
repo_info = {"repo_url": repo.html_url, "repo_name": repo.name, "repo_description": repo.description}
output.append(repo_info)
else:
print(item.html_url)
repo_info = {"repo_url": item.html_url, "repo_name": item.name, "repo_description": item.description}
if args.get_open_issues:
searcher.get_repo_open_issues(item)
print("\n")
open_issues = searcher.get_repo_open_issues(item)
if open_issues:
repo_info["open_issues"] = [{"title": issue.title, "url": issue.html_url} for issue in open_issues]
if args.get_open_pull_requests:
searcher.get_repo_open_pull_requests(item)
print("\n")
open_prs = searcher.get_repo_open_pull_requests(item)
if open_prs:
repo_info["open_pull_requests"] = [{"title": pr.title, "url": pr.html_url} for pr in open_prs]
if args.get_file_contents:
searcher.get_repo_contents(item)
print("\n")
contents = searcher.get_repo_contents(item)
if contents:
repo_info["contents"] = [content_file.name for content_file in contents]
output.append(repo_info)
if args.json:
print(json.dumps(output, indent=4))
else:
for item in output:
print(item)
if __name__ == "__main__":
main()