-
Notifications
You must be signed in to change notification settings - Fork 33
Repo filtering #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Repo filtering #48
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ def __init__(self, base_url, token_env, commits_api_url, throttle_time=2): | |
| self._commits_api_url = commits_api_url | ||
| self._throttle_time = throttle_time | ||
|
|
||
| def run_github_search(self, search_query, extension, org=[], repo=[]): | ||
| def run_github_search(self, search_query, extension, org=[], repo=[], search_archived = True, search_forked = True): | ||
| """ | ||
| Run the GitHub API search with given search query | ||
| Get the items from the response content and Return | ||
|
|
@@ -73,20 +73,24 @@ def run_github_search(self, search_query, extension, org=[], repo=[]): | |
|
|
||
| if not extension or extension == "others" or len(extension) == 0: | ||
| response = self.__github_api_get_params( | ||
| search_query, org_qualifiers, repo_qualifiers | ||
| search_query, org_qualifiers, repo_qualifiers, search_archived, search_forked | ||
| ) | ||
| elif self._token_env == "public": | ||
|
|
||
| response = self.__github_api_get_params( | ||
| (search_query + " extension:" + extension), | ||
| org_qualifiers, | ||
| repo_qualifiers, | ||
| search_archived, | ||
| search_forked | ||
| ) | ||
| else: | ||
| response = self.__github_api_get_params( | ||
| (search_query + " extension:" + extension), | ||
| org_qualifiers, | ||
| repo_qualifiers, | ||
| search_archived, | ||
| search_forked | ||
| ) | ||
|
|
||
| if response: | ||
|
|
@@ -95,7 +99,7 @@ def run_github_search(self, search_query, extension, org=[], repo=[]): | |
| return [] | ||
|
|
||
| def __github_api_get_params( | ||
| self, search_query, org_qualifiers="", repo_qualifiers="" | ||
| self, search_query, org_qualifiers="", repo_qualifiers="", search_archived = True, search_forked = True | ||
| ): | ||
| """ | ||
| For the given GITHUB API url and search query, call the api | ||
|
|
@@ -132,13 +136,17 @@ def __github_api_get_params( | |
| elif len(repo_qualifiers) > 0: | ||
| additional_qualifiers = repo_qualifiers | ||
|
|
||
| archive_filter = "" if search_archived else "NOT is:archived" | ||
| forked_filter = "" if search_forked else "NOT is:fork" | ||
|
|
||
| search_response = [] | ||
| if additional_qualifiers: | ||
| try: | ||
| q_string = f"{search_query} {additional_qualifiers} {archive_filter} {forked_filter}" | ||
| response = requests.get( | ||
| self._base_url, | ||
| params={ | ||
| "q": f"{search_query} {additional_qualifiers}", | ||
| "q": q_string, | ||
| "order": "desc", | ||
| "sort": "indexed", | ||
| "per_page": 100, | ||
|
|
@@ -149,10 +157,11 @@ def __github_api_get_params( | |
| logger.error(f"Github API call Error: {e}") | ||
| else: | ||
| try: | ||
| q_string = f"{search_query} {archive_filter} {forked_filter}" | ||
| response = requests.get( | ||
| self._base_url, | ||
| params={ | ||
| "q": f"{search_query}", | ||
| "q": q_string, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. search api Not filtering archived repos correctly.Need to fix it |
||
| "order": "desc", | ||
| "sort": "indexed", | ||
| "per_page": 100, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -540,7 +540,7 @@ def format_search_query_list(secondary_keywords): | |
|
|
||
|
|
||
| def run_detection( | ||
| secondary_keywords=[], extensions=[], ml_prediction=False, org=[], repo=[] | ||
| secondary_keywords=[], extensions=[], ml_prediction=False, org=[], repo=[], search_archived = True, search_forked = True | ||
| ): | ||
| """ | ||
| Run GitHub detections | ||
|
|
@@ -646,7 +646,7 @@ def run_detection( | |
| # Search GitHub and return search response confidence_score | ||
| total_processed_search += 1 | ||
| search_response_lines = githubCalls.run_github_search( | ||
| search_query, extension, org, repo | ||
| search_query, extension, org, repo, search_archived, search_forked | ||
| ) | ||
| # If search has detections, process the result urls else continue next search | ||
| if search_response_lines: | ||
|
|
@@ -782,6 +782,28 @@ def arg_parser(): | |
| help="Pass the repo name list as comma separated string", | ||
| ) | ||
|
|
||
| argparser.add_argument( | ||
| "-a", | ||
| "--archived", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please update the argument parser as --include-archived or -ia and for forked --include-forked or -if for better user understanding or also change help message like "Pass Yes or No to include archived repositories for search. Default is Yes". change in all the respective files wherever used |
||
| metavar="Archived", | ||
| action="store", | ||
| type=str, | ||
| default="Yes", | ||
| choices=flag_choices, | ||
| help="Pass Yes or No to search for Archived repos. Default is Yes", | ||
| ) | ||
|
|
||
| argparser.add_argument( | ||
| "-f", | ||
| "--forked", | ||
| metavar="Forked", | ||
| action="store", | ||
| type=str, | ||
| default="Yes", | ||
| choices=flag_choices, | ||
| help="Pass Yes or No to search for Forked repos. Default is Yes", | ||
| ) | ||
|
|
||
| argparser.add_argument( | ||
| "-l", | ||
| "--log_level", | ||
|
|
@@ -839,6 +861,16 @@ def arg_parser(): | |
| else: | ||
| repo = [] | ||
|
|
||
| if args.archived.lower() in flag_choices[:5]: | ||
| search_archived = True | ||
| else: | ||
| search_archived = False | ||
|
|
||
| if args.forked.lower() in flag_choices[:5]: | ||
| search_forked = True | ||
| else: | ||
| search_forked = False | ||
|
|
||
| if args.log_level in log_level_choices: | ||
| log_level = args.log_level | ||
| else: | ||
|
|
@@ -855,6 +887,8 @@ def arg_parser(): | |
| unmask_secret, | ||
| org, | ||
| repo, | ||
| search_archived, | ||
| search_forked, | ||
| log_level, | ||
| console_logging, | ||
| ) | ||
|
|
@@ -869,6 +903,8 @@ def arg_parser(): | |
| unmask_secret, | ||
| org, | ||
| repo, | ||
| search_archived, | ||
| search_forked, | ||
| log_level, | ||
| console_logging, | ||
| ) = arg_parser() | ||
|
|
@@ -896,6 +932,6 @@ def arg_parser(): | |
| ) | ||
| sys.exit(1) | ||
|
|
||
| run_detection(secondary_keywords, extensions, ml_prediction, org, repo) | ||
| run_detection(secondary_keywords, extensions, ml_prediction, org, repo, search_archived, search_forked) | ||
|
|
||
| logger.info("xGitGuard Credentials Detection Process Completed") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
search api Not filtering archived repos correctly.Need to fix it