|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# (C) Crown copyright Met Office. All rights reserved. |
| 3 | +# The file LICENCE, distributed with this code, contains details of the terms |
| 4 | +# under which the code may be used. |
| 5 | +# ----------------------------------------------------------------------------- |
| 6 | + |
| 7 | +""" |
| 8 | +This script will change the milestone on all closed pull requests that don't |
| 9 | +have one. |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +from pathlib import Path |
| 14 | +from review_project import ProjectData, REVIEW_ID |
| 15 | + |
| 16 | + |
| 17 | +def print_banner(message: str) -> None: |
| 18 | + print("\n") |
| 19 | + print("=" * len(message)) |
| 20 | + print(message) |
| 21 | + print("=" * len(message)) |
| 22 | + |
| 23 | + |
| 24 | +def add_milestone( |
| 25 | + reviews: ProjectData, current_milestone: str, dry_run: bool = False |
| 26 | +) -> None: |
| 27 | + """ |
| 28 | + Set a milestone for closed PRs without one. |
| 29 | +
|
| 30 | + reviews: ProjectData from the Review Tracker Project |
| 31 | + current_milestone: Milestone to set |
| 32 | + dry_run: If true, do not actually modify the milestone |
| 33 | + """ |
| 34 | + |
| 35 | + print_banner( |
| 36 | + f"Setting closed pull requests with no milestone to {current_milestone}" |
| 37 | + ) |
| 38 | + |
| 39 | + closed_prs = reviews.get_milestone(milestone="None", status="closed") |
| 40 | + |
| 41 | + if closed_prs: |
| 42 | + for repo in closed_prs: |
| 43 | + for pr in closed_prs[repo]: |
| 44 | + pr.modify_milestone(current_milestone, dry_run) |
| 45 | + else: |
| 46 | + print("No closed pull requests without a milestone.") |
| 47 | + |
| 48 | + |
| 49 | +def parse_args(): |
| 50 | + """ |
| 51 | + Read command line args |
| 52 | + """ |
| 53 | + |
| 54 | + testfile_path = Path(__file__).parent / "test" |
| 55 | + |
| 56 | + parser = argparse.ArgumentParser( |
| 57 | + "Set all closed pull requests without a milestone to the requested milestone." |
| 58 | + ) |
| 59 | + |
| 60 | + parser.add_argument("--milestone", help="Milestone to set") |
| 61 | + parser.add_argument( |
| 62 | + "--test", |
| 63 | + action="store_true", |
| 64 | + help="Use test input files.", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--capture_project", |
| 68 | + action="store_true", |
| 69 | + help="Capture the current project status into the test file", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--file", |
| 73 | + default=testfile_path, |
| 74 | + help="Filepath to test data for either capturing the project status, " |
| 75 | + "or use as input data.", |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--dry", |
| 79 | + action="store_true", |
| 80 | + help="Dry run. Print commands, don't action them. Always true when " |
| 81 | + "running with test data.", |
| 82 | + ) |
| 83 | + |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + args.file = Path(args.file) |
| 87 | + args.file = args.file.expanduser().resolve() |
| 88 | + |
| 89 | + if args.test: |
| 90 | + args.dry = True |
| 91 | + |
| 92 | + return args |
| 93 | + |
| 94 | + |
| 95 | +def main( |
| 96 | + milestone: str, test: bool, capture_project: bool, file: Path, dry: bool |
| 97 | +) -> None: |
| 98 | + # Get milestone data |
| 99 | + if test: |
| 100 | + review_data = ProjectData.from_file(REVIEW_ID, file / "pr.json") |
| 101 | + else: |
| 102 | + review_data = ProjectData.from_github( |
| 103 | + REVIEW_ID, capture_project, file / "pr.json" |
| 104 | + ) |
| 105 | + |
| 106 | + add_milestone(review_data, milestone, dry) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + args = parse_args() |
| 111 | + main(args.milestone, args.test, args.capture_project, args.file, args.dry) |
0 commit comments