From 42b42028fea334c8379731747f84d8774813712a Mon Sep 17 00:00:00 2001 From: Haardik Dharma Date: Thu, 1 Oct 2020 17:14:21 +0530 Subject: [PATCH] Added GitHub support for self-hosted repositories --- src/detectors/ScanningStrategyDetector.ts | 32 +++++++++++++++++++++++ src/services/git/GitHubService.ts | 13 +++++++++ 2 files changed, 45 insertions(+) diff --git a/src/detectors/ScanningStrategyDetector.ts b/src/detectors/ScanningStrategyDetector.ts index 422ed18a6..4ea735ad3 100644 --- a/src/detectors/ScanningStrategyDetector.ts +++ b/src/detectors/ScanningStrategyDetector.ts @@ -90,6 +90,10 @@ export class ScanningStrategyDetector implements IDetector => { + let serviceType = undefined; + + try { + await this.gitHubService.listRepos(); + await this.gitHubService.listGroups(); + serviceType = ServiceType.github; + } catch (error) { + return undefined; + } + + // second check to ensure that it is really a GitHub API + try { + const response = await this.gitHubService.checkVersion(); + if (has(response, 'version') && has(response, 'revision')) { + return ServiceType.github; + } + } catch (error) { + this.d(error); //debug error + if (error.response?.status === 401 || error.response?.status === 403) { + // return undefined if we're not sure that the service is Github + // - it prompts user for a credentials + return serviceType; + } + return undefined; + } + }; } export interface ScanningStrategy { diff --git a/src/services/git/GitHubService.ts b/src/services/git/GitHubService.ts index 7941fab07..3b2bc9148 100644 --- a/src/services/git/GitHubService.ts +++ b/src/services/git/GitHubService.ts @@ -65,6 +65,10 @@ export class GitHubService implements IVCSService { this.cache.purge(); } + async checkVersion() { + return (await this.client.Version.check()).data; + } + /** * The parent and source objects are present when the repository is a fork. * @@ -556,6 +560,15 @@ export class GitHubService implements IVCSService { updatedAt: comment.updated_at, }; } + async listRepos() { + const { data } = await this.unwrap(this.client.Projects.list()); + return data; + } + + async listGroups() { + const { data } = await this.unwrap(this.client.Users.listGroups()); + return data; + } /** * Update Comment on a Pull Request