Skip to content

Commit 263b0c3

Browse files
SamVerschuerensindresorhus
authored andcommitted
Linkify commits - fixes sindresorhus#233 (sindresorhus#234)
1 parent d63f917 commit 263b0c3

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

lib/ui.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const execa = require('execa');
33
const inquirer = require('inquirer');
44
const pTap = require('p-tap');
55
const chalk = require('chalk');
6+
const githubUrlFromGit = require('github-url-from-git');
67
const util = require('./util');
78
const version = require('./version');
89

@@ -30,6 +31,7 @@ function prettyVersionDiff(oldVersion, inc) {
3031
module.exports = options => {
3132
const pkg = util.readPkg();
3233
const oldVersion = pkg.version;
34+
const repositoryUrl = githubUrlFromGit(pkg.repository.url);
3335

3436
console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
3537

@@ -118,7 +120,10 @@ module.exports = options => {
118120
.map(commit => {
119121
const commitParts = commit.match(/^(.+)\s([a-f0-9]{7})$/);
120122

121-
return `- ${commitParts[1]} ${chalk.dim(commitParts[2])}`;
123+
const commitMessage = util.linkifyIssues(repositoryUrl, commitParts[1]);
124+
const commitId = util.linkifyCommit(repositoryUrl, commitParts[2]);
125+
126+
return `- ${commitMessage} ${commitId}`;
122127
})
123128
.join('\n');
124129

lib/util.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22
const readPkgUp = require('read-pkg-up');
3+
const issueRegex = require('issue-regex');
4+
const hyperlinker = require('hyperlinker');
5+
const supportsHyperlinks = require('supports-hyperlinks');
36

47
exports.readPkg = () => {
58
const pkg = readPkgUp.sync().pkg;
@@ -10,3 +13,27 @@ exports.readPkg = () => {
1013

1114
return pkg;
1215
};
16+
17+
exports.linkifyIssues = (url, message) => {
18+
if (!supportsHyperlinks.stdout) {
19+
return message;
20+
}
21+
22+
return message.replace(issueRegex(), issue => {
23+
const issuePart = issue.replace('#', '/issues/');
24+
25+
if (issue.startsWith('#')) {
26+
return hyperlinker(issue, `${url}${issuePart}`);
27+
}
28+
29+
return hyperlinker(issue, `https://github.com/${issuePart}`);
30+
});
31+
};
32+
33+
exports.linkifyCommit = (url, commit) => {
34+
if (!supportsHyperlinks.stdout) {
35+
return commit;
36+
}
37+
38+
return hyperlinker(commit, `${url}/commit/${commit}`);
39+
};

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"node": ">=4"
1010
},
1111
"scripts": {
12-
"test": "xo && ava"
12+
"test": "xo && FORCE_HYPERLINK=1 ava"
1313
},
1414
"files": [
1515
"index.js",
@@ -32,8 +32,11 @@
3232
"chalk": "^2.3.0",
3333
"del": "^3.0.0",
3434
"execa": "^0.8.0",
35+
"github-url-from-git": "^1.5.0",
3536
"has-yarn": "^1.0.0",
37+
"hyperlinker": "^1.0.0",
3638
"inquirer": "^3.0.6",
39+
"issue-regex": "^1.0.0",
3740
"listr": "^0.12.0",
3841
"listr-input": "^0.1.1",
3942
"log-symbols": "^2.1.0",
@@ -45,6 +48,7 @@
4548
"semver": "^5.2.0",
4649
"split": "^1.0.0",
4750
"stream-to-observable": "^0.2.0",
51+
"supports-hyperlinks": "^1.0.1",
4852
"update-notifier": "^2.1.0"
4953
},
5054
"devDependencies": {

test/hyperlinks.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import test from 'ava';
2+
import m from '../lib/util';
3+
4+
test('link issues', t => {
5+
t.is(m.linkifyIssues('https://github.com/unicorn/rainbow', 'Commit message - fixes #4'), 'Commit message - fixes ]8;;https://github.com/unicorn/rainbow/issues/4#4]8;;');
6+
t.is(m.linkifyIssues('https://github.com/unicorn/rainbow', 'Commit message - fixes #3 #4'), 'Commit message - fixes ]8;;https://github.com/unicorn/rainbow/issues/3#3]8;; ]8;;https://github.com/unicorn/rainbow/issues/4#4]8;;');
7+
t.is(m.linkifyIssues('https://github.com/unicorn/rainbow', 'Commit message - fixes foo/bar#4'), 'Commit message - fixes ]8;;https://github.com/foo/bar/issues/4foo/bar#4]8;;');
8+
});
9+
10+
test('link commit', t => {
11+
t.is(m.linkifyCommit('https://github.com/unicorn/rainbow', '5063f8a'), ']8;;https://github.com/unicorn/rainbow/commit/5063f8a5063f8a]8;;');
12+
});

0 commit comments

Comments
 (0)