Skip to content

Commit 009f698

Browse files
authored
Update sync.py
1 parent e2dfdac commit 009f698

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

google-sheet-sync/sync.py

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,25 @@
44
from datetime import datetime
55
from google.oauth2.service_account import Credentials
66
from googleapiclient.discovery import build
7-
from github import Github
7+
from github import Github, GithubException
88

99
def authenticate_google(credentials_file):
10+
"""
11+
Authenticate and create a Google Sheets API client.
12+
13+
This function reads a Google service account credentials file to authenticate
14+
and build a client for accessing the Google Sheets API.
15+
Args:
16+
credentials_file (str): The path to the Google service account JSON credentials file.
17+
GOOGLE_CREDENTIALS stored securely as GitHub Secret on the mc2-centerbot account
18+
19+
Returns:
20+
googleapiclient.discovery.Resource: An authenticated client object for interacting
21+
with the Google Sheets API.
22+
23+
Raises:
24+
google.auth.exceptions.GoogleAuthError: If authentication fails due to invalid or missing credentials.
25+
"""
1026
creds = Credentials.from_service_account_file(credentials_file, scopes=["https://www.googleapis.com/auth/spreadsheets.readonly"])
1127
return build('sheets', 'v4', credentials=creds)
1228

@@ -62,24 +78,41 @@ def main():
6278
# Ensure branch exists
6379
try:
6480
repo.get_branch(branch_name)
65-
except Exception:
81+
except GithubException as e:
82+
# Check if the error is due to a non-existent branch
83+
if e.status == 404:
6684
main_branch = repo.get_branch("main")
6785
repo.create_git_ref(ref=f"refs/heads/{branch_name}", sha=main_branch.commit.sha)
86+
else:
87+
raise
6888

69-
# Commit changes to the branch
89+
# Commit changes to the branch
90+
try:
7091
try:
71-
contents = None
72-
try:
73-
contents = repo.get_contents(file_path, ref=branch_name)
74-
except Exception:
75-
pass
76-
if contents:
77-
repo.update_file(file_path, "Update CSV file from Google Sheet", csv_content, contents.sha, branch=branch_name)
92+
# Attempt to get the file contents
93+
contents = repo.get_contents(file_path, ref=branch_name)
94+
# Update the file if it exists
95+
repo.update_file(
96+
file_path,
97+
"Update CSV file from Google Sheet",
98+
csv_content,
99+
contents.sha,
100+
branch=branch_name
101+
)
102+
except GithubException as e:
103+
# If the file doesn't exist (404), create it
104+
if e.status == 404:
105+
repo.create_file(
106+
file_path,
107+
"Create CSV file from Google Sheet",
108+
csv_content,
109+
branch=branch_name
110+
)
78111
else:
79-
repo.create_file(file_path, "Create CSV file from Google Sheet", csv_content, branch=branch_name)
80-
except Exception as e:
81-
print(f"Error committing changes: {e}")
82-
112+
raise # Re-raise other exceptions
113+
except Exception as e:
114+
print(f"Error committing changes: {e}")
115+
83116
# Create or reuse PR
84117
try:
85118
open_prs = repo.get_pulls(state="open", head=f"{repo.owner.login}:{branch_name}")
@@ -94,4 +127,4 @@ def main():
94127
print(f"Error creating PR: {e}")
95128

96129
if __name__ == "__main__":
97-
main()
130+
main()

0 commit comments

Comments
 (0)