|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPWatch\PHPCommitBuilder; |
| 4 | + |
| 5 | +class CommitFormatter { |
| 6 | + private array $commitsList = []; |
| 7 | + |
| 8 | + private array $commitsGroupedByAuthor = []; |
| 9 | + private const EOL = "\r\n"; |
| 10 | + |
| 11 | + public function __construct(array $inputCommits) { |
| 12 | + $this->process($inputCommits); |
| 13 | + } |
| 14 | + |
| 15 | + private function process(array $inputCommits): void { |
| 16 | + $formattedCommits = []; |
| 17 | + $i = 0; |
| 18 | + |
| 19 | + foreach ($inputCommits as $commit) { |
| 20 | + $commitArray = $this->splitCommit($commit); |
| 21 | + if ($this->shouldSkip($commitArray['subject'])) { |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + $commitArray['formatted'] = KeywordEnhancer::enhanceCommit($commitArray['subject'], substr($commitArray['hash'], 0, 10)); |
| 26 | + $formattedCommits[$i] = $commitArray; |
| 27 | + |
| 28 | + if (!isset($this->commitsGroupedByAuthor[$commitArray['author']])) { |
| 29 | + $this->commitsGroupedByAuthor[$commitArray['author']] = []; |
| 30 | + } |
| 31 | + |
| 32 | + $this->commitsGroupedByAuthor[$commitArray['author']][] = $i; |
| 33 | + |
| 34 | + $i++; |
| 35 | + } |
| 36 | + |
| 37 | + $this->commitsList = $formattedCommits; |
| 38 | + ksort($this->commitsGroupedByAuthor); |
| 39 | + } |
| 40 | + |
| 41 | + private function splitCommit(\stdClass $commit): array { |
| 42 | + $commitMessage = $commit->commit->message; |
| 43 | + $commitMessageParts = explode("\n", $commitMessage, 2); |
| 44 | + |
| 45 | + return [ |
| 46 | + 'subject' => trim(trim($commitMessageParts[0]), '.'), |
| 47 | + 'author' => trim($commit->commit->author->name), |
| 48 | + 'hash' => trim($commit->sha), |
| 49 | + 'message' => trim($commitMessage) |
| 50 | + ]; |
| 51 | + } |
| 52 | + |
| 53 | + private function shouldSkip(string $commitMessage): bool { |
| 54 | + if ($commitMessage === '') { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + // Skip merge commits |
| 59 | + if (str_starts_with($commitMessage, 'Merge branch')) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + // Skip merge commits |
| 64 | + if (str_starts_with($commitMessage, 'Merge remote-tracking branch')) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + // Skip "[ci skip]" messages |
| 69 | + if (str_contains($commitMessage, '[ci skip]') || str_contains($commitMessage, '[skip ci]')) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + if (str_contains($commitMessage, 'is now for PHP 8') || str_contains($commitMessage, 'is now for PHP-8')) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + if (str_starts_with($commitMessage, 'Update NEWS for ')) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + public function getFormattedCommitList(): array { |
| 85 | + return $this->commitsList; |
| 86 | + } |
| 87 | + |
| 88 | + public function getFormattedCommitListMarkup(): string { |
| 89 | + $output = ''; |
| 90 | + foreach ($this->getFormattedCommitList() as $commit) { |
| 91 | + $output .= $this->getFormattedCommitListMarkup($commit['formatted'] . ' by ' . $commit['author']); |
| 92 | + } |
| 93 | + |
| 94 | + return $output; |
| 95 | + } |
| 96 | + |
| 97 | + public function getFormattedCommitListGroupedByAuthor(): array { |
| 98 | + $return = $this->commitsGroupedByAuthor; |
| 99 | + foreach ($return as $author => &$commitList) { |
| 100 | + foreach ($commitList as $key => &$commitI) { |
| 101 | + $commitI = $this->commitsList[$commitI]; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return $return; |
| 106 | + } |
| 107 | + |
| 108 | + public function getFormattedCommitListGroupedByAuthorMarkup(): string { |
| 109 | + $commitsByAuthor = $this->getFormattedCommitListGroupedByAuthor(); |
| 110 | + |
| 111 | + $output = ''; |
| 112 | + foreach ($commitsByAuthor as $author => $commitList) { |
| 113 | + $output .= self::markdownTitle($author); |
| 114 | + foreach ($commitList as $commit) { |
| 115 | + $output .= self::markdownListItem($commit['formatted']); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return $output; |
| 120 | + } |
| 121 | + |
| 122 | + private static function markdownTitle(string $title): string { |
| 123 | + return '### ' . self::plainText($title) . static::EOL; |
| 124 | + } |
| 125 | + |
| 126 | + private static function markdownListItem(string $listItem): string { |
| 127 | + return ' - ' . self::plainText($listItem) . static::EOL; |
| 128 | + } |
| 129 | + |
| 130 | + private static function plainText(string $text): string { |
| 131 | + return htmlspecialchars($text); |
| 132 | + } |
| 133 | +} |
0 commit comments