github-url-to-object
Advanced tools
Comparing version 0.5.3 to 0.7.0
46
index.js
@@ -1,23 +0,45 @@ | ||
var url = require('url') | ||
var isUrl = require('is-url') | ||
"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 | ||
// bail if given a non-github URL | ||
if (isUrl(repo_url) && url.parse(repo_url).hostname != "github.com") return null | ||
var shorthand = repo_url.match(/^([\w-_]+)\/([\w-_\.]+)#?([\w-_\.]+)?$/) | ||
var antiquated = repo_url.match(/^git@[\w-_\.]+:([\w-_]+)\/([\w-_\.]+)$/) | ||
var re = /^(?:https?:\/\/|git:\/\/)?(?:[^@]+@)?(gist.github.com|github.com)[:\/]([^\/]+\/[^\/]+?|[0-9]+)$/ | ||
var match = re.exec(repo_url.replace(/\.git$/, '')); | ||
if (shorthand) { | ||
obj.user = shorthand[1] | ||
obj.repo = shorthand[2] | ||
obj.branch = shorthand[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 != "github.com") 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" | ||
} | ||
// support shorthand URLs | ||
var parts = match ? match[2].split('/') : repo_url.split('/') | ||
obj.tarball_url = util.format("https://api.github.com/repos/%s/%s/tarball/%s", obj.user, obj.repo, obj.branch) | ||
var obj = {user: parts[0], repo: parts[1]} | ||
if (obj.branch === "master") { | ||
obj.https_url = util.format("https://github.com/%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://github.com/%s/%s/tree/%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) | ||
} | ||
obj.tarball_url = "https://api.github.com/repos/" + obj.user + "/" + obj.repo + "/tarball" | ||
obj.https_url = "https://github.com/" + obj.user + "/" + obj.repo | ||
obj.travis_url = "https://travis-ci.org/" + obj.user + "/" + obj.repo | ||
return obj | ||
} |
{ | ||
"name": "github-url-to-object", | ||
"version": "0.5.3", | ||
"version": "0.7.0", | ||
"description": "Extract user, repo, and other interesting properties from GitHub URLs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,2 +32,3 @@ # github-url-to-object [![Build Status](https://travis-ci.org/zeke/github-url-to-object.png?branch=master)](https://travis-ci.org/zeke/github-url-to-object) | ||
repo: 'business', | ||
branch: 'master', | ||
https_url: 'https://github.com/monkey/business', | ||
@@ -39,2 +40,16 @@ tarball_url: 'https://api.github.com/repos/monkey/business/tarball' | ||
The shorthand format lets you specify a branch: | ||
```js | ||
gh('monkey/business#nachos') | ||
{ | ||
user: 'monkey', | ||
repo: 'business', | ||
branch: 'nachos', | ||
https_url: 'https://github.com/monkey/business/tree/nachos', | ||
tarball_url: 'https://api.github.com/repos/monkey/business/tarball/nachos' | ||
travis_url: 'https://travis-ci.org/monkey/business', | ||
} | ||
``` | ||
If you provide a non-github URL or a falsy value, you'll get `null`. | ||
@@ -41,0 +56,0 @@ |
@@ -7,2 +7,85 @@ var mocha = require("mocha") | ||
describe("shorthand", function(){ | ||
it("supports user/repo style", function(){ | ||
var obj = gh("user/repo#branch") | ||
assert.equal(obj.user, 'user') | ||
assert.equal(obj.repo, 'repo') | ||
}) | ||
it("supports user/repo#branch style", function(){ | ||
var obj = gh("user/repo#branch") | ||
assert.equal(obj.user, 'user') | ||
assert.equal(obj.repo, 'repo') | ||
assert.equal(obj.branch, 'branch') | ||
}) | ||
it("defaults to master branch", function(){ | ||
var obj = gh("user/repo") | ||
assert.equal(obj.user, 'user') | ||
assert.equal(obj.repo, 'repo') | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
}) | ||
describe("oldschool", function(){ | ||
it("supports git@ URLs", function() { | ||
var obj = gh("git@github.com:heroku/heroku-flags.git") | ||
assert.equal(obj.user, 'heroku') | ||
assert.equal(obj.repo, 'heroku-flags') | ||
}) | ||
it("defaults to master branch", function() { | ||
var obj = gh("git@github.com:heroku/heroku-flags.git") | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
it("supports git:// URLs", function() { | ||
var obj = gh("git://github.com/foo/bar.git") | ||
assert.equal(obj.user, 'foo') | ||
assert.equal(obj.repo, 'bar') | ||
}) | ||
it("defaults to master branch", function() { | ||
var obj = gh("git://github.com/foo/bar.git") | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
}) | ||
describe("http", function(){ | ||
it("supports http URLs", function() { | ||
var obj = gh("http://github.com/zeke/outlet.git") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'outlet') | ||
}) | ||
it("supports https URLs", function() { | ||
var obj = gh("https://github.com/zeke/outlet.git") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'outlet') | ||
}) | ||
it("supports deep URLs", function() { | ||
var obj = gh("https://github.com/zeke/ruby-rails-sample/blob/b1e1000fedb6ca448dd78702de4fc78dedfee48c/app.json") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'ruby-rails-sample') | ||
}) | ||
it("doesn't require .git extension", function() { | ||
var obj = gh("https://github.com/zeke/outlet") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'outlet') | ||
}) | ||
it("defaults to master branch", function() { | ||
var obj = gh("https://github.com/zeke/outlet") | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
}) | ||
describe("properties", function() { | ||
@@ -12,23 +95,27 @@ var obj | ||
before(function(){ | ||
obj = gh("git://github.com/foo-master/party-time.git") | ||
obj = gh("zeke/ord") | ||
}) | ||
it("user", function() { | ||
assert.equal(obj.user, "foo-master") | ||
assert.equal(obj.user, "zeke") | ||
}) | ||
it("repo", function() { | ||
assert.equal(obj.repo, "party-time") | ||
assert.equal(obj.repo, "ord") | ||
}) | ||
it("branch", function() { | ||
assert.equal(obj.branch, "master") | ||
}) | ||
it("tarball_url", function() { | ||
assert.equal(obj.tarball_url, "https://api.github.com/repos/foo-master/party-time/tarball") | ||
assert.equal(obj.tarball_url, "https://api.github.com/repos/zeke/ord/tarball/master") | ||
}) | ||
it("https_url", function() { | ||
assert.equal(obj.https_url, "https://github.com/foo-master/party-time") | ||
assert.equal(obj.https_url, "https://github.com/zeke/ord") | ||
}) | ||
it("travis_url", function() { | ||
assert.equal(obj.travis_url, "https://travis-ci.org/foo-master/party-time") | ||
assert.equal(obj.travis_url, "https://travis-ci.org/zeke/ord") | ||
}) | ||
@@ -38,50 +125,40 @@ | ||
it("handles URLs without .git at the end", function() { | ||
var obj = gh("https://github.com/zeke/outlet") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'outlet') | ||
}) | ||
describe("branch other than master", function() { | ||
it("handles http URLs", function() { | ||
var obj = gh("http://github.com/zeke/outlet.git") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'outlet') | ||
}) | ||
var obj | ||
it("handles https URLs", function() { | ||
var obj = gh("https://github.com/zeke/outlet.git") | ||
assert.equal(obj.user, 'zeke') | ||
assert.equal(obj.repo, 'outlet') | ||
}) | ||
before(function(){ | ||
obj = gh("zeke/ord#experiment") | ||
}) | ||
it("handles git URLs", function() { | ||
var obj = gh("git://github.com/foo/bar.git") | ||
assert.equal(obj.user, 'foo') | ||
assert.equal(obj.repo, 'bar') | ||
}) | ||
it("applies to tarball_url", function() { | ||
assert.equal(obj.tarball_url, "https://api.github.com/repos/zeke/ord/tarball/experiment") | ||
}) | ||
it("handles shorthand user/repo paths", function() { | ||
var obj = gh("foo/bar") | ||
assert.equal(obj.user, 'foo') | ||
assert.equal(obj.repo, 'bar') | ||
}) | ||
it("applies to https_url", function() { | ||
assert.equal(obj.https_url, "https://github.com/zeke/ord/tree/experiment") | ||
}) | ||
it("handles git@ URLs", function() { | ||
var obj = gh("git@github.com:heroku/heroku-flags.git") | ||
assert.equal(obj.user, 'heroku') | ||
assert.equal(obj.repo, 'heroku-flags') | ||
}) | ||
it("applies to travis_url", function() { | ||
assert.equal(obj.travis_url, "https://travis-ci.org/zeke/ord?branch=experiment") | ||
}) | ||
it("returns null if url is falsy", function() { | ||
assert.equal(gh(), null) | ||
assert.equal(gh(null), null) | ||
assert.equal(gh(undefined), null) | ||
assert.equal(gh(""), null) | ||
}) | ||
it("returns null for non-github URLs", function() { | ||
var obj = gh("https://bitbucket.com/other/thing") | ||
assert.equal(obj, null) | ||
describe("failure", function(){ | ||
it("returns null if url is falsy", function() { | ||
assert.equal(gh(), null) | ||
assert.equal(gh(null), null) | ||
assert.equal(gh(undefined), null) | ||
assert.equal(gh(""), null) | ||
}) | ||
it("returns null for non-github URLs", function() { | ||
var obj = gh("https://bitbucket.com/other/thing") | ||
assert.equal(obj, null) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7665
158
65