fetch-contributors 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/python
  2. '''
  3. A script to fetch recent contributors to borgmatic, used during documentation generation.
  4. '''
  5. import datetime
  6. import itertools
  7. import operator
  8. import subprocess
  9. import requests
  10. def list_merged_pulls(url):
  11. '''
  12. Given a Gitea or GitHub API endpoint URL for pull requests, fetch and return the corresponding
  13. JSON for all such merged pull requests.
  14. '''
  15. response = requests.get(f'{url}?state=closed', headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
  16. if not response.ok:
  17. response.raise_for_status()
  18. return tuple(pull for pull in response.json() if pull.get('merged_at'))
  19. API_ENDPOINT_URLS = (
  20. 'https://projects.torsion.org/api/v1/repos/borgmatic-collective/borgmatic/pulls',
  21. 'https://api.github.com/repos/borgmatic-collective/borgmatic/pulls',
  22. )
  23. RECENT_CONTRIBUTORS_CUTOFF_DAYS = 365
  24. def print_contributors():
  25. '''
  26. Display the recent contributors as a row of avatars in an HTML fragment.
  27. '''
  28. pulls = tuple(itertools.chain.from_iterable(list_merged_pulls(url) for url in API_ENDPOINT_URLS))
  29. seen_user_ids = set()
  30. print('<p>')
  31. for pull in sorted(pulls, key=operator.itemgetter('merged_at'), reverse=True):
  32. merged_at = pull.get('merged_at')
  33. user = pull.get('user')
  34. if not merged_at or not user:
  35. continue
  36. user_id = user.get('id')
  37. if not user_id or user_id in seen_user_ids:
  38. continue
  39. if datetime.datetime.fromisoformat(merged_at) < datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=RECENT_CONTRIBUTORS_CUTOFF_DAYS):
  40. continue
  41. seen_user_ids.add(user_id)
  42. print(
  43. f'''<a href="{user.get('html_url')}?tab=activity"><img src="{user.get('avatar_url')}" width="50" height="50" title="{user.get('full_name') or user.get('login')}" /></a>'''
  44. )
  45. print('</p>')
  46. if __name__ == '__main__':
  47. print_contributors()