1- # WARNING: DO NOT EDIT!
2- #
3- # This file was generated by plugin_template, and is managed by it. Please use
4- # './plugin-template --github pulp_python' to update this file.
5- #
6- # For more info visit https://github.com/pulp/plugin_template
1+ # This file is managed by the plugin template.
2+ # Do not edit.
73
84import os
95import re
106import subprocess
117import sys
128import tomllib
9+ import yaml
1310from pathlib import Path
1411
1512from github import Github
1613
17- with open ("pyproject.toml" , "rb" ) as fp :
18- PYPROJECT_TOML = tomllib .load (fp )
19- KEYWORDS = ["fixes" , "closes" ]
20- BLOCKING_REGEX = [
21- r"^DRAFT" ,
22- r"^WIP" ,
23- r"^NOMERGE" ,
24- r"^DO\s*NOT\s*MERGE" ,
25- r"^EXPERIMENT" ,
26- r"^FIXUP" ,
27- r"^fixup!" , # This is created by 'git commit --fixup'
28- r"Apply suggestions from code review" , # This usually comes from GitHub
29- ]
30- try :
31- CHANGELOG_EXTS = [
32- f".{ item ['directory' ]} " for item in PYPROJECT_TOML ["tool" ]["towncrier" ]["type" ]
33- ]
34- except KeyError :
35- CHANGELOG_EXTS = [".feature" , ".bugfix" , ".doc" , ".removal" , ".misc" ]
36- NOISSUE_MARKER = "[noissue]"
37-
38- sha = sys .argv [1 ]
39- message = subprocess .check_output (["git" , "log" , "--format=%B" , "-n 1" , sha ]).decode ("utf-8" )
40-
41- if NOISSUE_MARKER in message :
42- sys .exit (f"Do not add '{ NOISSUE_MARKER } ' in the commit message." )
43-
44- blocking_matches = [m for m in (re .match (pattern , message ) for pattern in BLOCKING_REGEX ) if m ]
45- if blocking_matches :
46- print ("Found these phrases in the commit message:" )
47- for m in blocking_matches :
48- print (" - " + m .group (0 ))
49- sys .exit ("This PR is not ready for consumption." )
5014
51- g = Github (os .environ .get ("GITHUB_TOKEN" ))
52- repo = g .get_repo ("pulp/pulp_python" )
53-
54-
55- def check_status (issue ):
15+ def check_status (issue , repo , cherry_pick ):
5616 gi = repo .get_issue (int (issue ))
5717 if gi .pull_request :
5818 sys .exit (f"Error: issue #{ issue } is a pull request." )
59- if gi .closed_at :
19+ if gi .closed_at and not cherry_pick :
20+ print ("Make sure to use 'git cherry-pick -x' when backporting a change." )
21+ print (
22+ "If a backport of a change requires a significant amount of rewriting, "
23+ "consider creating a new issue."
24+ )
6025 sys .exit (f"Error: issue #{ issue } is closed." )
6126
6227
63- def check_changelog (issue ):
28+ def check_changelog (issue , CHANGELOG_EXTS ):
6429 matches = list (Path ("CHANGES" ).rglob (f"{ issue } .*" ))
6530
6631 if len (matches ) < 1 :
@@ -70,18 +35,63 @@ def check_changelog(issue):
7035 sys .exit (f"Invalid extension for changelog entry '{ match } '." )
7136
7237
73- print ("Checking commit message for {sha}." .format (sha = sha [0 :7 ]))
38+ def main () -> None :
39+ TEMPLATE_CONFIG = yaml .safe_load (Path ("template_config.yml" ).read_text ())
40+ GITHUB_ORG = TEMPLATE_CONFIG ["github_org" ]
41+ PLUGIN_NAME = TEMPLATE_CONFIG ["plugin_name" ]
42+
43+ with Path ("pyproject.toml" ).open ("rb" ) as _fp :
44+ PYPROJECT_TOML = tomllib .load (_fp )
45+ KEYWORDS = ["fixes" , "closes" ]
46+ BLOCKING_REGEX = [
47+ r"^DRAFT" ,
48+ r"^WIP" ,
49+ r"^NOMERGE" ,
50+ r"^DO\s*NOT\s*MERGE" ,
51+ r"^EXPERIMENT" ,
52+ r"^FIXUP" ,
53+ r"^fixup!" , # This is created by 'git commit --fixup'
54+ r"Apply suggestions from code review" , # This usually comes from GitHub
55+ ]
56+ try :
57+ CHANGELOG_EXTS = [
58+ f".{ item ['directory' ]} " for item in PYPROJECT_TOML ["tool" ]["towncrier" ]["type" ]
59+ ]
60+ except KeyError :
61+ CHANGELOG_EXTS = [".feature" , ".bugfix" , ".doc" , ".removal" , ".misc" ]
62+ NOISSUE_MARKER = "[noissue]"
63+
64+ sha = sys .argv [1 ]
65+ message = subprocess .check_output (["git" , "log" , "--format=%B" , "-n 1" , sha ]).decode ("utf-8" )
66+
67+ if NOISSUE_MARKER in message :
68+ sys .exit (f"Do not add '{ NOISSUE_MARKER } ' in the commit message." )
69+
70+ blocking_matches = [m for m in (re .match (pattern , message ) for pattern in BLOCKING_REGEX ) if m ]
71+ if blocking_matches :
72+ print ("Found these phrases in the commit message:" )
73+ for m in blocking_matches :
74+ print (" - " + m .group (0 ))
75+ sys .exit ("This PR is not ready for consumption." )
76+
77+ g = Github (os .environ .get ("GITHUB_TOKEN" ))
78+ repo = g .get_repo (f"{ GITHUB_ORG } /{ PLUGIN_NAME } " )
79+
80+ print ("Checking commit message for {sha}." .format (sha = sha [0 :7 ]))
81+
82+ # validate the issue attached to the commit
83+ issue_regex = r"(?:{keywords})[\s:]+#(\d+)" .format (keywords = "|" .join (KEYWORDS ))
84+ issues = re .findall (issue_regex , message , re .IGNORECASE )
85+ cherry_pick_regex = r"^\s*\(cherry picked from commit [0-9a-f]*\)\s*$"
86+ cherry_pick = re .search (cherry_pick_regex , message , re .MULTILINE )
87+
88+ if issues :
89+ for issue in issues :
90+ check_status (issue , repo , cherry_pick )
91+ check_changelog (issue , CHANGELOG_EXTS )
7492
75- # validate the issue attached to the commit
76- issue_regex = r"(?:{keywords})[\s:]+#(\d+)" .format (keywords = ("|" ).join (KEYWORDS ))
77- issues = re .findall (issue_regex , message , re .IGNORECASE )
78- cherry_pick_regex = r"^\s*\(cherry picked from commit [0-9a-f]*\)\s*$"
79- cherry_pick = re .search (cherry_pick_regex , message , re .MULTILINE )
93+ print ("Commit message for {sha} passed." .format (sha = sha [0 :7 ]))
8094
81- if issues :
82- for issue in issues :
83- if not cherry_pick :
84- check_status (issue )
85- check_changelog (issue )
8695
87- print ("Commit message for {sha} passed." .format (sha = sha [0 :7 ]))
96+ if __name__ == "__main__" :
97+ main ()
0 commit comments