33import subprocess
44import requests
55
6+ def check_gh_auth ():
7+ """Check if GitHub CLI is properly authenticated."""
8+ try :
9+ result = subprocess .run (["gh" , "auth" , "status" ], capture_output = True , text = True )
10+ if result .returncode != 0 :
11+ return False , result .stderr
12+ return True , None
13+ except FileNotFoundError :
14+ return False , "GitHub CLI (gh) is not installed. Please install it first."
15+ except Exception as e :
16+ return False , f"Error checking authentication: { e } "
17+
18+ def handle_gh_error (error_output ):
19+ """Handle GitHub CLI errors and provide helpful messages."""
20+ if "Not Found (HTTP 404)" in error_output :
21+ return "Repository not found or access denied. Please check:\n " \
22+ "1. The repository name is correct\n " \
23+ "2. You have access to the repository\n " \
24+ "3. Your GitHub CLI authentication is valid"
25+ elif "Bad credentials" in error_output or "invalid" in error_output .lower ():
26+ return "Authentication failed. Please run 'gh auth login' to re-authenticate."
27+ elif "rate limit" in error_output .lower ():
28+ return "GitHub API rate limit exceeded. Please try again later."
29+ else :
30+ return f"GitHub API error: { error_output } "
31+
632def main ():
733 if len (sys .argv ) != 4 :
834 print ("Usage: ./push_repo_release_tag.py <repo> <branch> <version_tag>" )
@@ -14,6 +40,13 @@ def main():
1440 print (f"Error: Branch '{ branch } ' is not 'master' or 'main'." )
1541 sys .exit (1 )
1642
43+ # Check GitHub CLI authentication first
44+ auth_ok , auth_error = check_gh_auth ()
45+ if not auth_ok :
46+ print (f"Authentication error: { auth_error } " )
47+ print ("\n To fix this, run: gh auth login" )
48+ sys .exit (1 )
49+
1750 # Get the `lean-toolchain` file content
1851 lean_toolchain_url = f"https://raw.githubusercontent.com/{ repo } /{ branch } /lean-toolchain"
1952 try :
@@ -43,12 +76,23 @@ def main():
4376 for tag in existing_tags :
4477 print (tag .replace ("refs/tags/" , "" ))
4578 sys .exit (1 )
79+ elif list_tags_output .returncode != 0 :
80+ # Handle API errors when listing tags
81+ error_msg = handle_gh_error (list_tags_output .stderr )
82+ print (f"Error checking existing tags: { error_msg } " )
83+ sys .exit (1 )
4684
4785 # Get the SHA of the branch
4886 get_sha_cmd = [
4987 "gh" , "api" , f"repos/{ repo } /git/ref/heads/{ branch } " , "--jq" , ".object.sha"
5088 ]
51- sha_result = subprocess .run (get_sha_cmd , capture_output = True , text = True , check = True )
89+ sha_result = subprocess .run (get_sha_cmd , capture_output = True , text = True )
90+
91+ if sha_result .returncode != 0 :
92+ error_msg = handle_gh_error (sha_result .stderr )
93+ print (f"Error getting branch SHA: { error_msg } " )
94+ sys .exit (1 )
95+
5296 sha = sha_result .stdout .strip ()
5397
5498 # Create the tag
@@ -58,11 +102,20 @@ def main():
58102 "-F" , f"ref=refs/tags/{ version_tag } " ,
59103 "-F" , f"sha={ sha } "
60104 ]
61- subprocess .run (create_tag_cmd , capture_output = True , text = True , check = True )
105+ create_result = subprocess .run (create_tag_cmd , capture_output = True , text = True )
106+
107+ if create_result .returncode != 0 :
108+ error_msg = handle_gh_error (create_result .stderr )
109+ print (f"Error creating tag: { error_msg } " )
110+ sys .exit (1 )
62111
63112 print (f"Successfully created and pushed tag '{ version_tag } ' to { repo } ." )
64113 except subprocess .CalledProcessError as e :
65- print (f"Error while creating/pushing tag: { e .stderr .strip () if e .stderr else e } " )
114+ error_msg = handle_gh_error (e .stderr .strip () if e .stderr else str (e ))
115+ print (f"Error while creating/pushing tag: { error_msg } " )
116+ sys .exit (1 )
117+ except Exception as e :
118+ print (f"Unexpected error: { e } " )
66119 sys .exit (1 )
67120
68121if __name__ == "__main__" :
0 commit comments