-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also fixes new releases because of metadata updates
- Loading branch information
Showing
2 changed files
with
260 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
#!/usr/bin/env php | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license http://opensource.org/licenses/mit-license.php MIT | ||
* @link https://github.com/nicoSWD | ||
* @author Nicolas Oelgart <[email protected]> | ||
*/ | ||
|
||
function generate_start_list_diff(array $oldStartList, array $newStartList): string | ||
{ | ||
if ($oldStartList === $newStartList) { | ||
return ''; | ||
} | ||
|
||
$diff = ''; | ||
|
||
foreach ($newStartList as $key => $athlete) { | ||
if (isset($oldStartList[$key])) { | ||
if ($athlete !== $oldStartList[$key]) { | ||
$diff .= "|Replaced|<s>{$oldStartList[$key]['first_name']}</s>|<s>{$oldStartList[$key]['last_name']}</s>|{$athlete['first_name']}|{$athlete['last_name']}" . PHP_EOL; | ||
} | ||
} else { | ||
$diff .= "|Added|{$athlete['first_name']}|{$athlete['last_name']}|---|---|" . PHP_EOL; | ||
} | ||
} | ||
|
||
foreach ($oldStartList as $key => $athlete) { | ||
if (!isset($newStartList[$key])) { | ||
$diff .= "|Removed|<s>{$athlete['first_name']}</s>|<s>{$athlete['last_name']}</s>|---|---|" . PHP_EOL; | ||
} | ||
} | ||
|
||
return $diff; | ||
} | ||
|
||
function normalize_value(mixed $value): string | ||
{ | ||
if (empty($value)) { | ||
return 'null'; | ||
} | ||
|
||
if (is_array($value)) { | ||
$value = implode(', ', $value); | ||
} | ||
|
||
return (string) $value; | ||
} | ||
|
||
function find_round_by_name(array $oldRounds, string $name): ?array | ||
{ | ||
foreach ($oldRounds as $oldRound) { | ||
if ($oldRound['name'] === $name) { | ||
return $oldRound; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function get_event_by_id(array $events, int $eventId): ?array | ||
{ | ||
foreach ($events as $event) { | ||
if ($event['id'] === $eventId) { | ||
return $event; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function normalize_events_from_file(string $fileName): array | ||
{ | ||
return json_decode(file_get_contents($fileName), associative: true)['events']; | ||
} | ||
|
||
function generate_round_diff(array $oldRounds, array $newRounds): string | ||
{ | ||
$diff = ''; | ||
$changedRounds = ''; | ||
$addedRounds = ''; | ||
|
||
foreach ($newRounds as $newRound) { | ||
$oldRound = find_round_by_name($oldRounds, $newRound['name']); | ||
|
||
if (!$oldRound) { | ||
foreach ($newRound as $value) { | ||
$addedRounds .= "|" . normalize_value($value); | ||
} | ||
|
||
$addedRounds .= "|" . PHP_EOL; | ||
} else { | ||
$roundDiff = ''; | ||
foreach ($newRound as $key => $newValue) { | ||
$oldValue = $oldRound[$key]; | ||
|
||
if ($newValue !== $oldValue) { | ||
$roundDiff .= "|{$key} |" . normalize_value($oldValue) . "|" . 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; | ||
} | ||
} | ||
} | ||
|
||
if ($changedRounds) { | ||
$diff .= PHP_EOL; | ||
$diff .= "#### Changed Rounds" . PHP_EOL; | ||
$diff .= $changedRounds; | ||
} | ||
|
||
if ($addedRounds) { | ||
$diff .= "#### Added Rounds" . PHP_EOL; | ||
$diff .= "|" . implode('|', array_keys($newRounds[0])) . "|" . PHP_EOL; | ||
$diff .= "|" . str_repeat('-------------|', count($newRounds[0])) . PHP_EOL; | ||
$diff .= $addedRounds . PHP_EOL . PHP_EOL; | ||
} | ||
|
||
return $diff; | ||
} | ||
|
||
$oldEvents = normalize_events_from_file($argv[1]); | ||
$newEvents = normalize_events_from_file($argv[2]); | ||
|
||
$diff = ''; | ||
$addedEvents = ''; | ||
$removedEvents = ''; | ||
|
||
foreach ($newEvents as $newEvent) { | ||
if (!get_event_by_id($oldEvents, $newEvent['id'])) { | ||
$addedEvents .= "### 🏆 {$newEvent['name']}" . PHP_EOL; | ||
$addedEvents .= "|Key|Value|" . PHP_EOL; | ||
$addedEvents .= "|---|-----|" . PHP_EOL; | ||
|
||
foreach ($newEvent as $key => $value) { | ||
if ($key === 'rounds' || $key === 'start_list') { | ||
continue; | ||
} | ||
|
||
$addedEvents .= "|$key|" . normalize_value($value) ."|" . PHP_EOL; | ||
} | ||
|
||
$addedEvents .= PHP_EOL; | ||
$addedEvents .= "#### 🎯 Rounds:" . PHP_EOL; | ||
$addedEvents .= "|" . implode('|', array_keys($newEvent['rounds'][0])) . "|" . PHP_EOL; | ||
$addedEvents .= "|" . str_repeat('-------------|', count($newEvent['rounds'][0])) . PHP_EOL; | ||
|
||
foreach ($newEvent['rounds'] as $round) { | ||
foreach ($round as $value) { | ||
$addedEvents .= "|" . normalize_value($value); | ||
} | ||
$addedEvents .= "|" . PHP_EOL; | ||
} | ||
|
||
$addedEvents .= PHP_EOL; | ||
$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; | ||
|
||
foreach ($newEvent['start_list'] as $startList) { | ||
foreach ($startList as $value) { | ||
$addedEvents .= "|" . normalize_value($value); | ||
} | ||
$addedEvents .= "|" . PHP_EOL; | ||
} | ||
} | ||
} | ||
|
||
foreach ($oldEvents as $oldEvent) { | ||
if (!get_event_by_id($newEvents, $oldEvent['id'])) { | ||
$removedEvents .= "|🏆 {$oldEvent['name']}|---|--- |" . PHP_EOL; | ||
$removedEvents .= "|-------------|---------|-------|" . PHP_EOL; | ||
$removedEvents .= "|**Round** |**Starts At**|**Ends At**|" . PHP_EOL; | ||
|
||
foreach ($oldEvent['rounds'] as $round) { | ||
$removedEvents .= "|<s>{$round['name']}</s>|<s>{$round['starts_at']}</s>|<s>{$round['ends_at']}</s>" . PHP_EOL; | ||
} | ||
|
||
$removedEvents .= PHP_EOL; | ||
} | ||
} | ||
|
||
if ($addedEvents) { | ||
$diff .= "🎉 Added Events:" . PHP_EOL; | ||
$diff .= "===============" . PHP_EOL; | ||
$diff .= $addedEvents . PHP_EOL; | ||
} | ||
|
||
if ($removedEvents) { | ||
$diff .= "🗑️ Removed Events:" . PHP_EOL; | ||
$diff .= "===============" . PHP_EOL; | ||
$diff .= $removedEvents . PHP_EOL; | ||
} | ||
|
||
$changedEvents = ''; | ||
|
||
foreach ($newEvents as $newEvent) { | ||
$oldEvent = get_event_by_id($oldEvents, $newEvent['id']); | ||
|
||
if (!$oldEvent) { | ||
continue; | ||
} | ||
|
||
$newDiff = ''; | ||
$roundDiff = ''; | ||
$startListDiff = ''; | ||
|
||
foreach ($newEvent as $key => $value) { | ||
if ($key === 'rounds') { | ||
$roundDiff .= generate_round_diff($oldEvent[$key], $value); | ||
} elseif ($key === 'start_list') { | ||
$startListDiff .= generate_start_list_diff($oldEvent[$key], $value); | ||
} elseif (normalize_value($value) !== normalize_value($oldEvent[$key])) { | ||
$newDiff .= "|$key|" . normalize_value($oldEvent[$key]) ."|" . normalize_value($value) . "|" . PHP_EOL; | ||
} | ||
} | ||
|
||
if ($newDiff || $roundDiff || $startListDiff) { | ||
$changedEvents .= "#### 🏆 {$newEvent['name']}" . PHP_EOL; | ||
|
||
if ($newDiff) { | ||
$changedEvents .= "| ℹ️ Event Details | --- | --- |" . PHP_EOL; | ||
$changedEvents .= "|------------------------|---------------|----------------|" . PHP_EOL; | ||
$changedEvents .= "| **Key** | **Old Value** | **New Value** |" . PHP_EOL; | ||
$changedEvents .= $newDiff; | ||
} | ||
|
||
if ($roundDiff) { | ||
$changedEvents .= PHP_EOL; | ||
$changedEvents .= $roundDiff; | ||
} | ||
|
||
if ($startListDiff) { | ||
$changedEvents .= 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; | ||
} | ||
|
||
$changedEvents .= PHP_EOL; | ||
} | ||
} | ||
|
||
if ($changedEvents) { | ||
$diff .= "✏️ Changed Events:" . PHP_EOL; | ||
$diff .= "==================" . PHP_EOL; | ||
$diff .= $changedEvents; | ||
} | ||
|
||
echo $diff; |