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

Add support for source links on GitHub enterprise instances #843

Merged
merged 5 commits into from
Oct 26, 2018
Merged
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
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');
});
});
});