Skip to content

Commit

Permalink
Allow different git URL types in package repository url field
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Janeczko committed Aug 26, 2018
1 parent 22045fa commit aa21bfd
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Allow different git URL types in package repository url field to follow the [package.json standard](https://docs.npmjs.com/files/package.json#repository)

## [0.1.3] - 2018-08-25

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
### Requirements

- Node 8+
- package.json contains an http or https repository URL
- package.json contains a valid git repository URL

### Installation

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"kacl": "kacl.js"
},
"scripts": {
"pretest": "eslint kacl.js src test && ./kacl.js lint",
"pretest": "eslint kacl.js src test",
"test": "nyc --reporter html --reporter text mocha",
"posttest": "./kacl.js lint",
"report": "nyc report --reporter=text-lcov > coverage.lcov",
"preversion": "./kacl.js prerelease",
"version": "./kacl.js release && git add CHANGELOG.md",
Expand All @@ -23,7 +24,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/brightcove/kacl"
"url": "https://github.com/brightcove/kacl.git"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
33 changes: 32 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ const Semver = require('semver');
const { parser, Release, Changelog } = require('keep-a-changelog');
const fs = require('fs');
const chalk = require('chalk');
const url = require('url');

function getPackageUrl (packageUrl) {
packageUrl = packageUrl
//trim git+ from the front of the URL
.replace(/^git\+/, '')
//convert ssh://, ssh://git@, or just git@ to https://
.replace(/^(?:ssh:\/\/(?:git@)?|git@)/, 'https://');

const parsed = url.parse(packageUrl);

if (parsed.pathname) {
parsed.pathname = parsed.pathname
//replace the leading : for ssh paths with / (and pull the port out of the parsed path)
.replace(/\/:(?:(\d+):)?/, (match, port) => {
if (!parsed.port && port) {
parsed.port = port;
parsed.host += ':' + port;
}
return '/';
})
//replace the trailing .git
.replace(/\.git$/, '');
}

return url.format(parsed);
}

module.exports.getPackageUrl = getPackageUrl;

function getPackage () {
let pkg;
Expand All @@ -17,8 +46,10 @@ function getPackage () {
throw new Error('No repository URL found in package.json');
}

pkg.repository.url = getPackageUrl(pkg.repository.url);

if (!/^https?:\/\//.test(pkg.repository.url)) {
throw new Error('Repository URL in package.json must be an http or https URL');
throw new Error('Repository URL in package.json must be a valid URL to a git repository');
}
return pkg;
}
Expand Down
2 changes: 1 addition & 1 deletion test/changelog.conflict.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Did something

[Unreleased]: https://github.com/brightcove/changelog/compare/v0.1.0...HEAD
[Unreleased]: https://github.com/brightcove/kacl/compare/v0.1.0...HEAD
4 changes: 2 additions & 2 deletions test/changelog.expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Did something

[Unreleased]: https://github.com/brightcove/changelog/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/brightcove/changelog/compare/v0.0.1...v0.1.0
[Unreleased]: https://github.com/brightcove/kacl/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/brightcove/kacl/compare/v0.0.1...v0.1.0
2 changes: 1 addition & 1 deletion test/changelog.nounreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Did something

[Unreleased]: https://github.com/brightcove/changelog/compare/v0.1.0...HEAD
[Unreleased]: https://github.com/brightcove/kacl/compare/v0.1.0...HEAD
2 changes: 1 addition & 1 deletion test/changelog.valid.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Did something

[Unreleased]: https://github.com/brightcove/changelog/compare/v0.0.1...HEAD
[Unreleased]: https://github.com/brightcove/kacl/compare/v0.0.1...HEAD
38 changes: 36 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { init, lint, release } = require('../src');
const { getPackageUrl, init, lint, release } = require('../src');
const sinon = require('sinon');
const path = require('path');
const fs = require('fs');
Expand Down Expand Up @@ -36,7 +36,7 @@ function checkPackageJson (func) {
fs.readFileSync.returns('{"repository":{"url": "git://foo"}}');
expect(() => {
func();
}).to.throw(/Repository URL in package\.json must be an http or https URL/);
}).to.throw(/Repository URL in package\.json must be a valid URL to a git repository/);
});
}

Expand Down Expand Up @@ -81,6 +81,40 @@ describe('index', () => {
sinon.restore();
});

describe('getPackageUrl', () => {
it('should do nothing for a standard https url', () => {
expect(getPackageUrl('https://github.com/brightcove/kacl')).to.equal('https://github.com/brightcove/kacl');
});

it('should strip the ending .git from an https url', () => {
expect(getPackageUrl('https://github.com/brightcove/kacl.git')).to.equal('https://github.com/brightcove/kacl');
});

it('should convert a git@ url', () => {
expect(getPackageUrl('git@github.com:brightcove/kacl.git')).to.equal('https://github.com/brightcove/kacl');
});

it('should convert an ssh url', () => {
expect(getPackageUrl('ssh://github.com:brightcove/kacl.git')).to.equal('https://github.com/brightcove/kacl');
});

it('should convert an ssh://git@ url', () => {
expect(getPackageUrl('ssh://git@github.com:brightcove/kacl.git')).to.equal('https://github.com/brightcove/kacl');
});

it('should convert a git+https url', () => {
expect(getPackageUrl('git+https://github.com/brightcove/kacl.git')).to.equal('https://github.com/brightcove/kacl');
});

it('should convert a git+ssh url', () => {
expect(getPackageUrl('git+ssh://git@github.com:brightcove/kacl.git')).to.equal('https://github.com/brightcove/kacl');
});

it('should work with ports on ssh urls', () => {
expect(getPackageUrl('ssh://git@github.com:1234:brightcove/kacl.git')).to.equal('https://github.com:1234/brightcove/kacl');
});
});

describe('init', () => {
checkPackageJson(init);

Expand Down
4 changes: 2 additions & 2 deletions test/pkg.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@brightcove/changelog",
"name": "@brightcove/kacl",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://github.com/brightcove/changelog"
"url": "https://github.com/brightcove/kacl.git"
}
}

0 comments on commit aa21bfd

Please sign in to comment.