#!/usr/bin/env python3 import argparse import os from subprocess import check_call, check_output import openai my_base = 'master' openai_token = os.environ['OPENAI_TOKEN'] def gen_merge_msg(commits: str): openai_example_commits = """ 0d422120 - [API] Add a feature to retrieve user photos. 4a64895e - [api] Add maimai2 userPhoto API e271cb45 - [API] Add a feature to retrieve user photos. 0bf54e66 - [API] Let web music list read from database 0913ef20 - [api] Add maimai2 userPhoto API 7cc9fb11 - Revert "New API to return user photos." 9c51b1e0 - Revert "Add API - Get user photos (mai)" ba1f4589 - New API to return user photos. e7848cb9 - Add API - Get user photos (mai) """ openai_example_response = "Add user photos feature for maimai2" complete = openai.Completion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a senior software engineer helping with maintaining a game server repository."}, {"role": "user", "content": f"I just merged the following commits. " f"What commit message can best describe and summarize the changes below?\n\n" f"{openai_example_commits}"}, {"role": "assistant", "content": f'"{openai_example_response}"'}, {"role": "user", "content": f"I just merged the following commits. " f"What commit message can best describe and summarize the changes below?\n\n" f"{commits}"} ] ) return complete.choices[0].text if __name__ == '__main__': parser = argparse.ArgumentParser(description='Helper for merging remote sibling repos') parser.add_argument('url', type=str, help='Remote repo url') parser.add_argument('-b', '--branch', type=str, default='master', help='Branch to merge') parser.add_argument('-m', '--msg', type=str, default=None, help='Merge commit message') args = parser.parse_args() url = args.url print(f'Merging {url}...') # Fetch head branch print() print('Fetching head branch...') check_call(f'git -c http.sslVerify=false fetch {url} {args.branch}', shell=True) # Get the list of commits from the branch commits = check_output(f'git log --pretty=format:"%H - %s" {my_base}..FETCH_HEAD', shell=True).decode() commits = [c.split(' - ', 1) for c in commits.split('\n')] commits = [c for c in commits if len(c) == 2] commits = [f"{sha[:8]} - {msg}" for sha, msg in commits] commits = '\n'.join(commits) print("Commits:") print(commits) # Get the list of authors from the branch authors = check_output(f'git log --pretty=format:"%an <%ae>" {my_base}..FETCH_HEAD', shell=True).decode() authors = set(authors.split('\n')) authors = "\n".join(f"Co-authored-by: {a}" for a in authors) print("\nAuthors:") print(authors) # Create a merge commit message owner = url.split('/')[-2] repo = url.split('/')[-1].split('.')[0] # If there isn't a merge message, create one by asking ChatGPT to summarize the commits if not args.msg: while True: args.msg = input('\nPlease enter a merge message (or leave blank to generate one): ').strip() if args.msg: break args.msg = gen_merge_msg(commits) print(f'Generated message: {args.msg}') if input('Is this okay? [Y/n] ') == 'Y': break msg = f"""Merge {owner}/{repo} : {args.msg} {commits} {authors} """ print(msg) # Merge head branch print('\nMerging fetch_head...') check_call(f'git merge FETCH_HEAD --no-ff --no-edit', shell=True) # Push assert input('\nPush? [Enter/N]') == "" check_call('git push', shell=True)