Skip to content

Commit b49d27b

Browse files
committed
PLaying around with github secrents and connections.
1 parent eb3f098 commit b49d27b

File tree

5 files changed

+105
-17
lines changed

5 files changed

+105
-17
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# .github/workflows/ci.yml (excerpt)
2+
jobs:
3+
tests:
4+
runs-on: ubuntu-latest
5+
steps:
6+
- uses: actions/checkout@v4
7+
8+
- uses: actions/setup-python@v5
9+
with:
10+
python-version: '3.10'
11+
12+
- name: Install deps
13+
run: pip install -r requirements.txt
14+
15+
- name: Expose Google-Drive secrets
16+
run: |
17+
# 1) materialise the JSON file
18+
echo "${{ secrets.GDRIVE_SERVICE_ACCOUNT_JSON }}" > $HOME/gdrive.json
19+
# 2) make both paths available to the test process
20+
echo "GDRIVE_SERVICE_ACCOUNT_FILE=$HOME/gdrive.json" >> $GITHUB_ENV
21+
echo "GDRIVE_ROOT_FOLDER_ID=${{ secrets.GDRIVE_ROOT_FOLDER_ID }}" >> $GITHUB_ENV
22+
- name: Run pytest
23+
env:
24+
CI: "true" # so your skip-marker sees CI=true
25+
run: pytest -q -k test_gdrive_connection

datashuttle/datashuttle_class.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -928,17 +928,20 @@ def setup_google_drive_connection(self) -> None:
928928
browser_available = gdrive.ask_user_for_browser(log=True)
929929

930930
if not browser_available:
931-
config_token = gdrive.prompt_and_get_config_token(
932-
self.cfg,
933-
gdrive_client_secret,
934-
self.cfg.get_rclone_config_name("gdrive"),
935-
log=True,
936-
)
931+
service_account_filepath = utils.get_user_input(
932+
"Please input the path to your credentials json"
933+
) # TODO: add more explanation
934+
# config_token = gdrive.prompt_and_get_config_token(
935+
# self.cfg,
936+
# gdrive_client_secret,
937+
# self.cfg.get_rclone_config_name("gdrive"),
938+
# log=True,
939+
# )
937940
else:
938-
config_token = None
941+
service_account_filepath = None
939942

940943
self._setup_rclone_gdrive_config(
941-
gdrive_client_secret, config_token, log=True
944+
gdrive_client_secret, service_account_filepath, log=True
942945
)
943946

944947
rclone.check_successful_connection_and_raise_error_on_fail(self.cfg)
@@ -1565,14 +1568,14 @@ def _setup_rclone_central_local_filesystem_config(self) -> None:
15651568
def _setup_rclone_gdrive_config(
15661569
self,
15671570
gdrive_client_secret: str | None,
1568-
config_token: str | None,
1571+
service_account_filepath: str | None,
15691572
log: bool,
15701573
) -> None:
15711574
rclone.setup_rclone_config_for_gdrive(
15721575
self.cfg,
15731576
self.cfg.get_rclone_config_name("gdrive"),
15741577
gdrive_client_secret,
1575-
config_token,
1578+
service_account_filepath,
15761579
log=log,
15771580
)
15781581

datashuttle/utils/folders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def search_gdrive_or_aws_for_folders(
575575

576576
if output.returncode != 0:
577577
utils.log_and_message(
578-
f"Error searching files at {search_path.as_posix()} \n {output.stderr.decode('utf-8') if output.stderr else ""}"
578+
f"Error searching files at {search_path.as_posix()} \n {output.stderr.decode('utf-8') if output.stderr else ''}"
579579
)
580580
return all_folder_names, all_filenames
581581

datashuttle/utils/rclone.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def setup_rclone_config_for_gdrive(
160160
cfg: Configs,
161161
rclone_config_name: str,
162162
gdrive_client_secret: str | None,
163-
config_token: Optional[str] = None,
163+
service_account_filepath: Optional[str] = None,
164164
log: bool = True,
165165
):
166166
"""
@@ -197,10 +197,16 @@ def setup_rclone_config_for_gdrive(
197197
else ""
198198
)
199199

200-
extra_args = (
201-
f"config_is_local=false config_token={config_token}"
202-
if config_token
203-
else ""
200+
# extra_args = (
201+
# f"config_is_local=false config_token={config_token}"
202+
# if config_token
203+
# else ""
204+
# )
205+
206+
service_account_filepath_arg = (
207+
""
208+
if service_account_filepath is None
209+
else f"service_account_file {service_account_filepath} "
204210
)
205211
output = call_rclone(
206212
f"config create "
@@ -210,7 +216,8 @@ def setup_rclone_config_for_gdrive(
210216
f"{client_secret_key_value}"
211217
f"scope drive "
212218
f"root_folder_id {cfg['gdrive_root_folder_id']} "
213-
f"{extra_args}",
219+
f"{service_account_filepath_arg}",
220+
# f"{extra_args}",
214221
pipe_std=True,
215222
)
216223

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
from pathlib import Path
3+
4+
import pytest
5+
import test_utils
6+
from base import BaseTest
7+
8+
from datashuttle.configs.canonical_configs import get_broad_datatypes
9+
10+
11+
@pytest.mark.skipif(os.getenv("CI") is None, reason="Only runs in CI")
12+
class TestGoogleDriveGithubCI(BaseTest):
13+
14+
def test_google_drive_connection(self, project):
15+
# ── 1 pull the secrets from env ───────────────────────────────
16+
root_id = os.environ["GDRIVE_ROOT_FOLDER_ID"]
17+
sa_path = os.environ["GDRIVE_SERVICE_ACCOUNT_FILE"]
18+
19+
# ── 2 configure the project (no hard-coded ids/paths) ─────────
20+
project.update_config_file(
21+
local_path=str(Path.home() / "data"), # any temp location
22+
connection_method="gdrive",
23+
central_path="testGDrive",
24+
gdrive_root_folder_id=root_id,
25+
gdrive_client_id=None, # keep None
26+
)
27+
28+
# ── 3 feed the SA-file path to the interactive prompt ─────────
29+
state = {"first": True}
30+
31+
def mock_input(_: str) -> str:
32+
if state["first"]:
33+
state["first"] = False
34+
return "n" # ← tells setup to use file, not auth-browser
35+
return sa_path # ← absolute path written in the workflow
36+
37+
import builtins
38+
39+
original_input = builtins.input
40+
builtins.input = mock_input
41+
42+
try:
43+
project.setup_google_drive_connection()
44+
finally:
45+
builtins.input = original_input # always restore
46+
47+
# ── 4 run the usual checks ────────────────────────────────────
48+
subs, sessions = test_utils.get_default_sub_sessions_to_test()
49+
test_utils.make_and_check_local_project_folders(
50+
project, "rawdata", subs, sessions, get_broad_datatypes()
51+
)
52+
53+
project.upload_entire_project()

0 commit comments

Comments
 (0)