|
1 | 1 | # standard imports |
2 | | -from datetime import datetime |
| 2 | +import datetime |
3 | 3 | import io |
4 | 4 | import json |
5 | 5 | import os |
|
28 | 28 | GITHUB_HEADERS = {'Authorization': f'token {GITHUB_API_TOKEN}'} |
29 | 29 |
|
30 | 30 |
|
| 31 | +class TimestampUTC: |
| 32 | + """ |
| 33 | + Timestamp class to handle the timestamp conversion. |
| 34 | +
|
| 35 | + Attributes |
| 36 | + ---------- |
| 37 | + timestamp : datetime.datetime |
| 38 | + The timestamp in datetime format. |
| 39 | + year : int |
| 40 | + The year of the timestamp. |
| 41 | + month : str |
| 42 | + The month of the timestamp, zero-padded. |
| 43 | + day : str |
| 44 | + The day of the timestamp, zero-padded. |
| 45 | + hour : str |
| 46 | + The hour of the timestamp, zero-padded. |
| 47 | + minute : str |
| 48 | + The minute of the timestamp, zero-padded. |
| 49 | + second : str |
| 50 | + The second of the timestamp, zero-padded. |
| 51 | + """ |
| 52 | + def __init__(self, iso_timestamp: str): |
| 53 | + # use datetime.datetime and convert created at to yyyy.m.d-hhmmss |
| 54 | + # GitHub can provide timestamps in different formats, ensure we handle them all using `fromisoformat` |
| 55 | + # timestamp: "2023-01-25T10:43:35Z" |
| 56 | + # timestamp "2024-07-14T13:17:25-04:00" |
| 57 | + self.timestamp = datetime.datetime.fromisoformat(iso_timestamp).astimezone(datetime.timezone.utc) |
| 58 | + self.year = self.timestamp.year |
| 59 | + self.month = str(self.timestamp.month).zfill(2) |
| 60 | + self.day = str(self.timestamp.day).zfill(2) |
| 61 | + self.hour = str(self.timestamp.hour).zfill(2) |
| 62 | + self.minute = str(self.timestamp.minute).zfill(2) |
| 63 | + self.second = str(self.timestamp.second).zfill(2) |
| 64 | + |
| 65 | + def __repr__(self): |
| 66 | + class_name = type(self).__name__ |
| 67 | + return f"{class_name}(y{self.year}.m{self.month}.d{self.day}.h{self.hour}.m{self.minute}.s{self.second})" |
| 68 | + |
| 69 | + def __str__(self): |
| 70 | + return f"{self.year=}, {self.month=}, {self.day=}, {self.hour=}, {self.minute=}, {self.second=}" |
| 71 | + |
| 72 | + |
31 | 73 | def append_github_step_summary(message: str): |
32 | 74 | """ |
33 | 75 | Append a message to the GitHub Status Summary. |
@@ -212,27 +254,17 @@ def get_push_event_details() -> dict: |
212 | 254 | # get the commit |
213 | 255 | commit_timestamp = github_event["commits"][0]['timestamp'] |
214 | 256 |
|
215 | | - # use regex and convert created at to yyyy.m.d-hhmmss |
216 | | - # GitHub can provide timestamps in different formats, ensure we handle them all using `fromisoformat` |
217 | | - # timestamp: "2023-01-25T10:43:35Z" |
218 | | - # timestamp "2024-07-14T13:17:25-04:00" |
219 | | - timestamp = datetime.fromisoformat(commit_timestamp) |
220 | | - year = timestamp.year |
221 | | - month = str(timestamp.month).zfill(2) |
222 | | - day = str(timestamp.day).zfill(2) |
223 | | - hour = str(timestamp.hour).zfill(2) |
224 | | - minute = str(timestamp.minute).zfill(2) |
225 | | - second = str(timestamp.second).zfill(2) |
| 257 | + ts = TimestampUTC(iso_timestamp=commit_timestamp) |
226 | 258 |
|
227 | 259 | if os.getenv('INPUT_DOTNET', 'false').lower() == 'true': |
228 | 260 | # dotnet versioning |
229 | | - build = f"{hour}{minute}" |
230 | | - revision = second |
231 | | - release_version = f"{year}.{int(month)}{day}.{int(build)}.{int(revision)}" |
| 261 | + build = f"{ts.hour}{ts.minute}" |
| 262 | + revision = ts.second |
| 263 | + release_version = f"{ts.year}.{int(ts.month)}{ts.day}.{int(build)}.{int(revision)}" |
232 | 264 | else: |
233 | 265 | # default versioning |
234 | | - build = f"{hour}{minute}{second}" |
235 | | - release_version = f"{year}.{int(month)}{day}.{int(build)}" |
| 266 | + build = f"{ts.hour}{ts.minute}{ts.second}" |
| 267 | + release_version = f"{ts.year}.{int(ts.month)}{ts.day}.{int(build)}" |
236 | 268 |
|
237 | 269 | push_event_details['release_version'] = release_version |
238 | 270 | return push_event_details |
|
0 commit comments