|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Support\GitHub; |
| 4 | + |
| 5 | +/** |
| 6 | + * @property string $url |
| 7 | + * @property string $assets_url |
| 8 | + * @property string $upload_url |
| 9 | + * @property string $html_url |
| 10 | + * @property int $id |
| 11 | + * @property array $author |
| 12 | + * @property string $node_id |
| 13 | + * @property string $tag_name |
| 14 | + * @property string $target_commitish |
| 15 | + * @property string $name |
| 16 | + * @property bool $draft |
| 17 | + * @property bool $prerelease |
| 18 | + * @property string $created_at |
| 19 | + * @property string $published_at |
| 20 | + * @property array $assets |
| 21 | + * @property string $tarball_url |
| 22 | + * @property string $zipball_url |
| 23 | + * @property string $body |
| 24 | + * @property int $mentions_count |
| 25 | + */ |
| 26 | +class Release |
| 27 | +{ |
| 28 | + private bool $withUserLinks = true; |
| 29 | + |
| 30 | + private bool $convertLinks = true; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private array $data |
| 34 | + ) { |
| 35 | + // |
| 36 | + } |
| 37 | + |
| 38 | + public function __get(string $name) |
| 39 | + { |
| 40 | + return $this->data[$name] ?? null; |
| 41 | + } |
| 42 | + |
| 43 | + public function __isset(string $name): bool |
| 44 | + { |
| 45 | + return isset($this->data[$name]); |
| 46 | + } |
| 47 | + |
| 48 | + public function getBodyForMarkdown(): string |
| 49 | + { |
| 50 | + $body = $this->body; |
| 51 | + |
| 52 | + // Convert any URLs to Markdown links |
| 53 | + if ($this->convertLinks) { |
| 54 | + $body = preg_replace( |
| 55 | + '/https?:\/\/[^\s]+\/pull\/(\d+)/', |
| 56 | + '[#$1]($0)', |
| 57 | + $body |
| 58 | + ); |
| 59 | + |
| 60 | + $body = preg_replace( |
| 61 | + '/(https?:\/\/(?![^\s]+\/pull\/\d+)[^\s]+)/', |
| 62 | + '[$1]($1)', |
| 63 | + $body |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + // Change any @ tags to markdown links to GitHub |
| 68 | + if ($this->withUserLinks) { |
| 69 | + $body = preg_replace( |
| 70 | + '/@([a-zA-Z0-9_]+)/', |
| 71 | + '[@$1](https://github.com/$1)', |
| 72 | + $body |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return preg_replace('/^#/m', '###', $body); |
| 77 | + } |
| 78 | + |
| 79 | + public function withoutUserLinks(bool $withoutUserLinks = true): static |
| 80 | + { |
| 81 | + $this->withUserLinks = ! $withoutUserLinks; |
| 82 | + |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + public function withoutLinkConversion(bool $convertLinks = true): static |
| 87 | + { |
| 88 | + $this->convertLinks = ! $convertLinks; |
| 89 | + |
| 90 | + return $this; |
| 91 | + } |
| 92 | +} |
0 commit comments