Skip to content

Commit 0ad030b

Browse files
committed
Add GitHub Enterprise Server support
Enable CDash to post check runs and commit statuses to ghe installations in addition to github.com. Changes: - Add GITHUB_ENTERPRISE_URL config option (env var) - Pass enterprise URL to GitHubClient constructor with apiVersion=null (so it defaults to 'v3') so the PathPrepend plugin builds correct /api/v3/ paths for ghe - Add isGitHubUrl() helpers in GitHub.php and RepositoryUtils.php to match repository URLs against both github.com and the configured GHE host - Convert get_github_api_url() to construct /api/v3/repos/ paths for GHE repositories - Make DoneHandler always create/update the check when a revision exists, rather than requiring the pendingSubmissions recheck flag
1 parent d80c717 commit 0ad030b

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

app/Http/Submission/Handlers/DoneHandler.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ public function endElement($parser, $name): void
6363
$this->Build->UpdateBuild($this->Build->Id, -1, -1);
6464
$this->Build->MarkAsDone(true);
6565

66-
// Should we re-run any checks that were previously marked
67-
// as pending?
68-
if ($pendingSubmissionsModel !== null && $pendingSubmissionsModel->recheck) {
69-
$revision = \App\Models\Build::findOrFail((int) $this->Build->Id)->updateStep->revision ?? '';
66+
// Create or update the GitHub check for this commit.
67+
$revision = \App\Models\Build::findOrFail((int) $this->Build->Id)->updateStep->revision ?? '';
68+
if ($revision !== '') {
7069
Repository::createOrUpdateCheck($revision);
7170
}
7271

app/Utils/RepositoryUtils.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414

1515
class RepositoryUtils
1616
{
17+
private static function isGitHubUrl(string $url): bool
18+
{
19+
if (str_contains($url, 'github.com')) {
20+
return true;
21+
}
22+
$enterpriseUrl = config('cdash.github_enterprise_url');
23+
if ($enterpriseUrl !== null) {
24+
$host = parse_url($enterpriseUrl, PHP_URL_HOST);
25+
return $host !== false && str_contains($url, $host);
26+
}
27+
return false;
28+
}
29+
1730
/** Return the GitHub diff URL */
1831
public static function get_github_diff_url($projecturl, $directory, $file, $revision)
1932
{
@@ -146,6 +159,18 @@ public static function post_pull_request_comment($projectid, $pull_request, $com
146159
/** Convert GitHub repository viewer URL into corresponding API URL. */
147160
public static function get_github_api_url($github_url): string
148161
{
162+
$enterpriseUrl = config('cdash.github_enterprise_url');
163+
if ($enterpriseUrl !== null) {
164+
$host = parse_url($enterpriseUrl, PHP_URL_HOST);
165+
if ($host !== false && str_contains($github_url, $host)) {
166+
// GHE: ...://<host>/<user>/<repo> → ...://<host>/api/v3/repos/<user>/<repo>
167+
$idx = strpos($github_url, $host);
168+
$idx2 = $idx + strlen($host) + 1;
169+
$api_url = substr($github_url, 0, $idx) . $host . '/api/v3/repos/';
170+
$api_url .= substr($github_url, $idx2);
171+
return $api_url;
172+
}
173+
}
149174
/*
150175
* For a URL of the form:
151176
* ...://github.com/<user>/<repo>
@@ -166,7 +191,7 @@ public static function post_github_pull_request_comment(Project $project, $pull_
166191
$repo = null;
167192
$repositories = $project->GetRepositories();
168193
foreach ($repositories as $repository) {
169-
if (str_contains($repository['url'], 'github.com')) {
194+
if (self::isGitHubUrl($repository['url'])) {
170195
$repo = $repository;
171196
break;
172197
}

app/cdash/app/Lib/Repository/GitHub.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct(Project $project)
7676

7777
$repositories = $this->project->GetRepositories();
7878
foreach ($repositories as $repo) {
79-
if (str_contains($repo['url'], 'github.com')) {
79+
if ($this->isGitHubUrl($repo['url'])) {
8080
$this->installationId = $repo['username'];
8181
break;
8282
}
@@ -93,7 +93,8 @@ public function setApiClient(GitHubClient $client): void
9393
protected function initializeApiClient(): void
9494
{
9595
$builder = new GitHubBuilder();
96-
$apiClient = new GitHubClient($builder, 'machine-man-preview');
96+
$enterpriseUrl = config('cdash.github_enterprise_url');
97+
$apiClient = new GitHubClient($builder, null, $enterpriseUrl);
9798
$this->setApiClient($apiClient);
9899
}
99100

@@ -662,6 +663,19 @@ public function getRepository(): string
662663
return $this->repo;
663664
}
664665

666+
private function isGitHubUrl(string $url): bool
667+
{
668+
if (str_contains($url, 'github.com')) {
669+
return true;
670+
}
671+
$enterpriseUrl = config('cdash.github_enterprise_url');
672+
if ($enterpriseUrl !== null) {
673+
$host = parse_url($enterpriseUrl, PHP_URL_HOST);
674+
return $host !== false && str_contains($url, $host);
675+
}
676+
return false;
677+
}
678+
665679
protected function getRepositoryInformation(): void
666680
{
667681
$url = str_replace('//', '', $this->project->CvsUrl ?? '');

config/cdash.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'delete_old_subprojects' => env('DELETE_OLD_SUBPROJECTS', true),
4040
'github_always_pass' => env('GITHUB_ALWAYS_PASS', false),
4141
'github_app_id' => env('GITHUB_APP_ID', null),
42+
'github_enterprise_url' => env('GITHUB_ENTERPRISE_URL', null),
4243
'github_private_key' => env('GITHUB_PRIVATE_KEY', null),
4344
'github_webhook_secret' => env('GITHUB_WEBHOOK_SECRET', null),
4445
'large_text_limit' => env('LARGE_TEXT_LIMIT', 0),

0 commit comments

Comments
 (0)