|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Returns top commit of a PR (mostly used to comments) |
| 4 | +""" |
| 5 | +from os.path import expanduser, dirname, abspath, join, exists |
| 6 | +from optparse import OptionParser |
| 7 | +from socket import setdefaulttimeout |
| 8 | +from github_utils import ( |
| 9 | + api_rate_limits, |
| 10 | + get_pr_commits, |
| 11 | + get_pr_latest_commit, |
| 12 | + get_gh_token, |
| 13 | + enable_github_loggin, |
| 14 | +) |
| 15 | + |
| 16 | +setdefaulttimeout(120) |
| 17 | +import sys |
| 18 | + |
| 19 | +SCRIPT_DIR = dirname(abspath(sys.argv[0])) |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + parser = OptionParser(usage="%prog <pull-request-id>") |
| 23 | + parser.add_option( |
| 24 | + "-c", |
| 25 | + "--commit", |
| 26 | + dest="commit", |
| 27 | + action="store_true", |
| 28 | + help="Get last commit of the PR", |
| 29 | + default=False, |
| 30 | + ) |
| 31 | + parser.add_option( |
| 32 | + "-a", |
| 33 | + "--all", |
| 34 | + dest="all", |
| 35 | + action="store_true", |
| 36 | + help="Get all commits of the PR", |
| 37 | + default=False, |
| 38 | + ) |
| 39 | + parser.add_option( |
| 40 | + "-n", |
| 41 | + "--dry-run", |
| 42 | + dest="dryRun", |
| 43 | + action="store_true", |
| 44 | + help="Do not modify Github", |
| 45 | + default=False, |
| 46 | + ) |
| 47 | + parser.add_option( |
| 48 | + "-f", |
| 49 | + "--force", |
| 50 | + dest="force", |
| 51 | + action="store_true", |
| 52 | + help="Force process the issue/PR even if it is ignored.", |
| 53 | + default=False, |
| 54 | + ) |
| 55 | + parser.add_option( |
| 56 | + "-r", |
| 57 | + "--repository", |
| 58 | + dest="repository", |
| 59 | + help="Github Repositoy name e.g. cms-sw/cmssw.", |
| 60 | + type=str, |
| 61 | + default="cms-sw/cmssw", |
| 62 | + ) |
| 63 | + parser.add_option( |
| 64 | + "-d", |
| 65 | + "--debug", |
| 66 | + dest="debug", |
| 67 | + action="store_true", |
| 68 | + help="Enable debug logging in PyGithub", |
| 69 | + default=False, |
| 70 | + ) |
| 71 | + opts, args = parser.parse_args() |
| 72 | + if opts.debug: |
| 73 | + enable_github_loggin() |
| 74 | + |
| 75 | + if len(args) != 1: |
| 76 | + parser.error("Too many/few arguments") |
| 77 | + prId = int(args[0]) # Positional argument is "Pull request ID" |
| 78 | + if opts.commit: |
| 79 | + if opts.all: |
| 80 | + for c in get_pr_commits(prId, opts.repository): |
| 81 | + print(c["sha"]) |
| 82 | + else: |
| 83 | + print(get_pr_latest_commit(args[0], opts.repository)) |
| 84 | + else: |
| 85 | + from github import Github |
| 86 | + |
| 87 | + repo_dir = join(SCRIPT_DIR, "repos", opts.repository.replace("-", "_")) |
| 88 | + if exists(repo_dir): |
| 89 | + sys.path.insert(0, repo_dir) |
| 90 | + import repo_config |
| 91 | + |
| 92 | + if not getattr(repo_config, "RUN_DEFAULT_CMS_BOT", True): |
| 93 | + sys.exit(0) |
| 94 | + if getattr(repo_config, "REQUEST_PROCESSOR", "cms-bot") != "cms-bot": |
| 95 | + sys.exit(0) |
| 96 | + gh = Github(login_or_token=get_gh_token(opts.repository), per_page=100) |
| 97 | + api_rate_limits(gh) |
| 98 | + repo = gh.get_repo(opts.repository) |
| 99 | + from process_pr import process_pr |
| 100 | + |
| 101 | + process_pr(repo_config, gh, repo, repo.get_issue(prId), opts.dryRun, force=opts.force) |
| 102 | + api_rate_limits(gh) |
0 commit comments