diff --git a/action/main.py b/action/main.py index f5faf65..ae3f92c 100644 --- a/action/main.py +++ b/action/main.py @@ -1,4 +1,5 @@ # standard imports +from datetime import datetime import io import json import os @@ -209,16 +210,17 @@ def get_push_event_details() -> dict: commit_timestamp = github_event["commits"][0]['timestamp'] # use regex and convert created at to yyyy.m.d-hhmmss + # GitHub can provide timestamps in different formats, ensure we handle them all using `fromisoformat` # timestamp: "2023-01-25T10:43:35Z" - match = re.search(r'(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{2}):(\d{2})Z', commit_timestamp) - - # assume a match, and raise exception if not found - year = int(match.group(1)) - month = match.group(2).zfill(2) - day = match.group(3).zfill(2) - hour = match.group(4).zfill(2) - minute = match.group(5).zfill(2) - second = match.group(6).zfill(2) + # timestamp "2024-07-14T13:17:25-04:00" + timestamp = datetime.fromisoformat(commit_timestamp) + year = timestamp.year + month = str(timestamp.month).zfill(2) + day = str(timestamp.day).zfill(2) + hour = str(timestamp.hour).zfill(2) + minute = str(timestamp.minute).zfill(2) + second = str(timestamp.second).zfill(2) + if os.getenv('INPUT_DOTNET', 'false').lower() == 'true': # dotnet versioning build = f"{hour}{minute}" diff --git a/tests/conftest.py b/tests/conftest.py index d94a8d0..4af6969 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -116,20 +116,11 @@ def dummy_github_push_event_path_invalid_commits(): os.environ['GITHUB_EVENT_PATH'] = original_value -@pytest.fixture(scope='function', params=[True, False]) -def github_event_path(request, dummy_github_pr_event_path, dummy_github_push_event_path): - # true is PR event - # false is push event - +@pytest.fixture(scope='function', params=['pr', 'push', 'push_alt_timestamp']) +def github_event_path(request): original_value = os.getenv('GITHUB_EVENT_PATH', os.path.join(DATA_DIRECTORY, 'dummy_github_event.json')) - - if request.param: - os.environ['GITHUB_EVENT_PATH'] = os.path.join(DATA_DIRECTORY, 'dummy_github_pr_event.json') - yield - else: - os.environ['GITHUB_EVENT_PATH'] = os.path.join(DATA_DIRECTORY, 'dummy_github_push_event.json') - yield - + os.environ['GITHUB_EVENT_PATH'] = os.path.join(DATA_DIRECTORY, f'dummy_github_{request.param}_event.json') + yield os.environ['GITHUB_EVENT_PATH'] = original_value diff --git a/tests/data/dummy_github_push_alt_timestamp_event.json b/tests/data/dummy_github_push_alt_timestamp_event.json new file mode 100644 index 0000000..7eeaccc --- /dev/null +++ b/tests/data/dummy_github_push_alt_timestamp_event.json @@ -0,0 +1,10 @@ +{ + "repository": { + "default_branch": "master" + }, + "commits": [ + { + "timestamp": "2023-07-14T13:17:25-04:00" + } + ] +}