hyfetch/tools/accept_upstream.py

104 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import argparse
import json
import os
2022-07-30 14:08:16 -06:00
import shlex
from subprocess import check_output
2022-07-30 14:08:16 -06:00
import pyperclip
import requests
from github import Github
upstream = 'dylanaraps/neofetch'
my_fork = 'hykilpikonna/hyfetch'
my_base = 'master'
2022-08-11 18:55:19 -06:00
http = requests.Session()
if 'GH_TOKEN' in os.environ:
print('Token loaded')
http.headers['Authorization'] = f'token {os.environ["GH_TOKEN"]}'
2022-08-11 18:55:19 -06:00
def copy_comment():
# Get commit SHA
sha = check_output(shlex.split('git rev-parse --short HEAD')).decode().strip()
# Copy comment to clipboard
comment = f"""
Thank you for your contribution!
This PR is [merged into hyfetch](https://github.com/hykilpikonna/hyfetch/commit/{sha}) since this repo (dylanaraps/neofetch) seems no longer maintained.
[HyFetch](https://github.com/hykilpikonna/hyfetch) is a fork of neofetch with LGBTQ pride flags, but the repo also maintains an updated version of the original neofetch, addressing many pull requests that are not merged in the original repo.
Read the ["Running Updated Original Neofetch" section](https://github.com/hykilpikonna/hyfetch#running-updated-original-neofetch) for more info!
"""
pyperclip.copy(comment.strip())
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 = http.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}').json()
user = info['user']['login']
2022-07-30 14:14:25 -06:00
# Fetch commit information
commits = http.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}/commits').json()
author = json.loads(os.environ.get("override_author")) or commits[-1]['commit']['author']
2022-07-30 14:14:25 -06:00
2022-08-11 18:55:19 -06:00
# Create commit message
2022-07-30 16:13:43 -06:00
title = info["title"].replace('"', '\\"')
2022-08-11 18:55:19 -06:00
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"]}>"')
2022-08-11 18:55:19 -06:00
# 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...')
2022-07-30 14:39:01 -06:00
2022-08-11 18:55:19 -06:00
# Commit with merge
print()
print('Committing merge...')
os.system(f'git commit -a {msg}')
2022-07-30 16:04:21 -06:00
2022-08-11 18:55:19 -06:00
# 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)
2022-07-30 14:08:16 -06:00
2022-08-11 18:55:19 -06:00
# Fetch head branch
print()
print('Fetching head branch...')
os.system(f'git fetch https://github.com/{head} {head_br}')
2022-07-30 14:08:16 -06:00
2022-08-11 18:55:19 -06:00
# Merge head branch
print()
print('Merging fetch_head...')
os.system(f'git merge FETCH_HEAD --no-ff --no-edit {msg}')
2022-07-31 11:18:23 -06:00
2022-08-11 18:55:19 -06:00
# Push
2022-07-30 14:08:16 -06:00
print()
2022-11-15 21:37:09 -07:00
assert input('Push? [Enter/N]') == ""
2022-08-11 18:55:19 -06:00
os.system('git push')
copy_comment()