1010import sys
1111from collections import OrderedDict
1212from json import loads
13- from subprocess import CalledProcessError
14- from subprocess import PIPE
15- from subprocess import run
13+ from subprocess import PIPE , CalledProcessError , run
1614from tempfile import TemporaryDirectory
1715
1816import dateutil .parser
2523from .cache import _cache_data
2624from .graphql import GitHubGraphQlQuery
2725
28-
2926# The tags and description to use in creating subsets of PRs
3027TAGS_METADATA_BASE = OrderedDict (
3128 [
@@ -175,7 +172,9 @@ def get_activity(
175172 if auth is None :
176173 # Attempt to use the gh cli if installed
177174 try :
178- p = run (["gh" , "auth" , "token" ], text = True , capture_output = True )
175+ p = run (
176+ ["gh" , "auth" , "token" ], text = True , capture_output = True , check = False
177+ )
179178 auth = p .stdout .strip ()
180179 except CalledProcessError :
181180 print (
@@ -270,7 +269,7 @@ def generate_all_activity_md(
270269 include_opened = False ,
271270 strip_brackets = False ,
272271 branch = None ,
273- ignored_contributors : list [str ] = None ,
272+ ignored_contributors : list [str ] | None = None ,
274273):
275274 """Generate a full markdown changelog of GitHub activity of a repo based on release tags.
276275
@@ -316,10 +315,12 @@ def generate_all_activity_md(
316315 # Get the sha and tag name for each tag in the target repo
317316 with TemporaryDirectory () as td :
318317 subprocess .run (
319- shlex .split (f"git clone https://github.com/{ target } repo" ), cwd = td
318+ shlex .split (f"git clone https://github.com/{ target } repo" ),
319+ cwd = td ,
320+ check = False ,
320321 )
321322 repo = os .path .join (td , "repo" )
322- subprocess .run (shlex .split ("git fetch origin --tags" ), cwd = repo )
323+ subprocess .run (shlex .split ("git fetch origin --tags" ), cwd = repo , check = False )
323324
324325 cmd = 'git log --tags --simplify-by-decoration --pretty="format:%h | %D"'
325326 data = (
@@ -402,8 +403,7 @@ def add(self, contributor):
402403 def __iter__ (self ):
403404 if self .author :
404405 yield self .author
405- for item in sorted (self .other - {self .author }):
406- yield item
406+ yield from sorted (self .other - {self .author })
407407
408408
409409def generate_activity_md (
@@ -418,7 +418,7 @@ def generate_activity_md(
418418 strip_brackets = False ,
419419 heading_level = 1 ,
420420 branch = None ,
421- ignored_contributors : list [str ] = None ,
421+ ignored_contributors : list [str ] | None = None ,
422422):
423423 """Generate a markdown changelog of GitHub activity within a date window.
424424
@@ -536,12 +536,10 @@ def ignored_user(username):
536536 return True
537537
538538 # Check against user-specified ignored contributors
539- if ignored_contributors and any (
540- fnmatch .fnmatch (username , user ) for user in ignored_contributors
541- ):
542- return True
543-
544- return False
539+ return bool (
540+ ignored_contributors
541+ and any (fnmatch .fnmatch (username , user ) for user in ignored_contributors )
542+ )
545543
546544 def filter_ignored (userlist ):
547545 return {user for user in userlist if not ignored_user (user )}
@@ -617,7 +615,7 @@ def filter_ignored(userlist):
617615 comment_contributors = comment_contributor_counts [
618616 comment_contributor_counts >= comment_others_cutoff
619617 ].index .tolist ()
620- all_contributors |= set ( c for c in comment_contributors if isinstance (c , str ))
618+ all_contributors |= { c for c in comment_contributors if isinstance (c , str )}
621619
622620 closed_mask , opened_mask = _activity_window_masks (
623621 data , data .since_dt_str , data .until_dt_str , data .since_is_git_ref
@@ -646,7 +644,7 @@ def filter_ignored(userlist):
646644 # Add any contributors to a merged PR to our contributors list
647645 # Filter out NaN values and non-strings
648646 pr_contributors = closed_prs ["contributors" ].explode ().unique ().tolist ()
649- all_contributors |= set ( c for c in pr_contributors if isinstance (c , str ))
647+ all_contributors |= { c for c in pr_contributors if isinstance (c , str )}
650648
651649 # Define categories for a few labels
652650 if tags is None :
@@ -660,7 +658,7 @@ def filter_ignored(userlist):
660658 tags_metadata = {key : val for key , val in TAGS_METADATA_BASE .items () if key in tags }
661659
662660 # Initialize our tags with empty metadata
663- for key , vals in tags_metadata .items ():
661+ for vals in tags_metadata .values ():
664662 vals .update (
665663 {
666664 "mask" : None ,
@@ -673,14 +671,18 @@ def filter_ignored(userlist):
673671 # Track which PRs have already been assigned to prevent duplicates
674672 assigned_prs = set ()
675673
676- for kind , kindmeta in tags_metadata .items ():
674+ for kindmeta in tags_metadata .values ():
677675 # First find the PRs based on tag
678676 mask = closed_prs ["labels" ].map (
679- lambda a : any (ii == jj for ii in kindmeta ["tags" ] for jj in a )
677+ lambda a , kindmeta = kindmeta : any (
678+ ii == jj for ii in kindmeta ["tags" ] for jj in a
679+ )
680680 )
681681 # Now find PRs based on prefix
682682 mask_pre = closed_prs ["title" ].map (
683- lambda title : any (f"{ ipre } :" in title for ipre in kindmeta ["pre" ])
683+ lambda title , kindmeta = kindmeta : any (
684+ f"{ ipre } :" in title for ipre in kindmeta ["pre" ]
685+ )
684686 )
685687 mask = mask | mask_pre
686688
@@ -705,39 +707,39 @@ def filter_ignored(userlist):
705707
706708 # Add some optional kinds of PRs / issues
707709 tags_metadata .update (
708- dict ( others = {"description" : other_description , "md" : [], "data" : others })
710+ { " others" : {"description" : other_description , "md" : [], "data" : others }}
709711 )
710712 if include_issues :
711713 tags_metadata .update (
712- dict (
713- closed_issues = {
714+ {
715+ " closed_issues" : {
714716 "description" : "Closed issues" ,
715717 "md" : [],
716718 "data" : closed_issues ,
717719 }
718- )
720+ }
719721 )
720722 if include_opened :
721723 tags_metadata .update (
722- dict (
723- opened_issues = {
724+ {
725+ " opened_issues" : {
724726 "description" : "Opened issues" ,
725727 "md" : [],
726728 "data" : opened_issues ,
727729 }
728- )
730+ }
729731 )
730732 if include_opened :
731733 tags_metadata .update (
732- dict ( opened_prs = {"description" : "Opened PRs" , "md" : [], "data" : opened_prs })
734+ { " opened_prs" : {"description" : "Opened PRs" , "md" : [], "data" : opened_prs }}
733735 )
734736
735737 # Generate the markdown
736738 prs = tags_metadata
737739
738740 extra_head = "#" * (heading_level - 1 )
739741
740- for kind , items in prs .items ():
742+ for items in prs .values ():
741743 n_orgs = len (items ["data" ]["org" ].unique ())
742744 for org , idata in items ["data" ].groupby ("org" ):
743745 if n_orgs > 1 :
@@ -794,7 +796,7 @@ def filter_ignored(userlist):
794796 "" ,
795797 f"([full changelog]({ changelog_url } ))" ,
796798 ]
797- for kind , info in prs .items ():
799+ for info in prs .values ():
798800 if len (info ["md" ]) > 0 :
799801 md += ["" ]
800802 md .append (f"{ extra_head } ## { info ['description' ]} " )
@@ -943,9 +945,7 @@ def _get_datetime_and_type(org, repo, datetime_or_git_ref, auth):
943945 return (dt , False )
944946 except Exception :
945947 raise ValueError (
946- "{0} not found as a ref or valid date format" .format (
947- datetime_or_git_ref
948- )
948+ f"{ datetime_or_git_ref } not found as a ref or valid date format"
949949 )
950950
951951
@@ -981,7 +981,7 @@ def _get_latest_release_tag(org, repo):
981981 ]
982982 print (f"Auto-detecting latest release tag for: { org } /{ repo } " , file = sys .stderr )
983983 print (f"Running command: { ' ' .join (cmd )} " , file = sys .stderr )
984- out = run (cmd , stdout = PIPE )
984+ out = run (cmd , stdout = PIPE , check = False )
985985 try :
986986 json = out .stdout .decode ()
987987 release_data = loads (json )
@@ -998,6 +998,6 @@ def _get_latest_release_tag(org, repo):
998998 f"Error getting latest release tag for { org } /{ repo } : { e } " , file = sys .stderr
999999 )
10001000 print ("Reverting to using latest local git tag..." , file = sys .stderr )
1001- out = run ("git describe --tags" . split () , stdout = PIPE )
1001+ out = run ([ "git" , " describe" , " --tags"] , stdout = PIPE , check = False )
10021002 tag = out .stdout .decode ().rsplit ("-" , 2 )[0 ]
10031003 return tag
0 commit comments