''' Usage: $ python ss13_autochangelog.py [--dry-run] html/changelogs_ch [PR_number] [PR_author] [PR_Body] ss13_autochangelog.py - Generate changelog YAML files from pull request. ''' from __future__ import print_function import os, sys, re, time, argparse from datetime import datetime, date from time import time today = date.today() dateformat = "%d %B %Y" opt = argparse.ArgumentParser() opt.add_argument('target_dir', help='The location to write changelog files to.') opt.add_argument('pr_numb', help='The number of the pull request.') opt.add_argument('pr_author', help='The author of the pull request. If specific authors aren\'t specified, the PR author will be used') opt.add_argument('pr_body', help='Name of a .txt file with the pr body to scan') args = opt.parse_args() all_changelog_entries = {} validPrefixes = { "fix": 'bugfix', "fixed": 'bugfix', "fixes": 'bugfix', "bugfix": 'bugfix', "wip": 'wip', "qol": 'qol', "soundadd": 'soundadd', "sounddel": 'sounddel', "add": 'rscadd', "added": 'rscadd', "adds": 'rscadd', "rscadd": 'rscadd', "del": 'rscdel', "dels": 'rscdel', "delete": 'rscdel', "deletes": 'rscdel', "rscdeldel": 'rscdel', "imageadd": 'imageadd', "imagedel": 'imagedel', "maptweak": 'maptweak', "remap": 'maptweak', "remaps": 'maptweak', "typo": 'spellcheck', "spellcheck": 'spellcheck', "experimental": 'experiment', "experiments": 'experiment', "experiment": 'experiment', "balance": 'balance', "code_imp": 'code_imp', "refactor": 'refactor', "config": 'config', "admin": 'admin', "server": 'server', } incltag = False new_logs = {} author = args.pr_author new = 0 # Parse PR body for changelog entries print('Reading changelogs...') f = open(args.pr_body, "r") pr_body = f.read() f.close() print('Reading changelogs...') for line in pr_body.splitlines(): line = line.replace('"','\\"') #line = line.replace("'","''") print(f"Checking line '{line}'") if line[:1] == "🆑" or line[:4] == ":cl:": # Find the start of the changelog print("Found opening :cl: tag") if incltag == True: # If we're already reading logs, skip continue incltag = True # Fetch the author name if line[:1] == "🆑": author = line[1:] elif line[:4] == ":cl:": author = line[4:] author.strip() if not len(author): author = args.pr_author if author not in new_logs: new_logs[author] = [] # Make array entry for the author continue # If we hit a /cl, we're no longer reading logs elif line == "/🆑" or line == "/:cl:": print("Found closing /:cl: tag") incltag = False # If we aren't reading logs, we don't care about any other line contents if not incltag: continue # Split line into tag (icon) and body (comment) body = re.split("[ ,:-]", line, 1) if len(body) != 2: continue # If there's just one word, then it can't really be a changelog, now can it if body[0] in validPrefixes: tag = validPrefixes[body[0]] body = body[1].strip(" ,-:\t\n") else: # If the tag is invalid, just default to rscadd tag = "rscadd" body = line.strip(" ,-:\t\n") new_logs[author].append(f" - {tag}: \"{body}\"") new += 1 print(f"Writing {new} new changelog entries.") # f supposedly formats new into the var for auth in new_logs: # Sanitize authors without changes if not len(new_logs[auth]): continue f = open(os.path.join(args.target_dir, f"{auth}{args.pr_numb}.yml"), 'w') print(f"Writing changes to {f}") f.write(f'author: {auth}\n') f.write('delete-after: True\n') f.write('changes:\n') for log in new_logs[auth]: f.write(f'{log}\n') f.close()