Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle multiple timestamp formats #109

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions action/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# standard imports
from datetime import datetime
import io
import json
import os
Expand Down Expand Up @@ -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}"
Expand Down
17 changes: 4 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
10 changes: 10 additions & 0 deletions tests/data/dummy_github_push_alt_timestamp_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"repository": {
"default_branch": "master"
},
"commits": [
{
"timestamp": "2023-07-14T13:17:25-04:00"
}
]
}