Skip to content

Commit

Permalink
Env -> with for better parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
chmielsen committed Jun 26, 2023
1 parent 675f253 commit f903eac
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
uses: actions/[email protected]
- name: Deploy schemas to BigQuery
uses: Atom-Learning/bigquery-upload-action
env:
with:
gcp_project: 'gcp-us-project'
dataset_id: 'dataset-id'
table_id: 'table-id'
Expand Down
10 changes: 5 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

@pytest.fixture
def dataset_id(monkeypatch):
return monkeypatch.setenv("dataset_id", "foo_dataset_id")
return monkeypatch.setenv("INPUT_DATASET_ID", "foo_dataset_id")


@pytest.fixture
def gcp_project(monkeypatch):
return monkeypatch.setenv("gcp_project", "foo_gcp_project")
return monkeypatch.setenv("INPUT_GCP_PROJECT", "foo_gcp_project")


@pytest.fixture
def table_id(monkeypatch):
return monkeypatch.setenv("table_id", "foo_table_id")
return monkeypatch.setenv("INPUT_TABLE_ID", "foo_table_id")


@pytest.fixture
def bq_rows_as_json_path(monkeypatch):
return monkeypatch.setenv("bq_rows_as_json_path", "foo.json")
return monkeypatch.setenv("INPUT_BQ_ROWS_AS_JSON_PATH", "foo.json")


@pytest.fixture
def credentials(monkeypatch):
return monkeypatch.setenv("credentials", "{'secret': 'value'}")
return monkeypatch.setenv("INPUT_CREDENTIALS", "{'secret': 'value'}")
20 changes: 9 additions & 11 deletions plugin_scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def nullthrows(arg: Optional[T]) -> T:


class Config(NamedTuple):
"""
Names of the arguments are env var names read from
"""

gcp_project: str
dataset_id: str
table_id: str
Expand All @@ -23,18 +19,20 @@ class Config(NamedTuple):


def _validate_env_variables() -> None:
for env_var in Config._fields:
for param in Config._fields:
# GH actions prefixes the arguments passed to `with` with `INPUT_` and puts upper case.
env_var = "INPUT_" + param.upper()
if not os.environ.get(env_var):
raise Exception(f"Missing `{env_var}` config")
raise Exception(f"Missing `{param}` config")


def read_config() -> Config:
_validate_env_variables()
gcp_project = os.environ.get("gcp_project")
dataset_id = os.environ.get("dataset_id")
table_id = os.environ.get("table_id")
bq_rows_as_json_path = os.environ.get("bq_rows_as_json_path")
credentials_as_json = os.environ.get("credentials")
gcp_project = os.environ.get("INPUT_GCP_PROJECT")
dataset_id = os.environ.get("INPUT_DATASET_ID")
table_id = os.environ.get("INPUT_TABLE_ID")
bq_rows_as_json_path = os.environ.get("INPUT_BQ_ROWS_AS_JSON_PATH")
credentials_as_json = os.environ.get("INPUT_CREDENTIALS")
credentials = json.loads(nullthrows(credentials_as_json))

return Config(
Expand Down
2 changes: 1 addition & 1 deletion plugin_scripts/requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ google-cloud-core==2.3.1
google-crc32c==1.3.0
google-resumable-media==2.3.3
googleapis-common-protos==1.56.4
grpcio==1.54.2
grpcio==1.56.0
grpcio-status==1.47.0
idna==3.3
packaging==21.3
Expand Down
1 change: 1 addition & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Fixtures imported automatically from conftest.py file


def test__validate_env_variables_missing_dataset_id(
table_id, gcp_project, credentials, bq_rows_as_json_path
):
Expand Down

0 comments on commit f903eac

Please sign in to comment.