-
Notifications
You must be signed in to change notification settings - Fork 2k
Misc: Add script creating DCA source suites from MRVA #19232
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
Open
tausbn
wants to merge
2
commits into
main
Choose a base branch
from
tausbn/misc-add-script-for-converting-mrva-results-to-dca-suite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import os | ||
| import re | ||
| import subprocess | ||
| import tempfile | ||
| import argparse | ||
| from collections import defaultdict | ||
|
|
||
| help_text = """ | ||
| To use this script, pass the URL of a GitHub Gist as an argument. The Gist should contain the | ||
| exported MarkDown output of a MRVA run. | ||
|
|
||
| The script clones the Gist to a temporary directory, and constructs a DCA source suite that covers the same repos/SHAs that had results in the Gist. | ||
|
|
||
| Additionally, you can limit the list of repos to just the ones for which number of results are within a given range, by passing the --min and --max arguments. | ||
| """ | ||
|
|
||
| def clone_gist(gist_url, repo_dir): | ||
| try: | ||
| subprocess.run( | ||
| ["gh", "gist", "clone", gist_url, repo_dir], | ||
| check=True, | ||
| stderr=subprocess.DEVNULL | ||
| ) | ||
| except subprocess.CalledProcessError: | ||
| print(f"Failed to clone the gist from {gist_url}") | ||
| subprocess.run(["rm", "-rf", repo_dir]) | ||
| exit(1) | ||
|
|
||
| def get_mrva_test_name(repo_dir): | ||
| """ | ||
| Returns a kebab-case name for the MRVA test, based on the first header of the _summary.md file. | ||
| """ | ||
| # Format of first header: ### Results for "name goes here" | ||
| # In this case, the return value is "name-goes-here" | ||
| with open(os.path.join(repo_dir, "_summary.md"), "r") as summary_file: | ||
| # Find the first line that matches "Results for" | ||
| for line in summary_file: | ||
| if line.startswith("### Results for"): | ||
| # Extract the quoted name | ||
| return line.split('"')[1].replace(" ", "-") | ||
| return "unknown-name" | ||
|
|
||
| def get_repo_alert_counts(repo_dir): | ||
| """ | ||
| Parses the Summary table in the _summary.md file to produce a dict mapping repo NWOs to alert counts. | ||
| """ | ||
| with open(os.path.join(repo_dir, "_summary.md"), "r") as summary_file: | ||
| # Skip ahead to the Summary | ||
| for line in summary_file: | ||
| if line.startswith("### Summary"): | ||
| break | ||
|
|
||
| # Match remaining lines to extract the repo NWO and alert count using a regex. | ||
| # Example line: | Nuitka/Nuitka | [45 result(s)](#file-result-01-Nuitka-Nuitka-md) | | ||
| line_re = re.compile(r"\| ([^|]+) \| \[([0-9,]+) result") | ||
| d = {} | ||
| for line in summary_file: | ||
| m = line_re.match(line) | ||
| if m: | ||
| nwo, count = m.groups() | ||
| d[nwo] = int(count.replace(",", "")) | ||
| return d | ||
|
|
||
| def get_repo_nwo_shas(repo_dir): | ||
| """ | ||
| Parses each non _summary.md file in the repo_dir to produce a dict mapping repo NWOs to their corresponding SHAs. | ||
| """ | ||
| # We want to look for a match in the file of the form | ||
| # github.com/Nuitka/Nuitka/blob/b289ee4f9d55172ed5165dab262d49bfa9cb2586/ | ||
| # and extract the NWO (as a single unit) and SHA | ||
| nwo_sha_re = re.compile(r"github.com/([^/]+/[^/]+)/blob/([0-9a-f]{40})/") | ||
|
|
||
| repo_nwo_shas = {} | ||
| for filename in os.listdir(repo_dir): | ||
| if filename.endswith(".md") and filename != "_summary.md": | ||
| with open(os.path.join(repo_dir, filename), "r") as file: | ||
| for line in file: | ||
| m = nwo_sha_re.search(line) | ||
| if m: | ||
| nwo, sha = m.groups() | ||
| repo_nwo_shas[nwo] = sha | ||
| break | ||
| return repo_nwo_shas | ||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description="Calculate MRVA totals from a GitHub Gist", epilog=help_text, formatter_class=argparse.RawTextHelpFormatter) | ||
| parser.add_argument("gist_url", nargs='?', help="URL of the GitHub Gist") | ||
| parser.add_argument("--keep-dir", action="store_true", help="Keep the temporary directory") | ||
| parser.add_argument("--min", type=int, help="Minimum number of alerts in repo") | ||
| parser.add_argument("--max", type=int, help="Maximum number of alerts in repo") | ||
| parser.add_argument("--language", type=str, required=True, help="Language of the MRVA run") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not args.gist_url: | ||
| parser.print_help() | ||
| exit(1) | ||
|
|
||
| repo_dir = tempfile.mkdtemp(dir=".") | ||
| clone_gist(args.gist_url, repo_dir) | ||
|
|
||
| repo_alerts = get_repo_alert_counts(repo_dir) | ||
| repo_nwo_shas = get_repo_nwo_shas(repo_dir) | ||
|
|
||
| min_count = args.min if args.min else min(repo_alerts.values()) | ||
|
tausbn marked this conversation as resolved.
Outdated
|
||
| max_count = args.max if args.max else max(repo_alerts.values()) | ||
|
tausbn marked this conversation as resolved.
Outdated
|
||
|
|
||
| filtered_alerts = { | ||
| nwo: count for nwo, count in repo_alerts.items() if min_count <= count <= max_count | ||
| } | ||
|
|
||
| test_name = get_mrva_test_name(repo_dir) | ||
|
|
||
| source_suite_name = f"{test_name}" | ||
| if args.min: | ||
| source_suite_name += f"-min-{args.min}" | ||
| if args.max: | ||
| source_suite_name += f"-max-{args.max}" | ||
| source_suite_name += ".yml" | ||
|
|
||
| with open(source_suite_name, "w") as source_suite_file: | ||
| source_suite_file.write("# This file was generated by misc/scripts/mrva-to-dca-source-suite.py\n") | ||
| source_suite_file.write(f"# Input Gist: {args.gist_url}\n\n") | ||
| for nwo, count in filtered_alerts.items(): | ||
| source_suite_file.write(f"- language: {args.language}\n") | ||
| source_suite_file.write(f" sha: {repo_nwo_shas[nwo]}\n") | ||
| source_suite_file.write(f" slug: {nwo} # Alert count: {count}\n") | ||
|
|
||
| print(f"Source suite written to {source_suite_name}") | ||
|
|
||
| if args.keep_dir: | ||
| print(f"Temporary directory retained at: {repo_dir}") | ||
| else: | ||
| subprocess.run(["rm", "-rf", repo_dir]) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.