parse-changelog.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. import re
  3. from argparse import ArgumentParser
  4. from collections import defaultdict
  5. from _pkg import Const as C
  6. from _pkg import Utils as U
  7. import github # type: ignore # pip install PyGithub
  8. def generate_msg_from_repo(repo_name, tag_name):
  9. """Generate changelog messages from repository and tag name.
  10. Envs:
  11. GITHUB_HOST: the custom github host.
  12. GITHUB_TOKEN: the github access token.
  13. Args:
  14. repo_name (str): The repository name
  15. tag_name (str): the tag name
  16. """
  17. hostname = os.getenv("GITHUB_HOST") or C.hostname
  18. token = os.getenv("GITHUB_TOKEN")
  19. desc_mapping = defaultdict(list)
  20. gh = github.Github(token, base_url=f"https://{hostname}")
  21. repo = gh.get_repo(repo_name)
  22. milestone = U.find_milestone(repo, tag_name)
  23. for issue in repo.get_issues(state="closed", milestone=milestone): # type: ignore
  24. # REF https://pygithub.readthedocs.io/en/latest/github_objects/Issue.html#github.Issue.Issue
  25. desc_mapping[U.get_issue_first_label(issue, C.docmap_siyuan)].append(
  26. {"title": issue.title, "url": issue.html_url}
  27. )
  28. U.generate_msg(desc_mapping, C.docmap_siyuan)
  29. if __name__ == "__main__":
  30. parser = ArgumentParser(
  31. description="Automaticly generate information from issues by tag."
  32. )
  33. parser.add_argument("-t", "--tag", help="the tag to filter issues.")
  34. parser.add_argument("repo", help="The repository name")
  35. args = parser.parse_args()
  36. try:
  37. generate_msg_from_repo(args.repo, args.tag)
  38. except AssertionError:
  39. print(args.tag)