From e61dc31b05a802e429a0fe48a88d9468aa913680 Mon Sep 17 00:00:00 2001 From: Frank Lee Date: Tue, 21 Jun 2022 12:22:53 +0800 Subject: [PATCH] [ci] added scripts to auto-generate release post text (#1142) * [ci] added scripts to auto-generate release post text * polish code --- .../scripts/generate_release_draft.py | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 .github/workflows/scripts/generate_release_draft.py diff --git a/.github/workflows/scripts/generate_release_draft.py b/.github/workflows/scripts/generate_release_draft.py new file mode 100644 index 000000000..5c7153e7e --- /dev/null +++ b/.github/workflows/scripts/generate_release_draft.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python +# coding: utf-8 + +import argparse +import requests +import re +import os + +COMMIT_API = 'https://api.github.com/repos/hpcaitech/ColossalAI/commits' +TAGS_API = 'https://api.github.com/repos/hpcaitech/ColossalAI/tags' + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--out', type=str, help='output path for the release draft', required=True) + return parser.parse_args() + + +def get_latest_tag_commit(headers=None): + res = requests.get(url=TAGS_API, headers=headers) + data = res.json() + commit_hash = data[0]['commit']['sha'] + version = data[0]['name'] + return commit_hash, version + + +def get_commit_info(commit_hash, headers=None): + api = f'{COMMIT_API}/{commit_hash}' + res = requests.get(url=api, headers=headers) + return res.json() + + +def get_all_commit_info(since, headers=None): + page = 1 + results = [] + + while True: + api = f'{COMMIT_API}?since={since}&per_page=100&page={page}' + resp = requests.get(url=api, headers=headers) + data = resp.json() + + # exit when no more data + if len(data) == 0: + break + + results.extend(data) + page += 1 + + return results + + +def collate_release_info(commit_info_list): + results = dict() + pattern = pattern = r'\[.*\]' + + for commit_info in commit_info_list: + author = commit_info['commit']['author']['name'] + author_url = commit_info['author']['url'] + msg = commit_info['commit']['message'] + match = re.search(pattern, msg) + + if match: + tag = match.group().lstrip('[').rstrip(']').capitalize() + if tag not in results: + results[tag] = [] + results[tag].append((msg, author, author_url)) + + return results + + +def generate_release_post_markdown(current_version, last_version, release_info): + text = [] + + # add highlights + highlights = "## What's Changed \n\n" + text.append(highlights) + + # add items + for k, v in release_info.items(): + topic = f"### {k} \n" + text.append(topic) + + for msg, author, author_url in v: + # only keep the first line + msg = msg.split('\n')[0] + + item = f'{msg} by [{author}]({author_url})\n' + text.append(f'- {item}') + + text.append('\n') + + # add full change log + text.append( + f'**Full Changelog**: https://github.com/hpcaitech/ColossalAI/compare/{current_version}...{last_version}') + + return text + + +if __name__ == '__main__': + token = os.environ['GITHUB_API_TOKEN'] + headers = {'Authorization': token} + + # get previous release tag + last_release_commit, last_version = get_latest_tag_commit(headers) + last_release_commit_info = get_commit_info(last_release_commit, headers=headers) + last_release_date = last_release_commit_info['commit']['author']['date'] + + # get the commits since last release + commit_info = get_all_commit_info(since=last_release_date, headers=headers) + commit_info = commit_info[:-1] # remove the release commit + + # collate into markdown + release_info = collate_release_info(commit_info) + markdown_text = generate_release_post_markdown('v0.1.7', last_version, release_info) + + # write into a file + args = parse_args() + with open(args.out, 'w') as f: + for line in markdown_text: + f.write(line)