|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +import argparse |
| 19 | +import re |
| 20 | +import os |
| 21 | +import textwrap |
| 22 | +from dataclasses import dataclass |
| 23 | +from typing import Any, List, Callable |
| 24 | + |
| 25 | + |
| 26 | +from git_utils import GitHubRepo, parse_remote, git |
| 27 | +from cmd_utils import init_log, tags_from_title |
| 28 | + |
| 29 | + |
| 30 | +GITHUB_USERNAME_REGEX = re.compile(r"(@[a-zA-Z0-9-]+)", flags=re.MULTILINE) |
| 31 | +OK = object() |
| 32 | +FAIL = object() |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class Check: |
| 37 | + # check to run, returning OK means it passed, anything else means it failed |
| 38 | + check: Callable[[str], Any] |
| 39 | + |
| 40 | + # function to call to generate the error message |
| 41 | + error_fn: Callable[[Any], str] |
| 42 | + |
| 43 | + |
| 44 | +def non_empty(s: str): |
| 45 | + if len(s) == 0: |
| 46 | + return FAIL |
| 47 | + return OK |
| 48 | + |
| 49 | + |
| 50 | +def usernames(s: str): |
| 51 | + m = GITHUB_USERNAME_REGEX.findall(s) |
| 52 | + return m if m else OK |
| 53 | + |
| 54 | + |
| 55 | +def tags(s: str): |
| 56 | + items = tags_from_title(s) |
| 57 | + if len(items) == 0: |
| 58 | + return FAIL |
| 59 | + return OK |
| 60 | + |
| 61 | + |
| 62 | +def trailing_period(s: str): |
| 63 | + if s.endswith("."): |
| 64 | + return FAIL |
| 65 | + return OK |
| 66 | + |
| 67 | + |
| 68 | +title_checks = [ |
| 69 | + Check(check=non_empty, error_fn=lambda d: "PR must have a title but title was empty"), |
| 70 | + Check(check=trailing_period, error_fn=lambda d: "PR must not end in a tailing '.'"), |
| 71 | + # TODO(driazati): enable this check once https://github.com/apache/tvm/issues/12637 is done |
| 72 | + # Check( |
| 73 | + # check=usernames, |
| 74 | + # error_fn=lambda d: f"PR title must not tag anyone but found these usernames: {d}", |
| 75 | + # ), |
| 76 | +] |
| 77 | +body_checks = [ |
| 78 | + Check(check=non_empty, error_fn=lambda d: "PR must have a body but body was empty"), |
| 79 | + # TODO(driazati): enable this check once https://github.com/apache/tvm/issues/12637 is done |
| 80 | + # Check( |
| 81 | + # check=usernames, |
| 82 | + # error_fn=lambda d: f"PR body must not tag anyone but found these usernames: {d}", |
| 83 | + # ), |
| 84 | +] |
| 85 | + |
| 86 | + |
| 87 | +def run_checks(checks: List[Check], s: str, name: str) -> bool: |
| 88 | + print(f"Running checks for {name}") |
| 89 | + print(textwrap.indent(s, prefix=" ")) |
| 90 | + passed = True |
| 91 | + print(" Checks:") |
| 92 | + for i, check in enumerate(checks): |
| 93 | + result = check.check(s) |
| 94 | + if result == OK: |
| 95 | + print(f" [{i+1}] {check.check.__name__}: PASSED") |
| 96 | + else: |
| 97 | + passed = False |
| 98 | + msg = check.error_fn(result) |
| 99 | + print(f" [{i+1}] {check.check.__name__}: FAILED: {msg}") |
| 100 | + |
| 101 | + return passed |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + init_log() |
| 106 | + help = "Check a PR's title and body for conformance to guidelines" |
| 107 | + parser = argparse.ArgumentParser(description=help) |
| 108 | + parser.add_argument("--pr", required=True) |
| 109 | + parser.add_argument("--remote", default="origin", help="ssh remote to parse") |
| 110 | + parser.add_argument( |
| 111 | + "--pr-body", help="(testing) PR body to use instead of fetching from GitHub" |
| 112 | + ) |
| 113 | + parser.add_argument( |
| 114 | + "--pr-title", help="(testing) PR title to use instead of fetching from GitHub" |
| 115 | + ) |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + try: |
| 119 | + pr = int(args.pr) |
| 120 | + except ValueError: |
| 121 | + print(f"PR was not a number: {args.pr}") |
| 122 | + exit(0) |
| 123 | + |
| 124 | + if args.pr_body: |
| 125 | + body = args.pr_body |
| 126 | + title = args.pr_title |
| 127 | + else: |
| 128 | + remote = git(["config", "--get", f"remote.{args.remote}.url"]) |
| 129 | + user, repo = parse_remote(remote) |
| 130 | + |
| 131 | + github = GitHubRepo(token=os.environ["GITHUB_TOKEN"], user=user, repo=repo) |
| 132 | + pr = github.get(f"pulls/{args.pr}") |
| 133 | + body = pr["body"] |
| 134 | + title = pr["title"] |
| 135 | + |
| 136 | + body = body.strip() |
| 137 | + title = title.strip() |
| 138 | + |
| 139 | + title_passed = run_checks(checks=title_checks, s=title, name="PR title") |
| 140 | + print("") |
| 141 | + body_passed = run_checks(checks=body_checks, s=body, name="PR body") |
| 142 | + |
| 143 | + if title_passed and body_passed: |
| 144 | + print("All checks passed!") |
| 145 | + exit(0) |
| 146 | + else: |
| 147 | + print( |
| 148 | + "Some checks failed, please review the logs above and edit your PR on GitHub accordingly" |
| 149 | + ) |
| 150 | + exit(1) |
0 commit comments