Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci,crusher: increase supported num branches #3902

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions .gitlab/config/generate_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,43 @@
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def request_as_dict(url):
r = requests.get(url + '?per_page=100', verify=False)
return r.json()
class skip_after_n_successes:
def __init__(self, default_value, n):
self.runs_max = n
self.runs_current = 0
self.default_value = default_value

def __call__(self, fn, *args, **kwargs):
if self.runs_current >= self.runs_max:
return self.default_value

ret = fn(*args, **kwargs)
if ret:
self.runs_current += 1
return ret


def http_get_request(*args, **kwargs):
kwargs['verify'] = False
return requests.get(*args, **kwargs)


def request_as_list(url, *args, **kwargs):
current_url = url
body_json = []
while current_url:
response = http_get_request(current_url, *args, **kwargs)
body_json += response.json()

header = response.headers
current_url = None
if 'link' in header:
links = re.search(
r'(?<=\<)([\S]*)(?=>; rel="next")', header['link'], flags=re.IGNORECASE)
if links is not None:
current_url = links.group(0)

return body_json


def add_timestamp(branch):
Expand All @@ -44,7 +78,12 @@ def has_no_status(branch):
gh_commit_sha = branch['commit']['parent_ids'][1]

# Query GitHub for the status of this commit
commit = request_as_dict(gh_url + '/commits/' + gh_commit_sha + '/status')
response = http_get_request(
gh_url + '/commits/' + gh_commit_sha + '/status')
if int(response.headers['x-ratelimit-remaining']) <= 0:
raise ConnectionError(response.json())
scottwittenburg marked this conversation as resolved.
Show resolved Hide resolved

commit = response.json()
if commit is None or 'sha' not in commit:
return False

Expand Down Expand Up @@ -88,14 +127,15 @@ def has_no_status(branch):
with open(args.template_file, 'r') as fd:
template_str = fd.read()

branches = request_as_dict(gl_url + '/repository/branches')
branches = map(add_timestamp, branches)
branches = filter(is_recent, branches)
branches = filter(has_no_status, branches)

# Select the arg.max most least recent branches
branches = request_as_list(gl_url + '/repository/branches')
branches = [add_timestamp(branch) for branch in branches]
branches = [b for b in branches if is_recent(b)]
branches = sorted(branches, key=lambda x: x['dt'])
branches = itertools.islice(branches, args.max)

# Skip running (and return true) has_no_status after returning True args.max times.
# We need this not to hog the Github Rest API draconian ratelimit.
run_n_times = skip_after_n_successes(default_value=False, n=args.max)
branches = [b for b in branches if run_n_times(has_no_status, b)]
vicentebolea marked this conversation as resolved.
Show resolved Hide resolved
vicentebolea marked this conversation as resolved.
Show resolved Hide resolved

for branch in branches:
print(template_str.format(
Expand Down
Loading