|
| 1 | +import sys |
| 2 | +import settings |
| 3 | +import utils |
| 4 | +import shutil |
| 5 | +import json |
| 6 | +import os |
| 7 | + |
| 8 | +""" |
| 9 | +This script compares the generated CSV coverage files with the ones in the codebase. And if they are different, it |
| 10 | +creates a PR with the changes. If a PR already exists, then it will be updated to reflect the latest changes. |
| 11 | +""" |
| 12 | + |
| 13 | +# Name of the branch that's used to push the updated coverage files, also the head of the PR |
| 14 | +branch_name = "workflow/coverage/update" |
| 15 | +# Name of the remote where the new commit is pushed |
| 16 | +remote = "origin" |
| 17 | +# Name of the branch that provides the base for the new branch, and the base of the PR |
| 18 | +main = "main" |
| 19 | + |
| 20 | +repo = sys.argv[2] |
| 21 | +owner = repo.split('/')[0] |
| 22 | + |
| 23 | + |
| 24 | +def overwrite_files(): |
| 25 | + languages = ['java'] |
| 26 | + for lang in languages: |
| 27 | + repo_output_rst = settings.repo_output_rst.format(language=lang) |
| 28 | + repo_output_csv = settings.repo_output_csv.format(language=lang) |
| 29 | + |
| 30 | + generated_output_rst = settings.generated_output_rst.format( |
| 31 | + language=lang) |
| 32 | + generated_output_csv = settings.generated_output_csv.format( |
| 33 | + language=lang) |
| 34 | + |
| 35 | + exists = utils.check_file_exists(generated_output_rst) |
| 36 | + if not exists: |
| 37 | + print( |
| 38 | + f"Generated RST file {generated_output_rst} is missing", file=sys.stderr) |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + exists = utils.check_file_exists(generated_output_csv) |
| 42 | + if not exists: |
| 43 | + print( |
| 44 | + f"Generated RST file {generated_output_csv} is missing", file=sys.stderr) |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + shutil.move(generated_output_rst, repo_output_rst) |
| 48 | + shutil.move(generated_output_csv, repo_output_csv) |
| 49 | + |
| 50 | + |
| 51 | +def get_pr_number(repo, owner, from_branch, to_branch): |
| 52 | + ids = utils.subprocess_check_output(["gh", "api", "-X", "GET", f"repos/{repo}/pulls", "-f", |
| 53 | + f"name={to_branch}", "-f", f"head={owner}:{from_branch}", "--jq", "[.[].number]"]) |
| 54 | + |
| 55 | + ids = json.loads(ids) |
| 56 | + |
| 57 | + if len(ids) > 1: |
| 58 | + print( |
| 59 | + f"Found more than one PR that matches the branches. {ids}", file=sys.stderr) |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + if len(ids) == 1: |
| 63 | + print(f"Matching PR found: {ids[0]}") |
| 64 | + return ids[0] |
| 65 | + |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +def create_pr(repo, from_branch, to_branch): |
| 70 | + print(f"Creating PR for {from_branch}") |
| 71 | + utils.subprocess_check_output(["gh", "pr", "create", "-R", repo, "--base", to_branch, |
| 72 | + "--head", from_branch, "--title", "Update CSV framework coverage reports", |
| 73 | + "--body", "This PR changes the CSV framework coverage reports."]) |
| 74 | + |
| 75 | + |
| 76 | +working_dir = "" |
| 77 | +if len(sys.argv) > 1: |
| 78 | + working_dir = sys.argv[1] |
| 79 | +else: |
| 80 | + print("Working directory is not specified") |
| 81 | + exit(1) |
| 82 | + |
| 83 | +found_diff = False |
| 84 | +overwrite_files() |
| 85 | + |
| 86 | +os.chdir(working_dir) |
| 87 | + |
| 88 | +already_open_pr = get_pr_number(repo, owner, branch_name, main) |
| 89 | +try: |
| 90 | + utils.subprocess_check_output(["git", "diff", "--exit-code"]) |
| 91 | + print("No differences found") |
| 92 | + found_diff = False |
| 93 | +except: |
| 94 | + print("Differences found") |
| 95 | + found_diff = True |
| 96 | + |
| 97 | +if not found_diff: |
| 98 | + if already_open_pr != 0: |
| 99 | + # close the PR |
| 100 | + utils.subprocess_run( |
| 101 | + ["gh", "pr", "close", str(already_open_pr), "-R", repo]) |
| 102 | +else: |
| 103 | + utils.subprocess_run(["git", "config", "user.name", |
| 104 | + "github-actions[bot]"]) |
| 105 | + utils.subprocess_run(["git", "config", "user.email", |
| 106 | + "41898282+github-actions[bot]@users.noreply.github.com"]) |
| 107 | + utils.subprocess_run(["git", "add", "."]) |
| 108 | + utils.subprocess_run( |
| 109 | + ["git", "commit", "-m", "Add changed framework coverage reports"]) |
| 110 | + utils.subprocess_run(["git", "branch", "-f", branch_name, main]) |
| 111 | + utils.subprocess_run(["git", "push", "-f", remote, branch_name]) |
| 112 | + if already_open_pr == 0: |
| 113 | + create_pr(repo, branch_name, main) |
0 commit comments