accept_upstream.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. http = requests.Session()
  13. if 'GH_TOKEN' in os.environ:
  14. print('Token loaded')
  15. http.headers['Authorization'] = f'token {os.environ["GH_TOKEN"]}'
  16. def copy_comment():
  17. # Get commit SHA
  18. sha = check_output(shlex.split('git rev-parse --short HEAD')).decode().strip()
  19. # Copy comment to clipboard
  20. comment = f"""
  21. Thank you for your contribution!
  22. This PR is [merged into hyfetch](https://github.com/hykilpikonna/hyfetch/commit/{sha}) since this repo (dylanaraps/neofetch) seems no longer maintained.
  23. [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.
  24. Read the ["Running Updated Original Neofetch" section](https://github.com/hykilpikonna/hyfetch#running-updated-original-neofetch) for more info!
  25. """
  26. pyperclip.copy(comment.strip())
  27. print()
  28. print('Done!')
  29. print('Comment response copied to clipboard.')
  30. if __name__ == '__main__':
  31. parser = argparse.ArgumentParser(description='Helper for accepting upstream pull requests')
  32. parser.add_argument('pull', type=int, help='Pull request number')
  33. args = parser.parse_args()
  34. pr = args.pull
  35. print(f'Accepting pull request {pr}...')
  36. # Fetch original pr's information
  37. info = http.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}').json()
  38. user = info['user']['login']
  39. # Fetch commit information
  40. commits = http.get(f'https://api.github.com/repos/{upstream}/pulls/{pr}/commits').json()
  41. author = commits[0]['commit']['author']
  42. # Create commit message
  43. title = info["title"].replace('"', '\\"')
  44. msg = (f'-m "[PR] {upstream}#{pr} from {user} - {title}" '
  45. f'-m "Upstream PR: https://github.com/{upstream}/pull/{pr} \n'
  46. f'Thanks to @{user}\n\n'
  47. f'Co-authored-by: {author["name"]} <{author["email"]}>"')
  48. # head could be null, if the pr repo is deleted
  49. if info['head'] is None or info['head']['repo'] is None:
  50. print(f'Original repo is deleted. Please manually merge.')
  51. input('Press any key to continue when the changes are made...')
  52. # Commit with merge
  53. print()
  54. print('Committing merge...')
  55. os.system(f'git commit -a {msg}')
  56. # Automatically merge
  57. else:
  58. head = info['head']['repo']['full_name']
  59. head_br = info['head']['ref']
  60. head_lbl = info['head']['label']
  61. print()
  62. print('Original Pull Request Info:')
  63. print('> State:', info['state'])
  64. print('> Title:', info['title'])
  65. print('> User:', user)
  66. print('> Created:', info['created_at'])
  67. print('> Head:', head, head_br, head_lbl)
  68. # Fetch head branch
  69. print()
  70. print('Fetching head branch...')
  71. os.system(f'git fetch https://github.com/{head} {head_br}')
  72. # Merge head branch
  73. print()
  74. print('Merging fetch_head...')
  75. os.system(f'git merge FETCH_HEAD --no-ff --no-edit {msg}')
  76. # Push
  77. print()
  78. assert input('Push? [Enter/N]') == ""
  79. os.system('git push')
  80. copy_comment()