From 8278b92e12fbabcfab1e8fa136d3641c2827886d Mon Sep 17 00:00:00 2001 From: "Azalea (on HyDEV-Daisy)" Date: Thu, 11 Aug 2022 20:55:19 -0400 Subject: [PATCH] [O] Manually commit for deleted PR --- tools/accept_upstream.py | 113 ++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/tools/accept_upstream.py b/tools/accept_upstream.py index 575f3c35..e1b6e7ce 100755 --- a/tools/accept_upstream.py +++ b/tools/accept_upstream.py @@ -11,56 +11,9 @@ from github import Github upstream = 'dylanaraps/neofetch' my_fork = 'hykilpikonna/hyfetch' my_base = 'master' -# gh_token = os.environ['GH_TOKEN'] -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Helper for accepting upstream pull requests') - parser.add_argument('pull', type=int, help='Pull request number') - args = parser.parse_args() - pr = args.pull - - print(f'Accepting pull request {pr}...') - - # Fetch original pr's information - info = requests.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}').json() - # print(info) - head = info['head']['repo']['full_name'] - head_br = info['head']['ref'] - head_lbl = info['head']['label'] - user = info['user']['login'] - print() - print('Original Pull Request Info:') - print('> State:', info['state']) - print('> Title:', info['title']) - print('> User:', user) - print('> Created:', info['created_at']) - print('> Head:', head, head_br, head_lbl) - - # Fetch commit information - commits = requests.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}/commits').json() - author = commits[0]['commit']['author'] - - # Fetch head branch - print() - print('Fetching head branch...') - os.system(f'git fetch https://github.com/{head} {head_br}') - - # Merge head branch - print() - print('Merging fetch_head...') - title = info["title"].replace('"', '\\"') - os.system(f'git merge FETCH_HEAD --no-ff --no-edit ' - f'-m "[PR] {upstream}#{pr} from {user} - {title}" ' - f'-m "Upstream PR: https://github.com/{upstream}/pull/{pr} \n' - f'Thanks to @{user}\n\n' - f'Co-authored-by: {author["name"]} <{author["email"]}>"') - - # Push - print() - print('Pushing...') - os.system('git push') - +def copy_comment(): # Get commit SHA sha = check_output(shlex.split('git rev-parse --short HEAD')).decode().strip() @@ -78,3 +31,67 @@ Read the ["Running Updated Original Neofetch" section](https://github.com/hykilp print() print('Done!') print('Comment response copied to clipboard.') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Helper for accepting upstream pull requests') + parser.add_argument('pull', type=int, help='Pull request number') + args = parser.parse_args() + pr = args.pull + + print(f'Accepting pull request {pr}...') + + # Fetch original pr's information + info = requests.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}').json() + user = info['user']['login'] + + # Fetch commit information + commits = requests.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}/commits').json() + author = commits[0]['commit']['author'] + + # Create commit message + title = info["title"].replace('"', '\\"') + msg = (f'-m "[PR] {upstream}#{pr} from {user} - {title}" ' + f'-m "Upstream PR: https://github.com/{upstream}/pull/{pr} \n' + f'Thanks to @{user}\n\n' + f'Co-authored-by: {author["name"]} <{author["email"]}>"') + + # head could be null, if the pr repo is deleted + if info['head'] is None or info['head']['repo'] is None: + print(f'Original repo is deleted. Please manually merge.') + input('Press any key to continue when the changes are made...') + + # Commit with merge + print() + print('Committing merge...') + os.system(f'git commit -a {msg}') + + # Automatically merge + else: + head = info['head']['repo']['full_name'] + head_br = info['head']['ref'] + head_lbl = info['head']['label'] + print() + print('Original Pull Request Info:') + print('> State:', info['state']) + print('> Title:', info['title']) + print('> User:', user) + print('> Created:', info['created_at']) + print('> Head:', head, head_br, head_lbl) + + # Fetch head branch + print() + print('Fetching head branch...') + os.system(f'git fetch https://github.com/{head} {head_br}') + + # Merge head branch + print() + print('Merging fetch_head...') + os.system(f'git merge FETCH_HEAD --no-ff --no-edit {msg}') + + # Push + print() + print('Pushing...') + os.system('git push') + + copy_comment()