parse-changelog.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. import re
  3. from argparse import ArgumentParser
  4. from collections import defaultdict
  5. import github
  6. docmap = {
  7. "Feature": "Feature",
  8. "Enhancement": "Enhancement",
  9. "Bug": "Bug",
  10. "Document": "Document",
  11. "Refactor": "Refactor",
  12. "Abolishment": "Abolishment",
  13. "Development": "Development",
  14. }
  15. def generate_msg_from_repo(repo_name, tag_name, lastestRelease):
  16. """Generate changelog messages from repository and tag name.
  17. Envs:
  18. GITHUB_HOST: the custom github host.
  19. GITHUB_TOKEN: the github access token.
  20. Args:
  21. repo_name (str): The repository name
  22. tag_name (str): the tag name
  23. """
  24. hostname = os.getenv("GITHUB_HOST") or "api.github.com"
  25. token = os.getenv("GITHUB_TOKEN")
  26. desc_mapping = defaultdict(list)
  27. gh = github.Github(token, base_url=f"https://{hostname}")
  28. repo = gh.get_repo(repo_name)
  29. milestone = find_milestone(repo, tag_name, lastestRelease)
  30. for issue in repo.get_issues(state="closed", milestone=milestone):
  31. # REF https://pygithub.readthedocs.io/en/latest/github_objects/Issue.html#github.Issue.Issue
  32. desc_mapping[get_issue_first_label(issue)].append(
  33. {"title": issue.title, "url": issue.html_url}
  34. )
  35. generate_msg(desc_mapping)
  36. def find_milestone(repo, title, lastestRelease):
  37. """Find the milestone in a repository that is similar to milestone title
  38. Args:
  39. repo (github.repository.Repository): The repository to search
  40. title (str): the title to match
  41. Returns:
  42. The milestone which title matches the given argument.
  43. If no milestone matches, it will return None
  44. """
  45. pat = re.search("v([0-9.]+)", title)
  46. thisRelease = title.split("/")[-1]
  47. if not pat:
  48. return None
  49. version = pat.group(1)
  50. print(f'''
  51. ---
  52. <p align="center">
  53. <a href="https://github.com/siyuan-note/siyuan/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/siyuan-note/siyuan/ci.yml?event=push&label=ci.yml%20Action&logo=github" style="cursor:pointer;height: 30px;margin: 3px auto;"/></a>
  54. <a href="https://github.com/siyuan-note/siyuan/releases/{thisRelease}/"><img src="https://img.shields.io/github/downloads/siyuan-note/siyuan/{thisRelease}/total?logo=github" style="cursor:pointer;height: 30px;margin: 3px auto;"/></a>
  55. <img alt="GitHub commits difference between two branches/tags/commits" src="https://img.shields.io/github/commits-difference/siyuan-note/siyuan?base={lastestRelease}&head={thisRelease}&logo=git" style="cursor:pointer;height: 30px;margin: 3px auto;"/>
  56. </p>
  57. ---
  58. ''')
  59. for milestone in repo.get_milestones():
  60. if version in milestone.title:
  61. return milestone
  62. def get_issue_first_label(issue):
  63. """Get the first label from issue, if no labels, return empty string."""
  64. for label in issue.get_labels():
  65. if label.name in docmap:
  66. return label.name
  67. return ""
  68. def generate_msg(desc_mapping):
  69. """Print changelogs from direction."""
  70. print()
  71. for header in docmap:
  72. if not desc_mapping[header]:
  73. continue
  74. print(f"### {docmap[header]}\n")
  75. for item in desc_mapping[header]:
  76. print(f"* [{item['title']}]({item['url']})")
  77. print()
  78. if __name__ == "__main__":
  79. parser = ArgumentParser(
  80. description="Automaticly generate information from issues by tag."
  81. )
  82. parser.add_argument("-t", "--tag", help="the tag to filter issues.")
  83. parser.add_argument("-b", "--lastestRelease", help="lastest Release")
  84. parser.add_argument("repo", help="The repository name")
  85. args = parser.parse_args()
  86. try:
  87. generate_msg_from_repo(args.repo, args.tag, args.lastestRelease)
  88. except AssertionError:
  89. print(args.tag)