Skip to content

Commit

Permalink
GitHub: Add canonicalization support for {user}/{repo}/issues/{number}
Browse files Browse the repository at this point in the history
And the /pull/ form as well.  Juan requested both of these [1,2],
presumably to allow for easier copy-pasting.  In both cases, redirect
them to the canonical-URI for the issue/pull in question.

When the user does enter one of these non-canonical keys, use an
onEnter handler to redirect them to the canonical path.

[1]: jbenet#4
[2]: jbenet#5
  • Loading branch information
wking committed Nov 25, 2016
1 parent e7c3c7c commit fe297ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
13 changes: 12 additions & 1 deletion webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export class DepGraphView extends Component {
}
}

function enterGraphView(nextState, replace) {
const splat = nextState.params.splat;
const canonicalKey = CanonicalKey(splat);
const canonicalPath = canonicalKey.replace(/#/g, '/');
if (splat === canonicalKey || splat === canonicalPath) {
return;
}
replace('/http/' + canonicalPath);
}

class App extends Component {
render() {
Canonicalizers['dummy'] = CanonicalDummyHostKey;
Expand All @@ -33,7 +43,8 @@ class App extends Component {
</div>
<Router history={hashHistory}>
<Route path="/" component={Home} />
<Route path="/http/*" component={DepGraphView} />
<Route path="/http/*" component={DepGraphView}
onEnter={enterGraphView} />
</Router>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/DummyHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import DepCard from './DepCard';
var calls = {};

export function CanonicalDummyHostKey(key) {
var match = /^dummy\/(.*)\/(.*)[#\/]([0-9]*)$/.exec(key);
var match = /^dummy\/([^\/]+)\/([^\/]+)[#\/]([0-9]*)$/.exec(key);
if (!match) {
throw new Error('unrecognized dummy key: ' + key);
}
Expand Down
12 changes: 9 additions & 3 deletions webapp/src/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import DepCard from './DepCard';
const gh = new GitHub(); /* unauthenticated client */

export function CanonicalGitHubKey(key) {
var match = /^github\.com\/(.*)\/(.*)[#\/]([0-9]*)$/.exec(key);
var match = /^github\.com\/([^\/]+)\/([^\/]+)(\/|\/issues\/|\/pull\/)(#?)([0-9]*)$/.exec(key);
if (!match) {
throw new Error('unrecognized GitHub key: ' + key);
}
var user = match[1];
var repo = match[2];
var number = parseInt(match[3], 10);
var spacer = match[3];
var hash = match[4]
var number = parseInt(match[5], 10);
if (spacer && hash) {
throw new Error('unrecognized GitHub key: ' + key);
}

return 'github.com/' + user + '/' + repo + '#' + number;
}

function GetGitHubNode(key) {
var match = /^github\.com\/(.*)\/(.*)#([0-9]*)$/.exec(key);
var match = /^github\.com\/([^\/]*)\/([^\/]*)#([0-9]*)$/.exec(key);
if (!match) {
throw new Error('unrecognized GitHub key: ' + key);
}
Expand Down
26 changes: 25 additions & 1 deletion webapp/src/GitHub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,36 @@ jest.mock('github-api', () => {

import GetGitHubNode, { CanonicalGitHubKey } from './GitHub';

it('canonical key uses a hash sign', () => {
it('canonical key found for slash path', () => {
expect(
CanonicalGitHubKey('github.com/jbenet/depviz/1')
).toBe('github.com/jbenet/depviz#1');
});

it('canonical key found for issues path', () => {
expect(
CanonicalGitHubKey('github.com/jbenet/depviz/issues/1')
).toBe('github.com/jbenet/depviz#1');
});

it('canonical key found for pull path', () => {
expect(
CanonicalGitHubKey('github.com/jbenet/depviz/pull/1')
).toBe('github.com/jbenet/depviz#1');
});

it('spacer hash key fails canonicalization', () => {
expect(() =>
CanonicalGitHubKey('github.com/jbenet/depviz/#1')
).toThrowError('unrecognized GitHub key: github.com/jbenet/depviz/#1');
});

it('spacer without repo fails canonicalization', () => {
expect(() =>
CanonicalGitHubKey('github.com/jbenet/#1')
).toThrowError('unrecognized GitHub key: github.com/jbenet/#1');
});

it('example.com host fails canonicalization', () => {
expect(() =>
CanonicalGitHubKey('example.com/jbenet/depviz#1')
Expand Down

0 comments on commit fe297ae

Please sign in to comment.