2022-06-01 09:21:53 +08:00
import os
import re
2022-05-30 15:29:21 +08:00
from argparse import ArgumentParser
2022-06-01 09:21:53 +08:00
from collections import defaultdict
2022-05-30 15:29:21 +08:00
2023-04-14 16:06:15 +08:00
import github # pip install PyGithub
# ensure the milestone is open before run this
2022-06-01 09:21:53 +08:00
docmap = {
2023-03-29 19:54:20 +08:00
" Feature " : " Feature " ,
" Enhancement " : " Enhancement " ,
2023-03-30 14:18:52 +08:00
" Bug " : " Bugfix " ,
2023-03-29 19:54:20 +08:00
" Document " : " Document " ,
" Refactor " : " Refactor " ,
" Abolishment " : " Abolishment " ,
" Development " : " Development " ,
2022-06-01 09:21:53 +08:00
}
2022-05-30 15:29:21 +08:00
2023-03-29 08:44:33 +08:00
def generate_msg_from_repo ( repo_name , tag_name , lastestRelease ) :
2022-06-01 09:21:53 +08:00
""" Generate changelog messages from repository and tag name.
2022-05-30 15:29:21 +08:00
2022-06-01 09:21:53 +08:00
Envs :
GITHUB_HOST : the custom github host .
GITHUB_TOKEN : the github access token .
Args :
repo_name ( str ) : The repository name
tag_name ( str ) : the tag name
"""
hostname = os . getenv ( " GITHUB_HOST " ) or " api.github.com "
token = os . getenv ( " GITHUB_TOKEN " )
desc_mapping = defaultdict ( list )
gh = github . Github ( token , base_url = f " https:// { hostname } " )
repo = gh . get_repo ( repo_name )
2023-03-29 08:44:33 +08:00
milestone = find_milestone ( repo , tag_name , lastestRelease )
2022-06-01 09:21:53 +08:00
2023-04-14 16:06:15 +08:00
for issue in repo . get_issues ( state = " closed " , milestone = milestone ) : # type: ignore
2022-11-17 15:51:54 +08:00
# REF https://pygithub.readthedocs.io/en/latest/github_objects/Issue.html#github.Issue.Issue
2022-06-01 09:21:53 +08:00
desc_mapping [ get_issue_first_label ( issue ) ] . append (
2022-11-17 15:51:54 +08:00
{ " title " : issue . title , " url " : issue . html_url }
2022-06-01 09:21:53 +08:00
)
generate_msg ( desc_mapping )
2023-03-29 08:44:33 +08:00
def find_milestone ( repo , title , lastestRelease ) :
2022-06-01 09:21:53 +08:00
""" Find the milestone in a repository that is similar to milestone title
Args :
repo ( github . repository . Repository ) : The repository to search
title ( str ) : the title to match
2022-05-30 15:29:21 +08:00
2022-06-01 09:21:53 +08:00
Returns :
The milestone which title matches the given argument .
If no milestone matches , it will return None
"""
pat = re . search ( " v([0-9.]+) " , title )
2023-03-29 08:44:33 +08:00
thisRelease = title . split ( " / " ) [ - 1 ]
2022-06-01 09:21:53 +08:00
if not pat :
return None
version = pat . group ( 1 )
2023-03-29 08:44:33 +08:00
print ( f '''
2023-05-22 09:12:44 +08:00
< p >
2023-05-25 09:53:16 +08:00
< a href = " https://github.com/siyuan-note/siyuan/actions/workflows/cd.yml " > < img src = " https://img.shields.io/github/actions/workflow/status/siyuan-note/siyuan/cd.yml?event=push&label=cd.yml % 20Action&logo=github " style = " cursor:pointer;height: 30px;margin: 3px auto; " / > < / a >
2023-03-29 08:44:33 +08:00
< 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 >
< 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; " / >
< / p >
''' )
2022-06-01 09:21:53 +08:00
for milestone in repo . get_milestones ( ) :
if version in milestone . title :
return milestone
def get_issue_first_label ( issue ) :
""" Get the first label from issue, if no labels, return empty string. """
for label in issue . get_labels ( ) :
if label . name in docmap :
return label . name
return " "
def generate_msg ( desc_mapping ) :
""" Print changelogs from direction. """
print ( )
for header in docmap :
if not desc_mapping [ header ] :
continue
print ( f " ### { docmap [ header ] } \n " )
for item in desc_mapping [ header ] :
print ( f " * [ { item [ ' title ' ] } ]( { item [ ' url ' ] } ) " )
print ( )
if __name__ == " __main__ " :
parser = ArgumentParser (
description = " Automaticly generate information from issues by tag. "
)
parser . add_argument ( " -t " , " --tag " , help = " the tag to filter issues. " )
2023-03-29 08:44:33 +08:00
parser . add_argument ( " -b " , " --lastestRelease " , help = " lastest Release " )
2022-06-01 09:21:53 +08:00
parser . add_argument ( " repo " , help = " The repository name " )
args = parser . parse_args ( )
2022-05-30 15:29:21 +08:00
2022-06-01 09:21:53 +08:00
try :
2023-03-29 08:44:33 +08:00
generate_msg_from_repo ( args . repo , args . tag , args . lastestRelease )
2022-06-01 09:21:53 +08:00
except AssertionError :
print ( args . tag )