Skip to content

Commit

Permalink
Add util.get_yyyymmdd_from_title
Browse files Browse the repository at this point in the history
  • Loading branch information
kwk committed Apr 22, 2024
1 parent d2f894a commit 5dd67aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
18 changes: 4 additions & 14 deletions snapshot_manager/snapshot_manager/snapshot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,19 @@ def retest(
logging.info(
f"Getting issue with number {issue_number} from repo {self.config.github_repo}"
)
issue = repo.get_issue(issue_number)
issue = repo.get_issue(number=issue_number)
if issue is None:
return
logging.info(f"Got issue: {issue.html_url}")

# Get YYYYMMDD from issue.title
issue_datetime: datetime = None
year_month_day = re.search("([0-9]{4})([0-9]{2})([0-9]{2})", issue.title)
if year_month_day is None:
logging.info("This comment doesn't appear to be a snapshot issue")
return

y = year_month_day.group(1)
m = year_month_day.group(2)
d = year_month_day.group(3)
try:
issue_datetime = datetime.date(year=y, month=m, day=d)
yyyymmdd = util.get_yyyymmdd_from_title(issue.title)
except ValueError as ex:
logging.info(
f"Is this really a snapshot issue. Invalid date found in issue title: {issue.title}: {ex}"
f"issue title doesn't appear to look like a snapshot issue: {issue.title}: {ex}"
)
return
yyyymmdd = issue_datetime.strftime("%Y%m%d")

# Get strategy from issue
strategy: str = None
Expand Down Expand Up @@ -159,7 +149,7 @@ def retest(
issue.edit(body=new_comment_body)

# Kick off a new workflow run and pass the exact date in YYYYMMDD
# (issue_datetime) form because we don't know if the issue was for today
# form because we don't know if the issue was for today
# or some other day.
workflow = repo.get_workflow("check-snapshots")
inputs = {"strategy": strategy, "yyyymmdd": yyyymmdd}
Expand Down
48 changes: 45 additions & 3 deletions snapshot_manager/snapshot_manager/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
util
"""

import enum
import logging
import pathlib
import shlex
import subprocess
import os
import re
import string
import datetime

import requests
import regex

import snapshot_manager.file_access as file_access
import snapshot_manager.build_status as build_status


def fenced_code_block(
Expand Down Expand Up @@ -170,6 +168,50 @@ def golden_file_path(basename: str, extension: str = ".golden.txt") -> pathlib.P
return pathlib.Path(path)


def get_yyyymmdd_from_title(title: str) -> str:
"""Returns the year-month-day combination in YYYYMMDD form from
an issue `title` or raises an error.
Args:
title (str): The title of a github issue
Raises:
ValueError: If `title` doesn't contain proper YYYYMMDD string
ValueError: If the date in the title is invalid
Returns:
str: The year-month-day extracted from `title`
Examples:
>>> get_yyyymmdd_from_title("Foo 20240124 Bar")
'20240124'
>>> get_yyyymmdd_from_title("Foo 20240132 Bar")
Traceback (most recent call last):
...
ValueError: invalid date found in issue title: Foo 20240132 Bar
>>> get_yyyymmdd_from_title("Foo")
Traceback (most recent call last):
...
ValueError: title doesn't appear to reference a snapshot issue: Foo
"""
issue_datetime: datetime = None
year_month_day = re.search("([0-9]{4})([0-9]{2})([0-9]{2})", title)
if year_month_day is None:
raise ValueError(f"title doesn't appear to reference a snapshot issue: {title}")

y = int(year_month_day.group(1))
m = int(year_month_day.group(2))
d = int(year_month_day.group(3))
try:
issue_datetime = datetime.date(year=y, month=m, day=d)
except ValueError as ex:
raise ValueError(f"invalid date found in issue title: {title}") from ex
return issue_datetime.strftime("%Y%m%d")


def expect_chroot(chroot: str) -> str:
"""Raises an exception if given string is not a chroot
Expand Down

0 comments on commit 5dd67aa

Please sign in to comment.