-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (43 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict"
var url = require("url")
var util = require("util")
var isUrl = require("is-url")
module.exports = function(repo_url) {
var obj = {}
if (!repo_url) return null
var shorthand = repo_url.match(/^([\w-_]+)\/([\w-_\.]+)#?([\w-_\.]+)?$/)
var mediumhand = repo_url.match(/^bitbucket:([\w-_]+)\/([\w-_\.]+)#?([\w-_\.]+)?$/)
var antiquated = repo_url.match(/^git@[\w-_\.]+:([\w-_]+)\/([\w-_\.]+)$/)
if (shorthand) {
obj.user = shorthand[1]
obj.repo = shorthand[2]
obj.branch = shorthand[3] || "master"
} else if (mediumhand) {
obj.user = mediumhand[1]
obj.repo = mediumhand[2]
obj.branch = mediumhand[3] || "master"
} else if (antiquated) {
obj.user = antiquated[1]
obj.repo = antiquated[2].replace(/\.git$/i, "")
obj.branch = "master"
} else {
if (!isUrl(repo_url)) return null
var parsedURL = url.parse(repo_url)
if (parsedURL.hostname != "bitbucket.org") return null
var parts = parsedURL.pathname.match(/^\/([\w-_]+)\/([\w-_\.]+)/)
if (!parts) return null
obj.user = parts[1]
obj.repo = parts[2].replace(/\.git$/i, "")
obj.branch = "master"
}
obj.tarball_url = util.format("https://bitbucket.org/%s/%s/get/%s.tar.gz", obj.user, obj.repo, obj.branch)
obj.api_url = util.format("https://api.bitbucket.org/2.0/repositories/%s/%s", obj.user, obj.repo)
if (obj.branch === "master") {
obj.https_url = util.format("https://bitbucket.org/%s/%s", obj.user, obj.repo)
obj.travis_url = util.format("https://travis-ci.org/%s/%s", obj.user, obj.repo)
} else {
obj.https_url = util.format("https://bitbucket.org/%s/%s/branch/%s", obj.user, obj.repo, obj.branch)
obj.travis_url = util.format("https://travis-ci.org/%s/%s?branch=%s", obj.user, obj.repo, obj.branch)
}
return obj
}