Skip to content

Commit

Permalink
Add support for source links on GitHub enterprise instances (#843)
Browse files Browse the repository at this point in the history
* Add support for source links on GitHub enterprise instances

* Export Repository class from GitHub plugin

Needed for better unit testing

* Let Repository class constructor take urls as parameter

Allows us to more easily unit test the class by passing in an array of strings

* Add unit tests for GitHubRepository class
  • Loading branch information
Tom Ratcliffe authored and aciccarello committed Oct 26, 2018
1 parent c03ac2d commit 02d2882
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/lib/converter/plugins/GitHubPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ParameterType } from '../../utils/options/declaration';
/**
* Stores data of a repository.
*/
class Repository {
export class Repository {
/**
* The root path of this repository.
*/
Expand All @@ -38,34 +38,43 @@ class Repository {
*/
gitHubProject?: string;

/**
* The hostname for this github project.
*
* Defaults to: `github.com` (for normal, public GitHub instance projects)
*
* Or the hostname for an enterprise version of GitHub, e.g. `github.acme.com`
* (if found as a match in the list of git remotes).
*/
gitHubHostname = 'github.com';

/**
* Create a new Repository instance.
*
* @param path The root path of the repository.
*/
constructor(path: string, gitRevision: string) {
constructor(path: string, gitRevision: string, repoLinks: string[]) {
this.path = path;
this.branch = gitRevision || 'master';
ShellJS.pushd(path);

let out = <ShellJS.ExecOutputReturnValue> ShellJS.exec('git ls-remote --get-url', {silent: true});
if (out.code === 0) {
let url: RegExpExecArray | null;
const remotes = out.stdout.split('\n');
for (let i = 0, c = remotes.length; i < c; i++) {
url = /github\.com[:\/]([^\/]+)\/(.*)/.exec(remotes[i]);
if (url) {
this.gitHubUser = url[1];
this.gitHubProject = url[2];
if (this.gitHubProject.substr(-4) === '.git') {
this.gitHubProject = this.gitHubProject.substr(0, this.gitHubProject.length - 4);
}
break;
let url: RegExpExecArray | null;

for (let i = 0, c = repoLinks.length; i < c; i++) {
url = /(github(?:\.[a-z]+)*\.com)[:\/]([^\/]+)\/(.*)/.exec(repoLinks[i]);

if (url) {
this.gitHubHostname = url[1];
this.gitHubUser = url[2];
this.gitHubProject = url[3];
if (this.gitHubProject.substr(-4) === '.git') {
this.gitHubProject = this.gitHubProject.substr(0, this.gitHubProject.length - 4);
}
break;
}
}

out = <ShellJS.ExecOutputReturnValue> ShellJS.exec('git ls-files', {silent: true});
let out = <ShellJS.ExecOutputReturnValue> ShellJS.exec('git ls-files', {silent: true});
if (out.code === 0) {
out.stdout.split('\n').forEach((file) => {
if (file !== '') {
Expand Down Expand Up @@ -106,7 +115,7 @@ class Repository {
}

return [
'https://github.com',
`https://${this.gitHubHostname}`,
this.gitHubUser,
this.gitHubProject,
'blob',
Expand All @@ -132,7 +141,11 @@ class Repository {
if (!out || out.code !== 0) {
return;
}
return new Repository(BasePath.normalize(out.stdout.replace('\n', '')), gitRevision);

let remotesOutput = <ShellJS.ExecOutputReturnValue> ShellJS.exec('git ls-remote --get-url', {silent: true});
let remotes: string[] = (remotesOutput.code === 0) ? remotesOutput.stdout.split('\n') : [];

return new Repository(BasePath.normalize(out.stdout.replace('\n', '')), gitRevision, remotes);
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/githubplugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as github from './../lib/converter/plugins/GitHubPlugin';
import Assert = require('assert');

describe('GitHubRepository', function() {

describe('contructor', function() {
it('must default to github.com hostname', function() {
let repository = new github.Repository('', '', []);

Assert.equal(repository.gitHubHostname, 'github.com');
});

it('must correctly handle an enterprise github URL hostname', function() {
let mockRemotes = [
'[email protected]:joebloggs/foobar.git'
];

let repository = new github.Repository('', '', mockRemotes);

Assert.equal(repository.gitHubHostname, 'github.acme.com');
});
});
});

0 comments on commit 02d2882

Please sign in to comment.