Skip to content

Commit

Permalink
Add workflow step to ensure parties are in the past
Browse files Browse the repository at this point in the history
  • Loading branch information
homeworkprod committed Jun 5, 2024
1 parent 795d662 commit ddbba01
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ jobs:
run: |
shopt -s globstar
taplo lint --schema "file://"$PWD"/schemas/party.json" data/parties/**/*.toml && taplo format --check data/parties/**/*.toml
- name: Ensure that parties are in the past
run: |
python3 tools/check_party_date.py data/parties/**/*.toml
67 changes: 67 additions & 0 deletions tools/check_party_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

"""Return error code if parties are found that are not in the past.
Author: Jochen Kupperschmidt
"""

from collections.abc import Iterable
from dataclasses import dataclass
from datetime import date
from pathlib import Path
import sys
import tomllib


@dataclass(frozen=True, slots=True)
class Party:
path: Path
slug: str
title: str
end_on: date


def main(filenames: list[str]) -> None:
if not filenames:
return

paths = map(Path, filenames)
parties = map(load_party, paths)
parties_not_over = select_parties_not_over(parties)

if parties_not_over:
print_parties_not_in_past(parties_not_over)
sys.exit(1)


def load_party(path: Path) -> Party:
toml = path.read_text()
data = tomllib.loads(toml)

return Party(
path=path,
slug=data['slug'],
title=data['title'],
end_on=data['end_on'],
)


def select_parties_not_over(parties: Iterable[Party]) -> list[Party]:
today = date.today()
return list(filter(lambda party: party.end_on >= today, parties))


def print_parties_not_in_past(parties: Iterable[Party]) -> None:
write_error_line('Found parties not in the past:')
for party in parties:
write_error_line(f'+ "{party.title}" ends on {party.end_on}')
write_error_line(f' - file: {party.path}')


def write_error_line(text: str) -> None:
sys.stderr.write(f'{text}\n')


if __name__ == '__main__':
filenames = sys.argv[1:]
main(filenames)

0 comments on commit ddbba01

Please sign in to comment.