Skip to content
This repository was archived by the owner on Apr 30, 2019. It is now read-only.

Commit 15753b1

Browse files
committed
Merge pull request #223 from nlunets/master
[Feature] Support for enterprise Github repository
2 parents d780369 + 14914e1 commit 15753b1

File tree

8 files changed

+69
-21
lines changed

8 files changed

+69
-21
lines changed

src/getContent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ function getContent(options): Promise<any> {
7171
var ret: any = {
7272
repo: api.context.config.repo,
7373
ref: api.context.config.ref,
74+
githubHost: api.context.config.githubHost,
7475
count: content.length,
7576
time: new Date().toISOString()
7677
};
7778
ret.urls = {
78-
def: 'https://github.com/' + ret.repo + '/blob/' + ret.ref + '/{path}'
79+
def: 'https://' + ret.githubHost + '/' + ret.repo + '/blob/' + ret.ref + '/{path}'
7980
};
8081
ret.content = content;
8182
return ret;

src/git/GithubRepoConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="./_ref.d.ts" />
22

33
interface GithubRepoConfig {
4+
githubHost: string;
45
repoOwner: string;
56
repoProject: string;
67
}

src/git/GithubURLs.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,50 @@ class GithubURLs extends URLManager {
1616
private _apiBase: string = 'https://api.github.com';
1717
private _api: string = 'https://api.github.com/repos/{owner}/{project}';
1818
private _raw: string = 'https://raw.githubusercontent.com/{owner}/{project}';
19+
20+
private _enterpriseBase: string = 'https://{githubHost}/{owner}/{project}';
21+
private _enterpriseApiBase: string = 'https://{githubHost}/api/v3';
22+
private _enterpriseApi: string = 'https://{githubHost}/api/v3/repos/{owner}/{project}';
23+
private _enterpriseRaw: string = 'https://{githubHost}/{owner}/{project}/raw';
1924
private _repo: GithubRepo;
2025

2126
constructor(repo: GithubRepo) {
2227
super();
2328
assertVar(repo, 'object', 'repo');
2429

2530
this._repo = repo;
31+
var base: string = this._base;
32+
var raw: string = this._raw;
33+
var api: string = this._api;
34+
var apiBase: string = this._apiBase;
35+
if (this._repo.config.githubHost !== 'github.com') {
36+
// We are working with an enterprise github
37+
base = this._enterpriseBase;
38+
raw = this._enterpriseRaw;
39+
api = this._enterpriseApi;
40+
apiBase = this._enterpriseApiBase;
41+
}
2642
// externalise later
27-
this.addTemplate('base', this._base);
43+
this.addTemplate('base', base);
2844

29-
this.addTemplate('raw', this._raw);
30-
this.addTemplate('rawFile', this._raw + '/{+ref}/{+path}');
45+
this.addTemplate('raw', raw);
46+
this.addTemplate('rawFile', raw + '/{+ref}/{+path}');
3147

32-
this.addTemplate('htmlFile', this._base + '/blob/{ref}/{+path}');
48+
this.addTemplate('htmlFile', base + '/blob/{ref}/{+path}');
3349

34-
this.addTemplate('api', this._api);
35-
this.addTemplate('apiTree', this._api + '/git/trees/{tree}?recursive={recursive}');
36-
this.addTemplate('apiBranch', this._api + '/branches/{branch}');
37-
this.addTemplate('apiBranches', this._api + '/branches');
38-
this.addTemplate('apiCommit', this._api + '/commits/{commit}');
39-
this.addTemplate('apiPathCommits', this._api + '/commits?path={path}');
40-
this.addTemplate('apiBlob', this._api + '/git/blobs/{blob}');
41-
this.addTemplate('rateLimit', this._apiBase + '/rate_limit');
50+
this.addTemplate('api', api);
51+
this.addTemplate('apiTree', api + '/git/trees/{tree}?recursive={recursive}');
52+
this.addTemplate('apiBranch', api + '/branches/{branch}');
53+
this.addTemplate('apiBranches', api + '/branches');
54+
this.addTemplate('apiCommit', api + '/commits/{commit}');
55+
this.addTemplate('apiPathCommits', api + '/commits?path={path}');
56+
this.addTemplate('apiBlob', api + '/git/blobs/{blob}');
57+
this.addTemplate('rateLimit', apiBase + '/rate_limit');
4258
}
4359

4460
getURL(id: string, vars?: any): string {
4561
this.setVars({
62+
githubHost: this._repo.config.githubHost,
4663
owner: this._repo.config.repoOwner,
4764
project: this._repo.config.repoProject
4865
});

src/spec/git/GithubURLs.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ describe('GithubRepo / GithubURLs', () => {
3636
});
3737
it('should throw on bad params', () => {
3838
assert.throws(() => {
39-
repo = new GithubRepo({repoOwner: null, repoProject: null}, null, null);
39+
repo = new GithubRepo({repoOwner: null, repoProject: null, githubHost: null}, null, null);
4040
});
4141
});
4242
it('should be constructor', () => {
43-
repo = new GithubRepo({repoOwner: 'foo', repoProject: 'bar'}, 'baz', gitTest.opts);
43+
repo = new GithubRepo({repoOwner: 'foo', repoProject: 'bar', githubHost: 'github.com'}, 'baz', gitTest.opts);
4444
urls = repo.urls;
4545
assert.ok(urls, 'instance');
4646
});
@@ -56,7 +56,7 @@ describe('GithubRepo / GithubURLs', () => {
5656
});
5757
});
5858
it('should return replaced urls', () => {
59-
urls = new GithubRepo({repoOwner: 'foo', repoProject: 'bar'}, 'baz', gitTest.opts).urls;
59+
urls = new GithubRepo({repoOwner: 'foo', repoProject: 'bar', githubHost: 'github.com'}, 'baz', gitTest.opts).urls;
6060
var api = 'https://api.github.com/repos/foo/bar';
6161
var raw = 'https://raw.githubusercontent.com/foo/bar';
6262
var base = 'https://github.com/foo/bar';
@@ -66,7 +66,7 @@ describe('GithubRepo / GithubURLs', () => {
6666
assert.strictEqual(urls.rawFile('2ece23298f06d9fb45772fdb1d38086918c80f44', 'sub/folder/file.txt'), rawFile, 'rawFile');
6767
});
6868
it('should return correctly replaced urls if repoConfig is modified after repo creation', () => {
69-
var repoConfig = {repoOwner: 'foo', repoProject: 'bar'};
69+
var repoConfig = {repoOwner: 'foo', repoProject: 'bar', githubHost: 'github.com'};
7070
urls = new GithubRepo(repoConfig, 'baz', gitTest.opts).urls;
7171
repoConfig.repoOwner = 'correctOwner';
7272
repoConfig.repoProject = 'correctProject';
@@ -77,9 +77,19 @@ describe('GithubRepo / GithubURLs', () => {
7777
assert.strictEqual(urls.base(), base, 'base');
7878
});
7979
it('should return no trailing slash', () => {
80-
urls = new GithubRepo({repoOwner: 'foo', repoProject: 'bar'}, 'baz', gitTest.opts).urls;
80+
urls = new GithubRepo({repoOwner: 'foo', repoProject: 'bar', githubHost: 'github.com'}, 'baz', gitTest.opts).urls;
8181
assert.notMatch(urls.apiBranches(), /\/$/, 'apiBranches');
8282
assert.notMatch(urls.apiBranch('abc'), /\/$/, 'apiBranch');
8383
});
84+
it('should handle enterprise github urls', () => {
85+
urls = new GithubRepo({repoOwner: 'foo', repoProject: 'bar', githubHost: 'github.mycompany.com'}, 'baz', gitTest.opts).urls;
86+
var api = 'https://github.mycompany.com/api/v3/repos/foo/bar';
87+
var raw = 'https://github.mycompany.com/foo/bar/raw';
88+
var base = 'https://github.mycompany.com/foo/bar';
89+
var rawFile = raw + '/2ece23298f06d9fb45772fdb1d38086918c80f44/sub/folder/file.txt';
90+
assert.strictEqual(urls.api(), api, 'api');
91+
assert.strictEqual(urls.base(), base, 'base');
92+
assert.strictEqual(urls.rawFile('2ece23298f06d9fb45772fdb1d38086918c80f44', 'sub/folder/file.txt'), rawFile, 'rawFile');
93+
});
8494
});
8595
});

src/tsd/context/Config.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Config implements GithubRepoConfig {
4141
path: string;
4242
version: string;
4343
repo: string;
44+
githubHost: string;
4445
ref: string;
4546
stats: boolean;
4647
bundle: string;
@@ -61,6 +62,7 @@ class Config implements GithubRepoConfig {
6162
this.path = Const.typingsDir;
6263
this.version = Const.configVersion;
6364
this.repo = Const.definitelyRepo;
65+
this.githubHost = Const.githubHost;
6466
this.ref = Const.mainBranch;
6567
this.stats = Const.statsDefault;
6668
this.otherFields = {};
@@ -147,6 +149,11 @@ class Config implements GithubRepoConfig {
147149
var json = this.otherFields;
148150
json.version = this.version;
149151
json.repo = this.repo;
152+
if (this.githubHost !== Const.githubHost) {
153+
// only write to config if the host is not github.com
154+
json.githubHost = this.githubHost;
155+
}
156+
150157
json.ref = this.ref;
151158
json.path = this.path;
152159

@@ -191,6 +198,11 @@ class Config implements GithubRepoConfig {
191198
this.path = json.path;
192199
this.version = json.version;
193200
this.repo = json.repo;
201+
this.githubHost = json.githubHost;
202+
if (!this.githubHost) {
203+
// when migrating from file that do not have this parameter make sure upgrade won't fail
204+
this.githubHost = Const.githubHost;
205+
}
194206
this.ref = json.ref;
195207
this.bundle = json.bundle;
196208
this.stats = (typeOf.isBoolean(json.stats) ? json.stats : Const.statsDefault);
@@ -205,9 +217,9 @@ class Config implements GithubRepoConfig {
205217
});
206218
}
207219

208-
var reservedFields = ['path', 'version', 'repo', 'ref', 'bundle', 'stats', 'installed'];
209-
var otherFieldKeys = Object.keys(json).filter(function(key) { return reservedFields.indexOf(key) === -1; } );
210-
this.otherFields = otherFieldKeys.reduce(function(fields, key) {
220+
var reservedFields = ['path', 'version', 'repo', 'githubHost', 'ref', 'bundle', 'stats', 'installed'];
221+
var otherFieldKeys = Object.keys(json).filter(function (key) { return reservedFields.indexOf(key) === -1; });
222+
this.otherFields = otherFieldKeys.reduce(function (fields, key) {
211223
fields[key] = json[key];
212224
return fields;
213225
}, {});

src/tsd/context/Const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Const = {
1414

1515
configVersion: 'v4',
1616
definitelyRepo: 'borisyankov/DefinitelyTyped',
17+
githubHost: 'github.com',
1718
mainBranch: 'master',
1819
statsDefault: true,
1920

src/tsd/schema/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ var schema = Joi.object({
2121
.default(Const.definitelyRepo)
2222
.required()
2323
.description('github repository "owner/name"'),
24+
githubHost: Joi
25+
.string()
26+
.default(Const.githubHost)
27+
.optional()
28+
.description('github url, used to specify github enteprise url'),
2429
ref: Joi
2530
.string().regex(/^[\w\.-]+(?:\/[\w\.-]+)*$/)
2631
.default(Const.mainBranch)

test/spec/git/fixtures/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"repo": {
33
"repoOwner": "borisyankov",
44
"repoProject": "DefinitelyTyped",
5+
"githubHost": "github.com",
56
"ref": "master"
67
},
78
"data": {

0 commit comments

Comments
 (0)