Skip to content

Commit 7256b04

Browse files
committed
need to clone the repo first
1 parent 8d61ce7 commit 7256b04

File tree

6 files changed

+91
-8
lines changed

6 files changed

+91
-8
lines changed

.github/workflows/python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ jobs:
1414
- name: Lint
1515
run: |
1616
pip install flake8
17-
flake8 src/
17+
flake8 --ignore E501 src/

action.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ name: "script.google.com project sync action"
22
description: "script.google.com project sync action"
33
author: "Thai Tran"
44
inputs:
5+
GH_TOKEN:
6+
description: "GitHub Token"
7+
required: true
8+
9+
REPO_NAME:
10+
description: "Repository Name"
11+
required: true
12+
513
PROJECT_ID:
614
description: "ProjectId"
715
required: true

requirements.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Google API modules:
12
google-api-python-client
23
google-auth-httplib2
3-
google-auth-oauthlib
4+
google-auth-oauthlib
5+
6+
# GitHub integration modules:
7+
PyGithub~=1.58
8+
GitPython~=3.1

src/main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,35 @@
77

88
import os.path
99
import json
10-
from manager_environment import EnvironmentManager as ENV
1110

1211
# from google.auth.transport.requests import Request
1312
from google.oauth2.credentials import Credentials
1413
# from google_auth_oauthlib.flow import InstalledAppFlow
1514
from googleapiclient import errors
1615
from googleapiclient.discovery import build
1716

17+
18+
from manager_environment import EnvironmentManager as ENV
19+
from manager_github import GitHubManager
20+
1821
# If modifying these scopes, delete the file token.json.
1922
SCOPES = ['https://www.googleapis.com/auth/script.projects']
2023

2124
MANIFEST = {
22-
"timeZone": f"{ENV.TIMEZONE}",
23-
"exceptionLogging": "CLOUD"
25+
"timeZone": f"{ENV.TIMEZONE}",
26+
"exceptionLogging": "CLOUD"
2427
}
2528

29+
2630
def main():
2731
"""Calls the Apps Script API.
2832
"""
33+
GitHubManager.prepare_github_env()
2934
creds = None
3035
# The file token.json stores the user's access and refresh tokens, and is
3136
# created automatically when the authorization flow completes for the first
3237
# time.
33-
token ={
38+
token = {
3439
'refresh_token': ENV.REFRESH_TOKEN,
3540
"token_uri": "https://oauth2.googleapis.com/token",
3641
"client_id": ENV.CLIENT_ID,
@@ -52,7 +57,7 @@ def main():
5257
}]
5358
}
5459
# loop through gs files in webflow directory
55-
for file in os.listdir(ENV.PROJECT_PATH):
60+
for file in os.listdir(f'repo/{ENV.PROJECT_PATH}'):
5661
if file.endswith('.gs'):
5762
with open(os.path.join('.', file), 'r') as f:
5863
request['files'].append({
@@ -65,5 +70,6 @@ def main():
6570
# The API encountered a problem.
6671
print(error.content)
6772

73+
6874
if __name__ == '__main__':
6975
main()

src/manager_environment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class EnvironmentManager:
99

1010
_TRUTHY = ["true", "1", "t", "y", "yes"]
1111

12-
# Google Script
12+
GH_TOKEN = getenv("INPUT_GH_TOKEN")
13+
REPO_NAME = getenv("INPUT_REPO_NAME")
14+
# Google Script
1315
PROJECT_ID = environ["INPUT_PROJECT_ID"]
1416
PROJECT_NAME = environ["INPUT_PROJECT_NAME"]
1517
PROJECT_PATH = environ["INPUT_PROJECT_PATH"]

src/manager_github.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from os import makedirs
2+
from os.path import dirname, join
3+
from shutil import copy, rmtree
4+
5+
from git import Repo
6+
from github import Github, AuthenticatedUser, Repository
7+
8+
from manager_environment import EnvironmentManager as EM
9+
10+
11+
class GitHubManager:
12+
USER: AuthenticatedUser
13+
REPO: Repo
14+
REMOTE: Repository
15+
16+
_REMOTE_NAME: str
17+
_REMOTE_PATH: str
18+
_SINGLE_COMMIT_BRANCH = "latest_branch"
19+
20+
@staticmethod
21+
def prepare_github_env(cls):
22+
"""
23+
Download and store for future use:
24+
- Current GitHub user.
25+
- Named repo of the user [username]/[username].
26+
- Clone of the named repo.
27+
"""
28+
github = Github(EM.GH_TOKEN)
29+
clone_path = "repo"
30+
cls.USER = github.get_user()
31+
rmtree(clone_path, ignore_errors=True)
32+
33+
cls._REMOTE_NAME = f"{cls.USER.login}/{EM.REPO_NAME}"
34+
cls._REPO_PATH = f"https://{EM.GH_TOKEN}@github.com/{cls._REMOTE_NAME}.git"
35+
36+
cls.REMOTE = github.get_repo(cls._REMOTE_NAME)
37+
cls.REPO = Repo.clone_from(cls._REPO_PATH, to_path=clone_path)
38+
39+
@staticmethod
40+
def branch(cls, requested_branch: str) -> str:
41+
"""
42+
Gets requested branch name or the default branch name if requested branch wasn't found.
43+
The default branch name is regularly, 'main' or 'master'.
44+
45+
:param requested_branch: Requested branch name.
46+
:returns: Commit author.
47+
"""
48+
return cls.REMOTE.default_branch if requested_branch == "" else requested_branch
49+
50+
@staticmethod
51+
def _copy_file_and_add_to_repo(src_path: str):
52+
"""
53+
Copies file to repository folder, creating path if needed and adds file to git.
54+
The copied file relative to repository root path will be equal the source file
55+
relative to work directory path.
56+
57+
:param src_path: Source file path.
58+
"""
59+
dst_path = join(GitHubManager.REPO.working_tree_dir, src_path)
60+
makedirs(dirname(dst_path), exist_ok=True)
61+
copy(src_path, dst_path)
62+
GitHubManager.REPO.git.add(dst_path)

0 commit comments

Comments
 (0)