1+ #!/usr/bin/env python3
12import os
23import frontmatter
4+ import subprocess
35from pathlib import Path
46
57# Define collections and their corresponding TTL layouts
@@ -19,9 +21,8 @@ def extract_translation_key(filepath):
1921 """Extract the translation_key from the front matter of a markdown file."""
2022 try :
2123 with open (filepath , "r" , encoding = "utf-8" ) as f :
22- content = f .read ()
23- post = frontmatter .loads (content )
24- return post .get ("translation_key" )
24+ post = frontmatter .load (f )
25+ return post .get ("translation_key" )
2526 except Exception as e :
2627 print (f"Error reading { filepath } : { str (e )} " )
2728 return None
@@ -71,6 +72,46 @@ def process_collection(collection):
7172 else :
7273 print (f"No translation_key in { filename } " )
7374
75+ def commit_and_push ():
76+ """Commit and push the generated TTL files to Git using a fine-grained PAT."""
77+ try :
78+ os .chdir (BASE_DIR )
79+
80+ # Add TTL files to Git
81+ subprocess .run (["git" , "add" , "_ttl/" ], check = True )
82+
83+ # Commit changes
84+ result = subprocess .run (
85+ ["git" , "commit" , "-m" , "Update TTL files [automated]" ],
86+ check = False ,
87+ capture_output = True ,
88+ text = True
89+ )
90+ if result .returncode != 0 and "nothing to commit" not in result .stderr :
91+ print (f"Error committing: { result .stderr } " )
92+ return
93+
94+ # Push using the fine-grained PAT in the remote URL
95+ pat = os .getenv ("GITHUB_PAT" )
96+ if not pat :
97+ raise ValueError ("GITHUB_PAT environment variable not set." )
98+
99+ remote_url = f"https://oauth2:{ pat } @github.com/nfdi4objects/NFDI4Objects-Website.git"
100+ push_result = subprocess .run (
101+ ["git" , "push" , remote_url ],
102+ check = False ,
103+ capture_output = True ,
104+ text = True
105+ )
106+ if push_result .returncode != 0 :
107+ print (f"Error pushing: { push_result .stderr } " )
108+ else :
109+ print ("Committed and pushed TTL files to Git." )
110+ except ValueError as e :
111+ print (f"Error: { str (e )} " )
112+ except Exception as e :
113+ print (f"Unexpected error: { str (e )} " )
114+
74115def main ():
75116 # Create _ttl directory if it doesn't exist
76117 os .makedirs (TTL_DIR , exist_ok = True )
@@ -80,6 +121,8 @@ def main():
80121 for collection in COLLECTIONS :
81122 process_collection (collection )
82123
124+ # Commit and push the changes
125+ commit_and_push ()
126+
83127if __name__ == "__main__" :
84128 main ()
85-
0 commit comments