]>
Commit | Line | Data |
---|---|---|
9e46c621 PL |
1 | #!/usr/bin/env python3 |
2 | ||
3 | import requests | |
4 | import sys | |
5 | import subprocess | |
6 | import argparse | |
7 | ||
8 | ||
9 | def get_commits(pr): | |
10 | try: | |
11 | res = requests.get('https://api.github.com/repos/PowerDNS/pdns/pulls/' | |
12 | '{}/commits'.format(pr)).json() | |
13 | return [c['sha'] for c in res] | |
14 | except (ValueError, requests.exceptions.HTTPError) as e: | |
15 | print(e) | |
16 | sys.exit(1) | |
17 | ||
18 | ||
19 | def run_command(cmd): | |
20 | try: | |
21 | subprocess.check_call(cmd) | |
22 | except subprocess.CalledProcessError as e: | |
23 | print(e) | |
24 | sys.exit(1) | |
25 | ||
26 | ||
27 | a = argparse.ArgumentParser() | |
28 | action = a.add_mutually_exclusive_group(required=True) | |
29 | action.add_argument( | |
30 | '-b', '--backport-unto', metavar='REF', nargs=1, help='Backport, using ' | |
31 | 'cherry-pick, all commits from PULL_REQUEST onto REF. This is done on a ' | |
e8afde8a | 32 | 'branch called "backport-PULL_REQUEST-to-basename(REF)". When the cherry-pick fails, solve ' |
9e46c621 PL |
33 | 'the conflict as usual and run "git cherry-pick --continue --allow-empty"') |
34 | action.add_argument( | |
35 | '-m', '--merge-into', metavar='REF', nargs=1, help='Take the backport-' | |
36 | 'PULL_REQUEST branch and merge it into REF') | |
37 | a.add_argument( | |
38 | 'pull_request', metavar='PULL_REQUEST', type=int, | |
39 | help='The PR number to backport') | |
40 | ||
41 | args = a.parse_args() | |
42 | ||
43 | if args.backport_unto: | |
44 | command = ['git', 'checkout', '-b', | |
e8afde8a | 45 | 'backport-{}-to-{}'.format(args.pull_request, args.backport_unto[0].split('/')[-1]), args.backport_unto[0]] |
9e46c621 PL |
46 | run_command(command) |
47 | ||
48 | commits = get_commits(args.pull_request) | |
49 | command = ['git', 'cherry-pick', '-x', '--allow-empty'] + commits | |
50 | run_command(command) | |
51 | ||
52 | if args.merge_into: | |
53 | command = ['git', 'checkout', args.merge_into[0]] | |
54 | run_command(command) | |
55 | ||
56 | command = ['git', 'merge', '--no-ff', | |
ab74cfa8 | 57 | 'backport-{}-to-{}'.format(args.pull_request, args.merge_into[0].split('/')[-1]), '-m', |
9e46c621 PL |
58 | 'Backport #{}'.format(args.pull_request)] |
59 | run_command(command) |