]> git.proxmox.com Git - ceph.git/blame - ceph/src/script/build-integration-branch
import quincy beta 17.1.0
[ceph.git] / ceph / src / script / build-integration-branch
CommitLineData
9f95a23c
TL
1#!/usr/bin/env python3
2
3"""
f67539c2 4Builds integration branches. Something similar to
9f95a23c
TL
5 $ git checkout -b branch-name
6 $ for b in $(get-branches-from-github) ; do
7 > git pull b
8 > done
9
10Requires `~/.github_token`.
11
12
13Usage:
14 build-integration-branch <label> [--no-date]
15 build-integration-branch -h | --help
16
17Options:
18 -h --help Show this screen.
19 --no-date Don't add `{postfix}` to the branch name.
20"""
28e407b8 21
28e407b8
AA
22import json
23import os
24import requests
28e407b8
AA
25import sys
26import time
f67539c2
TL
27
28from subprocess import call, check_output
29from urllib.parse import urljoin
28e407b8 30
9f95a23c
TL
31TIME_FORMAT = '%Y-%m-%d-%H%M'
32postfix = "-" + time.strftime(TIME_FORMAT, time.localtime())
33
f67539c2
TL
34current_branch = check_output('git rev-parse --abbrev-ref HEAD',
35 shell=True).strip().decode()
36if current_branch in 'mimic nautilus octopus pacific'.split():
37 postfix += '-' + current_branch
38 print(f"Adding current branch name '-{current_branch}' as a postfix")
39
28e407b8
AA
40repo = "ceph/ceph"
41
9f95a23c 42try:
f67539c2
TL
43 from docopt import docopt
44 arguments = docopt(__doc__.format(postfix=postfix))
45 label = arguments['<label>']
46 branch = label
47 if not arguments['--no-date']:
48 branch += postfix
9f95a23c 49except ImportError:
f67539c2
TL
50 # Fallback without docopt.
51 label = sys.argv[1]
52 assert len(sys.argv) == 2
53 branch = label + postfix
9f95a23c
TL
54
55
f67539c2 56with open(os.path.expanduser('~/.github_token')) as myfile:
28e407b8
AA
57 token = myfile.readline().strip()
58
59# get prs
f67539c2
TL
60baseurl = urljoin('https://api.github.com',
61 ('repos/{repo}/issues?labels={label}'
62 '&sort=created'
63 '&direction=asc'))
64url = baseurl.format(label=label,
65 repo=repo)
66r = requests.get(url,
67 headers={'Authorization': 'token %s' % token})
28e407b8
AA
68assert(r.ok)
69j = json.loads(r.text or r.content)
70print("--- found %d issues tagged with %s" % (len(j), label))
71
72prs = []
73prtext = []
74for issue in j:
75 if 'pull_request' not in issue:
76 continue
f67539c2
TL
77 r = requests.get(issue['pull_request']['url'],
78 headers={'Authorization': 'token %s' % token})
28e407b8
AA
79 pr = json.loads(r.text or r.content)
80 prs.append(pr)
81 prtext.append(pr['html_url'] + ' - ' + pr['title'])
82print("--- queried %s prs" % len(prs))
83
28e407b8
AA
84print("branch %s" % branch)
85
86# assemble
87print('--- creating branch %s' % branch)
9f95a23c 88r = call(['git', 'branch', '-D', branch])
28e407b8
AA
89r = call(['git', 'checkout', '-b', branch])
90assert not r
91for pr in prs:
20effc67
TL
92 pr_number = pr['number']
93 pr_url = pr['head']['repo']['clone_url']
94 pr_ref = pr['head']['ref']
95 print(f'--- pr {pr_number} --- pulling {pr_url} branch {pr_ref}')
96 while True:
97 r = call(['git', 'pull', '--no-ff', '--no-edit', pr_url, pr_ref])
98 if r == 0:
99 break
100 elif r == 1:
101 print(f'Unable to access {pr_url}, retrying..')
102 elif r == 128:
103 message = f'Unable to resolve conflict when merging PR#{pr_number}'
104 raise Exception(message)
105 else:
106 message = ('Exiting due to an unknown failure when pulling '
107 f'PR#{pr_number}')
108 raise Exception(message)
109
28e407b8
AA
110print('--- done. these PRs were included:')
111print('\n'.join(prtext).encode('ascii', errors='ignore').decode())
20effc67 112print('--- perhaps you want to: ./run-make-check.sh && git push ci %s' % branch)