accept_upstream.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import shlex
  5. from subprocess import check_output
  6. import pyperclip
  7. import requests
  8. from github import Github
  9. upstream = 'dylanaraps/neofetch'
  10. my_fork = 'hykilpikonna/hyfetch'
  11. my_base = 'master'
  12. # gh_token = os.environ['GH_TOKEN']
  13. if __name__ == '__main__':
  14. parser = argparse.ArgumentParser(description='Helper for accepting upstream pull requests')
  15. parser.add_argument('pull', type=int, help='Pull request number')
  16. args = parser.parse_args()
  17. pr = args.pull
  18. print(f'Accepting pull request {pr}...')
  19. # Fetch original pr's information
  20. info = requests.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}').json()
  21. # print(info)
  22. head = info['head']['repo']['full_name']
  23. head_br = info['head']['ref']
  24. head_lbl = info['head']['label']
  25. user = info['user']['login']
  26. print()
  27. print('Original Pull Request Info:')
  28. print('> State:', info['state'])
  29. print('> Title:', info['title'])
  30. print('> User:', user)
  31. print('> Created:', info['created_at'])
  32. print('> Head:', head, head_br, head_lbl)
  33. # Fetch commit information
  34. commits = requests.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}/commits').json()
  35. author = commits[0]['commit']['author']
  36. # Fetch head branch
  37. print()
  38. print('Fetching head branch...')
  39. os.system(f'git fetch https://github.com/{head} {head_br}')
  40. # Merge head branch
  41. print()
  42. print('Merging fetch_head...')
  43. title = info["title"].replace('"', '\\"')
  44. os.system(f'git merge FETCH_HEAD --no-ff --no-edit '
  45. f'-m "[PR] {upstream}#{pr} from {user} - {title}" '
  46. f'-m "Upstream PR: https://github.com/{upstream}/pull/{pr} \n'
  47. f'Thanks to @{user}\n\n'
  48. f'Co-authored-by: {author["name"]} <{author["email"]}>"')
  49. # Push
  50. print()
  51. print('Pushing...')
  52. os.system('git push')
  53. # Get commit SHA
  54. sha = check_output(shlex.split('git rev-parse --short HEAD')).decode().strip()
  55. # Copy comment to clipboard
  56. comment = f"""
  57. Thank you for your contribution!
  58. This PR is [merged into hyfetch](https://github.com/hykilpikonna/hyfetch/commit/{sha}) since this repo (dylanaraps/neofetch) seems no longer maintained.
  59. [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.
  60. Read the ["Running Updated Original Neofetch" section](https://github.com/hykilpikonna/hyfetch#running-updated-original-neofetch) for more info!
  61. """
  62. pyperclip.copy(comment.strip())
  63. print()
  64. print('Done!')
  65. print('Comment response copied to clipboard.')