Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GitHub support for self-hosted repositories (fix: #269) #429

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/detectors/ScanningStrategyDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class ScanningStrategyDetector implements IDetector<string, ScanningStrat
const remotelyDetectedService = await this.determineGitLabRemoteServiceType();
if (remotelyDetectedService) return remotelyDetectedService;

//Trying to determine GitHub service type if it's self hosted
const remotelyDetectedGitHubService = await this.determineGitHubRemoteServiceType();
if (remotelyDetectedGitHubService) return remotelyDetectedGitHubService;

throw ErrorFactory.newInternalError(
`Unable to detect scanning strategy. It seems that the service is not implemented yet. (Input path: ${path})`,
);
Expand Down Expand Up @@ -218,6 +222,34 @@ export class ScanningStrategyDetector implements IDetector<string, ScanningStrat
return undefined;
}
};

private determineGitHubRemoteServiceType = async (): Promise<ServiceType | undefined> => {
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 {
Expand Down
13 changes: 13 additions & 0 deletions src/services/git/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down