Skip to content

Commit

Permalink
Refactor (#16)
Browse files Browse the repository at this point in the history
* Only parse categories when needed

* Remove redundant PHP action

* Rename classes and add tests

* Remove poster property as they're no longer a thing

* Use YouTube video duration to calculate end time

* Cleanup

* Add YouTube video duration

* Add provisional status

* Add tests

* Rename class

* Add source dir

* Add calendar diff to PR

* Add calendar diff to PR

* Add calendar diff to PR

* Fix event start date

* Add 5 minutes to YouTube scheduled start time

* Cleanup

* Add scrutinizer config

* Fix spelling

* Improve diff

* Improve diff

* Remove redundant clone

* Fix alarms, add speed qualifications to iCal calendar
  • Loading branch information
nicoSWD committed Apr 12, 2024
1 parent 9fa8b71 commit dcbb52d
Show file tree
Hide file tree
Showing 29 changed files with 549 additions and 122 deletions.
43 changes: 42 additions & 1 deletion .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ on:
pull_request:
branches: [ "main" ]

permissions:
pull-requests: write

jobs:
phpunit:
code-quality:
runs-on: ubuntu-latest
steps:
- name: "Checkout code"
Expand All @@ -24,3 +27,41 @@ jobs:
uses: php-actions/phpstan@v3
with:
level: 6

calendar-diff:
runs-on: ubuntu-latest
steps:
- name: "Checkout code"
uses: actions/checkout@master

- name: "Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer

- name: "Install poppler-utils"
run: sudo apt-get update && sudo apt-get install poppler-utils -y

- name: "Build Calendar"
run: |
composer install --no-dev
php app/run.php --season 2024 --output new-calendar.json --format json
- name: "Check diff"
run: |
curl -sSL "https://calendar.ifsc.stream/?format=json&nocache=1" --output old-calendar.json
php bin/calendar-diff old-calendar.json new-calendar.json > diff.md
if [ $(wc -w < diff.md) -gt 0 ]; then
echo "This PR produces the following changes in the calendar" > calendar.diff
cat diff.md >> calendar.diff
else
echo "No changes in the calendar were produced" > calendar.diff
fi
- name: "Comment calendar diff in PR"
uses: thollander/actions-comment-pull-request@v2
with:
filePath: calendar.diff
comment_tag: calendar_diff
5 changes: 0 additions & 5 deletions .github/workflows/push-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ jobs:
- name: "Checkout code"
uses: actions/checkout@master

- name: "Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'

- name: "Build and publish a Docker image for ${{ github.repository }}"
uses: macbre/push-to-ghcr@master
with:
Expand Down
16 changes: 16 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
build:
environment:
php:
version: 8.3.3
nodes:
analysis:
tests:
override:
- php-scrutinizer-run
filter:
excluded_paths:
- tests/*
- vendor/*

checks:
php: true
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ $ ./build/ifsc-calendar.phar \

## 🔧 Todo
- [ ] Use Symfony serializer to build response object from IFSC API
- [ ] Calculate average event duration and add it to `events.json` and to the calendar as the official web
does not tell when an event finishes.
- [ ] Calculate average event duration (based on past events)
- [ ] Add proper start lists to rounds
- [ ] Add tickets URL?
- [ ] Finish writing calendar setup guides
- [ ] Cleanup PHP code
- [ ] Add more tests
- [ ] Make scraping more robust and fail on errors or missing data
- [ ] Fix scraper for older seasons (formatting changes drastically)
- [ ] Add more domain events to improve output log
- [ ] Add automated tests to PRs (unit tests, coverage, etc)
- [x] Make scraping more robust and fail on errors or missing data
- [x] Add more domain events to improve output log
- [x] Add automated tests to PRs (unit tests, coverage, etc)
- [x] Push Docker image to Docker Hub
- [x] Add BuyMeACoffee link to `.ics` calendar events
- [x] Show activity and warnings in console (domain events)
Expand All @@ -151,6 +151,7 @@ $ ./build/ifsc-calendar.phar \
- [x] Always serve asset from latest release on calendar URL
- [x] Fetch stream links from YouTube API if none can be scraped
- [x] Automatically regenerate calendar and update release
- [ ] ~~Fix scraper for older seasons (formatting changes drastically)~~

## Requirements
- PHP 8.3
Expand Down
18 changes: 9 additions & 9 deletions bin/calendar-diff
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,28 @@ function generate_round_diff(array $oldRounds, array $newRounds): string
$oldValue = $oldRound[$key];

if ($newValue !== $oldValue) {
$roundDiff .= "|{$key} |" . normalize_value($oldValue) . "|" . normalize_value($newValue) . "|" . PHP_EOL;
$roundDiff .= "| 👉 |_{$key}_ |" . normalize_value($oldValue) . "|" . normalize_value($newValue) . "|" . PHP_EOL;
}
} else {
if (normalize_value($newValue) !== 'null') {
$roundDiff .= "|{$key} |null|" . normalize_value($newValue) . "|" . PHP_EOL;
$roundDiff .= "| 👉 |_{$key}_ |null|" . normalize_value($newValue) . "|" . PHP_EOL;
}
}
}

if ($roundDiff) {
$changedRounds .= "| {$newRound['name']} |--- |--- |" . PHP_EOL;
$changedRounds .= "|-----------------------|---------------|-----------------|" . PHP_EOL;
$changedRounds .= "| **Key** | **Old Value** | **New Value** |" . PHP_EOL;
$changedRounds .= $roundDiff . PHP_EOL;
$changedRounds .= "| **{$newRound['name']}** | | | |" . PHP_EOL;
$changedRounds .= $roundDiff;
}
}
}

if ($changedRounds) {
$diff .= PHP_EOL;
$diff .= "#### Changed Rounds" . PHP_EOL;
$diff .= $changedRounds;
$diff .= "|Round | **Key** | **Old Value** | **New Value** |" . PHP_EOL;
$diff .= "|------|-----------------------|---------------|-----------------|" . PHP_EOL;
$diff .= $changedRounds . PHP_EOL;
}

if ($addedRounds) {
Expand Down Expand Up @@ -168,7 +168,7 @@ foreach ($newEvents as $newEvent) {
if (count($newEvent['start_list']) > 0) {
$addedEvents .= "#### 📋 Start List:" . PHP_EOL;
$addedEvents .= "|" . implode('|', array_keys($newEvent['start_list'][0])) . "|" . PHP_EOL;
$addedEvents .= "|" . str_repeat('-------------|', count($newEvent['start_list'][0])) . PHP_EOL;
$addedEvents .= "|" . str_repeat(' |', count($newEvent['start_list'][0])) . PHP_EOL;

foreach ($newEvent['start_list'] as $startList) {
foreach ($startList as $value) {
Expand Down Expand Up @@ -250,7 +250,7 @@ foreach ($newEvents as $newEvent) {
}

if ($startListDiff) {
$changedEvents .= "| 📋 Start List | --- | --- | --- | --- |" . PHP_EOL;
$changedEvents .= "| 📋 Start List | | | | |" . PHP_EOL;
$changedEvents .= "|---------------|-----------------|---------------|--------------------|-------------------|" . PHP_EOL;
$changedEvents .= "| **Status** | **First Name** | **Last Name** | **New First Name** | **New Last Name** |" . PHP_EOL;
$changedEvents .= $startListDiff . PHP_EOL;
Expand Down
6 changes: 3 additions & 3 deletions config/services/domain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ services:
class: nicoSWD\IfscCalendar\Domain\Event\IFSCEventFactory
arguments:
- '@nicoSWD\IfscCalendar\Domain\Calendar\SiteURLBuilder'
- '@nicoSWD\IfscCalendar\Domain\Event\IFSCEventsSlug'
- '@nicoSWD\IfscCalendar\Domain\Event\IFSCEventSlug'
- '@nicoSWD\IfscCalendar\Domain\StartList\IFSCStartListGenerator'

nicoSWD\IfscCalendar\Domain\Event\IFSCEventsSlug: ~
nicoSWD\IfscCalendar\Domain\Event\IFSCEventSlug: ~
nicoSWD\IfscCalendar\Domain\Event\IFSCEventSorter: ~
nicoSWD\IfscCalendar\Domain\Event\Helpers\DOMHelper: ~

Expand All @@ -98,7 +98,7 @@ services:
class: nicoSWD\IfscCalendar\Domain\Round\IFSCRoundsScraper
arguments:
- '@nicoSWD\IfscCalendar\Domain\Round\IFSCRoundFactory'
- '@nicoSWD\IfscCalendar\Infrastructure\Round\PDFRoundProvider'
- '@nicoSWD\IfscCalendar\Infrastructure\Round\InfoSheetRoundProvider'

nicoSWD\IfscCalendar\Domain\Round\IFSCRoundFactory:
class: nicoSWD\IfscCalendar\Domain\Round\IFSCRoundFactory
Expand Down
4 changes: 2 additions & 2 deletions config/services/infrastructure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ services:
- '@nicoSWD\IfscCalendar\Infrastructure\HttpClient\HttpGuzzleClient'
- '@nicoSWD\IfscCalendar\Infrastructure\DomainEvent\SymfonyEventDispatcher'

nicoSWD\IfscCalendar\Infrastructure\Round\PDFRoundProvider:
class: nicoSWD\IfscCalendar\Infrastructure\Round\PDFRoundProvider
nicoSWD\IfscCalendar\Infrastructure\Round\InfoSheetRoundProvider:
class: nicoSWD\IfscCalendar\Infrastructure\Round\InfoSheetRoundProvider
arguments:
- '@nicoSWD\IfscCalendar\Infrastructure\Schedule\InfoSheetScheduleProvider'
- '@nicoSWD\IfscCalendar\Infrastructure\Schedule\InfoSheetDownloader'
Expand Down
35 changes: 17 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" colors="true" bootstrap="vendor/autoload.php">
<php>
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
33 changes: 33 additions & 0 deletions src/Domain/Discipline/IFSCDisciplines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

/**
* @license http://opensource.org/licenses/mit-license.php MIT
* @link https://github.com/nicoSWD
* @author Nicolas Oelgart <nico@oelgart.com>
*/
namespace nicoSWD\IfscCalendar\Domain\Discipline;

final readonly class IFSCDisciplines
{
/** @param IFSCDiscipline[] $disciplines */
public function __construct(
private array $disciplines,
) {
}

public function isSpeed(): bool
{
return $this->hasDiscipline(IFSCDiscipline::SPEED);
}

/** @return IFSCDiscipline[] */
public function all(): array
{
return $this->disciplines;
}

public function hasDiscipline(IFSCDiscipline $discipline): bool
{
return in_array($discipline, $this->disciplines, strict: true);
}
}
4 changes: 2 additions & 2 deletions src/Domain/Event/IFSCEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class IFSCEvent

/**
* @param IFSCRound[] $rounds
* @param IFSCStarter[] $starters
* @param IFSCStarter[] $startList
* @param IFSCDiscipline[] $disciplines
*/
public function __construct(
Expand All @@ -40,7 +40,7 @@ public function __construct(
public readonly DateTimeImmutable $endsAt,
public readonly array $disciplines,
array $rounds,
public readonly array $starters = [],
public readonly array $startList = [],
) {
$this->rounds = $rounds;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Domain/Event/IFSCEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
public function __construct(
private SiteURLBuilder $siteURLBuilder,
private IFSCEventsSlug $eventsSlug,
private IFSCEventSlug $eventsSlug,
private IFSCStartListGenerator $startListGenerator,
) {
}
Expand All @@ -47,7 +47,7 @@ public function create(IFSCSeasonYear $season, IFSCEventInfo $event, array $roun
endsAt: $endDate,
disciplines: $event->disciplines,
rounds: $rounds,
starters: $this->buildStartList($event->eventId),
startList: $this->buildStartList($event->eventId),
);
}

Expand All @@ -60,7 +60,7 @@ private function generateDateRangeFromRounds(array $rounds, IFSCEventInfo $event
$confirmedDates = [];

foreach ($rounds as $round) {
if ($round->status->isConfirmed()) {
if ($round->status->isConfirmed() || $round->status->isProvisional()) {
$confirmedDates[] = $round->startTime;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
namespace nicoSWD\IfscCalendar\Domain\Event;

final readonly class IFSCEventsSlug
final readonly class IFSCEventSlug
{
public function create(string $eventName): string
{
Expand Down
22 changes: 11 additions & 11 deletions src/Domain/Event/IFSCEventsFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,13 @@ public function __construct(
season: $season,
event: $event,
rounds: $rounds,
posterUrl: $scrapedRounds->posterUrl,
posterUrl: null,
);
}

return $events;
}

/**
* @throws IFSCEventsScraperException
* @throws Exception
*/
private function fetchScrapedRounds(IFSCEventInfo $event): IFSCScrapedEventsResult
{
return $this->roundsScraper->fetchRoundsAndPosterForEvent($event);
}

/** @return IFSCRound[] */
private function generateRounds(IFSCEventInfo $event): array
{
Expand All @@ -87,7 +78,7 @@ private function generateRounds(IFSCEventInfo $event): array
event: $event,
roundName: $this->normalizeRoundName($round),
startTime: $startTime,
endTime: $startTime->modify('90 minutes'),
endTime: $startTime->modify('+90 minutes'),
status: IFSCRoundStatus::ESTIMATED,
);
}
Expand Down Expand Up @@ -127,6 +118,15 @@ private function fetchLeaguesForSeason(IFSCSeasonYear $season, array $selectedLe
return $filteredLeagues;
}

/**
* @throws IFSCEventsScraperException
* @throws Exception
*/
private function fetchScrapedRounds(IFSCEventInfo $event): IFSCScrapedEventsResult
{
return $this->roundsScraper->fetchRoundsForEvent($event);
}

/**
* @param string[] $selectedLeagues
* @return IFSCEventInfo[]
Expand Down
1 change: 0 additions & 1 deletion src/Domain/Event/IFSCScrapedEventsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
{
/** @param IFSCRound[] $rounds */
public function __construct(
public ?string $posterUrl,
public array $rounds,
) {
}
Expand Down
Loading

0 comments on commit dcbb52d

Please sign in to comment.