Utils.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import re
  2. def find_milestone(repo, title, len=0):
  3. """Find the milestone in a repository that is similar to milestone title
  4. Args:
  5. repo (github.repository.Repository): The repository to search
  6. title (str): the title to match
  7. len: 版本号长度限制,默认 0 不限制
  8. Returns:
  9. The milestone which title matches the given argument.
  10. If no milestone matches, it will return None
  11. """
  12. thisRelease = title.split("/")[-1]
  13. pat = re.search("v([0-9.]+)", thisRelease)
  14. if not pat:
  15. return None
  16. if len > 0:
  17. version = ".".join(pat.group(1).split(".")[:len])
  18. else:
  19. version = ".".join(pat.group(1).split(".")[:])
  20. # REF https://docs.github.com/en/rest/issues/milestones?apiVersion=2022-11-28#list-milestones
  21. for milestone in repo.get_milestones(state="all"):
  22. if version in milestone.title:
  23. return milestone
  24. def generate_msg(desc_mapping, docmap):
  25. """Print changelogs from direction."""
  26. print()
  27. for header in docmap:
  28. if not desc_mapping[header]:
  29. continue
  30. print(f"#### {docmap[header]}\n")
  31. for item in desc_mapping[header]:
  32. print(f"* [{item['title']}]({item['url']})")
  33. print()
  34. def get_issue_first_label(issue, docmap):
  35. """Get the first label from issue, if no labels, return empty string."""
  36. for label in issue.get_labels():
  37. if label.name in docmap:
  38. return label.name
  39. return ""
  40. def generate_header_from_repo(repo_name, tag_name, lastestRelease, action_file, HEADER=''):
  41. thisRelease = tag_name.split("/")[-1]
  42. pat = re.search("v([0-9.]+)", thisRelease)
  43. if not pat:
  44. return None
  45. return f'''
  46. ---
  47. <p align="center">
  48. <a href="https://github.com/{repo_name}/actions/workflows/{action_file}"><img src="https://img.shields.io/github/actions/workflow/status/{repo_name}/{action_file}?logo=github&label={action_file}%20Action" style="cursor:pointer;height: 30px;margin: 3px auto;"/></a>
  49. <a href="https://github.com/{repo_name}/releases/{thisRelease}/"><img src="https://img.shields.io/github/downloads/{repo_name}/{thisRelease}/total?logo=github" style="cursor:pointer;height: 30px;margin: 3px auto;"/></a>
  50. <img alt="GitHub commits difference between two branches/tags/commits" src="https://img.shields.io/github/commits-difference/{repo_name}?base={lastestRelease}&head={thisRelease}&logo=git" style="cursor:pointer;height: 30px;margin: 3px auto;"/>
  51. </p>
  52. {HEADER}'''