Skip to content

Commit b7ff3c8

Browse files
Merge pull request #9 from codefresh-io/master
Add support of filepath
2 parents 1a747cf + e4fba1f commit b7ff3c8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function parse(str) {
3939
var hasBlob = seg[2] === 'blob';
4040
if (hasBlob && !isChecksum(seg[3])) {
4141
obj.branch = seg[3];
42+
if(seg.length > 4)
43+
{
44+
obj.filepath = seg.slice(4).join('/');
45+
}
4246
}
4347

4448
var blob = str.indexOf('blob');
@@ -80,14 +84,20 @@ function parse(str) {
8084
}
8185
}
8286

83-
obj.branch = obj.branch || seg[2] || getBranch(obj.path, obj);
87+
if(!obj.branch){
88+
obj.branch = seg[2] || getBranch(obj.path, obj);
89+
if(seg.length > 3){
90+
obj.filepath = seg.slice(3).join('/');
91+
}
92+
}
8493
var res = {};
8594
res.host = obj.host || 'github.com';
8695
res.owner = obj.owner || null;
8796
res.name = obj.name || null;
8897
res.repo = obj.repo;
8998
res.repository = res.repo;
9099
res.branch = obj.branch;
100+
res.filepath = obj.filepath || null;
91101
return res;
92102
}
93103

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ describe('parse-github-url', function() {
6868
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').branch, 'dev');
6969
});
7070

71+
it('should get the filepath:', function() {
72+
assert.equal(gh('assemble/verb#branch').filepath, null);
73+
assert.equal(gh('[email protected]:assemble/verb.git#0.6.0').filepath, null);
74+
assert.equal(gh('https://github.com/assemble/verb/blob/foo/README.md').filepath, 'README.md');
75+
assert.equal(gh('https://github.com/assemble/verb/blob/foo/').filepath, null);
76+
assert.equal(gh('https://github.com/assemble/verb/blob/foo').filepath, null);
77+
assert.equal(gh('https://github.com/assemble/verb/blob/foo/bar/README.md').filepath, 'bar/README.md');
78+
assert.equal(gh('https://github.com/assemble/verb/tree/dev').filepath, null);
79+
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').filepath, 'README.md');
80+
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/bar/README.md').filepath, 'bar/README.md');
81+
});
82+
7183
it('should use master branch when another branch is not defined:', function() {
7284
assert.equal(gh('assemble/verb').branch, 'master');
7385
assert.equal(gh('git://github.com/foo/bar.git').branch, 'master');

0 commit comments

Comments
 (0)