|
import json |
|
import re |
|
|
|
from invoke task |
|
|
|
|
|
def get_change_log(ctx, num: int=None): |
|
"""Get the change log with sha, title, and filenames.""" |
|
raw_result = ctx.run( |
|
"git log --pretty=format:'%n%h%n%s' --name-only {}".format( |
|
"" if num is None else "-{}".format(num + 1) |
|
), |
|
hide=True, |
|
).stdout.strip() |
|
result = [] |
|
cur_build = {} |
|
|
|
for line in raw_result.splitlines(): |
|
if not line and cur_build: |
|
result.append(cur_build.copy()) |
|
cur_build.clear() |
|
if line: |
|
if 'files' not in cur_build: |
|
cur_build['files'] = [] |
|
|
|
if 'sha' not in cur_build: |
|
cur_build['sha'] = line |
|
elif 'title' not in cur_build: |
|
cur_build['title'] = line |
|
else: |
|
cur_build['files'].append(line) |
|
|
|
return result |
|
|
|
def get_files_changes_since_pr_n_back(ctx, pr_max): |
|
"""Get a list of all files changed since last n PR(s)""" |
|
changes = get_change_log(ctx, 100) |
|
files = set() |
|
pr_count = 0 |
|
|
|
for change in changes: |
|
files.update(change['files']) |
|
if re.match(r"Merge pull request #\d+", change['title']): |
|
pr_count += 1 |
|
if pr_count >= pr_max: |
|
break |
|
|
|
return list(files) |
|
|
|
|
|
@task() |
|
def change_log(ctx): |
|
"""Output the change log in json""" |
|
print(json.dumps(get_change_log(ctx, 100))) |
|
|
|
|
|
@task() |
|
def file_changes_since_last_merged_pr(ctx): |
|
"""List of all files changed since last PR or two""" |
|
branch = ctx.run( |
|
"git rev-parse --abbrev-ref HEAD", |
|
hide=True, |
|
).stdout.strip() |
|
files = get_files_changes_since_pr_n_back( |
|
ctx, |
|
2 if branch == 'master' else 1 |
|
) |
|
print(json.dumps(list(files))) |
|
|
|
|
|
@task() |
|
def folders_changes_since_last_merged_pr(ctx): |
|
"""List of all files changed since last PR or two""" |
|
branch = ctx.run( |
|
"git rev-parse --abbrev-ref HEAD", |
|
hide=True, |
|
).stdout.strip() |
|
files = get_files_changes_since_pr_n_back( |
|
ctx, |
|
2 if branch == 'master' else 1 |
|
) |
|
folders = set() |
|
for file in files: |
|
if '/' in file: |
|
folders.add(file.split('/', 2)[0]) |
|
print(json.dumps(list(folders))) |