|
10 | 10 | use CDash\Model\Project; |
11 | 11 | use CDash\ServiceContainer; |
12 | 12 | use Illuminate\Support\Facades\Log; |
| 13 | +use Illuminate\Support\Str; |
| 14 | +use Illuminate\Support\Uri; |
13 | 15 | use PDO; |
14 | 16 |
|
15 | 17 | class RepositoryUtils |
16 | 18 | { |
| 19 | + public static function getEnterpriseUrl(): ?string |
| 20 | + { |
| 21 | + $url = config('cdash.github_enterprise_url'); |
| 22 | + return is_string($url) && $url !== '' ? $url : null; |
| 23 | + } |
| 24 | + |
| 25 | + public static function isGitHubUrl(string $url): bool |
| 26 | + { |
| 27 | + if (str_contains($url, 'github.com')) { |
| 28 | + return true; |
| 29 | + } |
| 30 | + $enterpriseUrl = self::getEnterpriseUrl(); |
| 31 | + if ($enterpriseUrl !== null) { |
| 32 | + $host = Uri::of($enterpriseUrl)->host(); |
| 33 | + return is_string($host) && str_contains($url, $host); |
| 34 | + } |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
17 | 38 | /** Return the GitHub diff URL */ |
18 | 39 | public static function get_github_diff_url($projecturl, $directory, $file, $revision) |
19 | 40 | { |
@@ -143,30 +164,29 @@ public static function post_pull_request_comment($projectid, $pull_request, $com |
143 | 164 | } |
144 | 165 | } |
145 | 166 |
|
146 | | - /** Convert GitHub repository viewer URL into corresponding API URL. */ |
147 | | - public static function get_github_api_url($github_url): string |
| 167 | + /** |
| 168 | + * Convert a GitHub repository viewer URL (e.g. https://<host>/<user>/<repo>) |
| 169 | + * into the corresponding REST API URL. |
| 170 | + */ |
| 171 | + public static function get_github_api_url(string $github_url): string |
148 | 172 | { |
149 | | - /* |
150 | | - * For a URL of the form: |
151 | | - * ...://github.com/<user>/<repo> |
152 | | - * We return: |
153 | | - * ...://api.github.com/repos/<user>/<repo> |
154 | | - */ |
155 | | - $idx1 = strpos($github_url, 'github.com'); |
156 | | - $idx2 = $idx1 + strlen('github.com/'); |
157 | | - $api_url = substr($github_url, 0, $idx2); |
158 | | - $api_url = str_replace('github.com', 'api.github.com', $api_url); |
159 | | - $api_url .= 'repos/'; |
160 | | - $api_url .= substr($github_url, $idx2); |
161 | | - return $api_url; |
| 173 | + $enterpriseUrl = self::getEnterpriseUrl(); |
| 174 | + $host = $enterpriseUrl !== null ? Uri::of($enterpriseUrl)->host() : null; |
| 175 | + |
| 176 | + if (is_string($host) && str_contains($github_url, $host)) { |
| 177 | + // GHE uses <host>/api/v3/repos/ as the API base |
| 178 | + return Str::replaceFirst($host . '/', $host . '/api/v3/repos/', $github_url); |
| 179 | + } |
| 180 | + // GitHub.com uses api.github.com/repos/ as the API base |
| 181 | + return Str::replaceFirst('github.com/', 'api.github.com/repos/', $github_url); |
162 | 182 | } |
163 | 183 |
|
164 | 184 | public static function post_github_pull_request_comment(Project $project, $pull_request, $comment, $cdash_url): void |
165 | 185 | { |
166 | 186 | $repo = null; |
167 | 187 | $repositories = $project->GetRepositories(); |
168 | 188 | foreach ($repositories as $repository) { |
169 | | - if (str_contains($repository['url'], 'github.com')) { |
| 189 | + if (self::isGitHubUrl($repository['url'])) { |
170 | 190 | $repo = $repository; |
171 | 191 | break; |
172 | 192 | } |
|
0 commit comments