Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Add Github Enterprise repositories support
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Vinokur <ivinokur@redhat.com>
  • Loading branch information
vinokurig committed Apr 12, 2023
1 parent 3968a39 commit ccfe086
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
6 changes: 4 additions & 2 deletions tools/devworkspace-generator/src/github/github-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ import { injectable } from 'inversify';
export class GithubResolver {
// eslint-disable-next-line max-len
static readonly GITHUB_URL_PATTERN =
/^(?:http)(?:s)?(?:\:\/\/)github\.com\/(?<repoUser>[^\/]+)\/(?<repoName>[^\/]+)((\/)|(?:\/(blob|tree)\/(?<branchName>[^\/]+)(?:\/(?<subFolder>.*))?))?$/;
/^(?<scheme>https?):\/\/(?<host>github(\..+)?\.[^\/]+)\/(?<repoUser>[^\/]+)\/(?<repoName>[^\/]+)((\/)|\/(blob|tree)\/(?<branchName>[^\/]+)(?:\/(?<subFolder>.*))?)?$/;

resolve(link: string): GithubUrl {
const match = GithubResolver.GITHUB_URL_PATTERN.exec(link);
if (!match) {
throw new Error(`Invalid github URL: ${link}`);
}
const scheme = this.getGroup(match, 'scheme');
const hostName = this.getGroup(match, 'host');
const repoUser = this.getGroup(match, 'repoUser');
const repoName = this.getGroup(match, 'repoName');
const branchName = this.getGroup(match, 'branchName', 'HEAD');
const subFolder = this.getGroup(match, 'subFolder');
return new GithubUrl(repoUser, repoName, branchName, subFolder);
return new GithubUrl(scheme, hostName, repoUser, repoName, branchName, subFolder);
}

getGroup(match: RegExpExecArray, groupName: string, defaultValue?: string) {
Expand Down
12 changes: 6 additions & 6 deletions tools/devworkspace-generator/src/github/github-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
* Provides helper methods on top of github URL to get for example raw content of get relative links
*/
export class GithubUrl {
// raw link
static readonly RAW_LINK = 'https://raw.githubusercontent.com';

constructor(
private readonly scheme: string,
private readonly hostName: string,
private readonly repoUser: string,
private readonly repoName: string,
private readonly branchName: string,
Expand All @@ -26,15 +25,16 @@ export class GithubUrl {
* Provides the raw link to the given path based on the current repository information
*/
getContentUrl(path: string): string {
return `${GithubUrl.RAW_LINK}/${this.repoUser}/${this.repoName}/${this.branchName}/${path}`;
const hostName = this.hostName === 'github.com' ? 'githubusercontent.com' : this.hostName;
return `${this.scheme}://raw.${hostName}/${this.repoUser}/${this.repoName}/${this.branchName}/${path}`;
}

getUrl(): string {
return `https://github.com/${this.repoUser}/${this.repoName}/tree/${this.branchName}/${this.subFolder}`;
return `${this.scheme}://${this.hostName}/${this.repoUser}/${this.repoName}/tree/${this.branchName}/${this.subFolder}`;
}

getCloneUrl(): string {
return `https://github.com/${this.repoUser}/${this.repoName}.git`;
return `${this.scheme}://${this.hostName}/${this.repoUser}/${this.repoName}.git`;
}

getRepoName(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@ describe('Test PluginRegistryResolver', () => {
});

test('basic resolve', async () => {
expect(githubResolver.resolve('http://github.com/eclipse/che').getUrl()).toBe(
expect(githubResolver.resolve('https://github.com/eclipse/che').getUrl()).toBe(
'https://github.com/eclipse/che/tree/HEAD/'
);
expect(githubResolver.resolve('https://github.com/eclipse/che/tree/7.30.x').getUrl()).toBe(
'https://github.com/eclipse/che/tree/7.30.x/'
);

expect(githubResolver.resolve('http://github.com/eclipse/che').getContentUrl('README.md')).toBe(
expect(githubResolver.resolve('https://github.com/eclipse/che').getContentUrl('README.md')).toBe(
'https://raw.githubusercontent.com/eclipse/che/HEAD/README.md'
);

expect(githubResolver.resolve('http://github.com/eclipse/che').getCloneUrl()).toBe(
expect(githubResolver.resolve('https://github.mycompany.net/user/repo').getContentUrl('README.md')).toBe(
'https://raw.github.mycompany.net/user/repo/HEAD/README.md'
);

expect(githubResolver.resolve('https://github.com/eclipse/che').getCloneUrl()).toBe(
'https://github.com/eclipse/che.git'
);

expect(githubResolver.resolve('https://github.com/eclipse/che').getRepoName()).toBe('che');

expect(githubResolver.resolve('http://github.com/eclipse/che').getBranchName()).toBe('HEAD');
expect(githubResolver.resolve('https://github.com/eclipse/che').getBranchName()).toBe('HEAD');
expect(githubResolver.resolve('https://github.com/eclipse/che/tree/test-branch').getBranchName()).toBe(
'test-branch'
);
Expand Down

0 comments on commit ccfe086

Please sign in to comment.