Skip to content

Commit 8923593

Browse files
Fix redirect. (#17)
* Fix redirect to GitHub. * Write tests using jest. Co-authored-by: Adi Roiban <[email protected]>
1 parent 43ffdfa commit 8923593

8 files changed

+139
-67
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ _site/
22
.sass-cache/
33
.jekyll-cache/
44
.jekyll-metadata
5+
venv/
6+
nodeenv/
7+
node-env/
8+
node_modules/
9+
# We don't want to commit all the pinned dependencies.
10+
package-lock.json

404.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ <h1 class="tw-m-0 tw-text-white">404</h1>
1818
<h1>Page not found on this site.</h1>
1919
<h2 id="js-redirection" class="tw-hidden tw-font-medium">
2020
Redirecting to moved
21-
<a href="#" class="tw-text-highlight">page</a>.
21+
<a id="js-redirection-link" href="#" class="tw-text-highlight">page</a>.
2222
</h2>
2323
<h2 class="tw-font-medium tw-m-0">
2424
Go to
@@ -27,14 +27,15 @@ <h2 class="tw-font-medium tw-m-0">
2727

2828
<noscript>
2929
<h2 class="tw-font-medium">
30-
Please enable JavaScript for this site to take advantage of automatic redirection.
30+
You need to enable JavaScript for this site to take advantage of automatic redirection.
3131
</h2>
3232
</noscript>
3333
</div>
3434

3535
<!-- Required vendor scripts (Do not remove) -->
36-
<script type="text/javascript" src="/vendor/js/jquery-v3.5.1.min.js"></script>
37-
<script type="module" src="/assets/js/redirect.js"></script>
36+
<script type="text/javascript" src="/assets/js/node-shim.js"></script>
37+
<script type="text/javascript" src="/assets/js/redirect_trac_tickets.js"></script>
38+
<script type="text/javascript" src="/assets/js/redirect.js"></script>
3839
</body>
3940

4041
</html>

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,33 @@ You can run a local test server via `$ python3 test_server.py`.
1212
It is designed to replicate the same 404 behaviour as GitHub Pages.
1313

1414

15-
## Tools used
15+
## Tools
1616

1717
This project is using [Tailwind v3](https://tailwindcss.com/docs/) in the
1818
*404.html* page.
1919
Use the `tw-` prefix with Tailwind CSS classes.
2020

2121
Node/npm is required to update the */build/css/tailwind.css* file.
2222

23+
You can get nodejs on your system via Python as:
24+
25+
```
26+
virtualenv venv
27+
. venv/bin/activate
28+
pip install nodeenv
29+
nodeenv node-env
30+
. node-env/bin/activate
31+
```
32+
## JS devlopment
33+
34+
`jest` is used for the test suite.
35+
36+
```
37+
./node_modules/.bin/jest
38+
```
39+
40+
## CSS development
41+
2342
Use this command when developing for the CSS file to be updated automatically
2443
with your Tailwind classes:
2544

@@ -28,6 +47,7 @@ npm run dev
2847
```
2948

3049
To minify the file run:
50+
3151
```
3252
npm run build
3353
```

assets/js/node-shim.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Helpers to get the same code over nodejs and a webbrowser
3+
//
4+
// This is to have nodejs and browser code without webpack or other tools.
5+
//
6+
if (typeof exports === 'undefined') {
7+
var require = function(node_path, browser_name) {
8+
return this[browser_name]
9+
}
10+
}

assets/js/redirect.js

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,39 @@
44
Show link to redirect to correct page and redirect.
55
*/
66

7-
// This is the Trac to GitHub issues mapping.
8-
// Stored in a separate file for convenience.
9-
import {migrated_tickets} from "./redirect_rules.mjs"
7+
(function(exports){
8+
9+
// This is poor man's hack to have the same code available in browser and
10+
// nodejs without webpack or other tools.
11+
const redirect_trac_tickets = require(
12+
'./redirect_trac_tickets', 'redirect_trac_tickets')
13+
1014

1115
// Redirection rules.
1216
// This is a mapping from a regex to a redirection URL.
1317
const regex_redirects = [
14-
['/trac$', '/'],
15-
['/trac/$', '/'],
16-
['/trac/wiki$', '/'],
17-
['/trac/wiki/$', '/'],
18-
['/trac/wiki/WikiStart$', '/'],
19-
['/trac/timeline$', 'https://github.com/twisted/twisted/pulse'],
20-
['/trac/roadmap$', 'https://github.com/twisted//twisted/milestones'],
21-
['/trac/newticket$', 'https://github.com/twisted//twisted/issues/new'],
22-
['/trac/search$', 'https://github.com/twisted//twisted/issues'],
23-
['/trac/report.*', 'https://github.com/twisted/twisted/issues'],
24-
['/trac/wiki/(.+)', 'https://github.com/twisted/trac-wiki-archive/blob/trunk/$1.mediawiki'],
18+
['.*/trac$', '/'],
19+
['.*/trac/$', '/'],
20+
['.*/trac/wiki$', '/'],
21+
['.*/trac/wiki/$', '/'],
22+
['.*/trac/wiki/WikiStart$', '/'],
23+
['.*/trac/timeline$', 'https://github.com/twisted/twisted/pulse'],
24+
['.*/trac/roadmap$', 'https://github.com/twisted/twisted/milestones'],
25+
['.*/trac/newticket$', 'https://github.com/twisted/twisted/issues/new'],
26+
['.*/trac/search.*', 'https://github.com/twisted/twisted/issues'],
27+
['.*/trac/report.*', 'https://github.com/twisted/twisted/issues'],
28+
['.*/trac/wiki/(.+)', 'https://github.com/twisted/trac-wiki-archive/blob/trunk/$1.mediawiki'],
2529
];
2630

27-
var path = window.location.pathname
28-
var path_simple = stripTrailingSlash(path)
29-
30-
if (path.match('/trac/ticket/.+')) {
31-
var gh_issues_base_url = 'https://github.com/twisted/twisted/issues/'
32-
33-
var r = path_simple.split('/')
34-
var trac_id = r[r.length - 1]
35-
36-
var new_id = getFirstMatch(migrated_tickets, parseInt(trac_id))
31+
function getRegexRedirectPath(regex_redirects, old_url) {
32+
var matching_pair = regex_redirects.find(function(pair) {
33+
var regex_path = new RegExp(pair[0])
34+
return old_url.match(regex_path)
35+
})
3736

38-
// #comment:2 -> #note_2
39-
var new_anchor = ''
40-
var anchor = window.location.href.match(/#comment:[0-9]+/gi)
41-
if (anchor) {
42-
new_anchor = '#note_' + anchor[0].match(/[0-9]+/)[0]
37+
if(matching_pair) {
38+
return old_url.replace(new RegExp(matching_pair[0]), matching_pair[1])
4339
}
44-
45-
var new_url = gh_issues_base_url + new_id + new_anchor
46-
setLink(new_url)
47-
window.location = new_url
48-
}
49-
50-
51-
goToRegexRedirectPath(regex_redirects, path_simple)
52-
53-
54-
function goToRegexRedirectPath(regex_redirects, path_simple) {
55-
regex_redirects.forEach(function(pair) {
56-
var regex_path = new RegExp(pair[0], 'gi')
57-
58-
if (path_simple.match(regex_path)) {
59-
new_url = path_simple.replace(regex_path, pair[1])
60-
setLink(new_url)
61-
window.location = new_url
62-
}
63-
})
6440
}
6541

6642
function getFirstMatch(rule_map, key) {
@@ -70,14 +46,41 @@ function getFirstMatch(rule_map, key) {
7046
return false
7147
}
7248

73-
function stripTrailingSlash(str) {
74-
if(str.substr(-1) === '/') {
75-
return str.substr(0, str.length - 1);
49+
const twisted_url = 'https://github.com/twisted'
50+
51+
exports.getMigratedURL = (old_url) => {
52+
var ticket_regex_path = new RegExp('/trac/ticket/(.+)')
53+
if (old_url.match(ticket_regex_path)) {
54+
55+
var new_url = twisted_url + '/twisted/issues/'
56+
var trac_id = old_url.match(ticket_regex_path)[1]
57+
var new_id = getFirstMatch(
58+
redirect_trac_tickets.trac_to_github, parseInt(trac_id))
59+
60+
// #comment:2 -> #note_2
61+
var new_anchor = ''
62+
var anchor = old_url.match(/#comment:[0-9]+/gi)
63+
if (anchor) {
64+
new_anchor = '#note_' + anchor[0].match(/[0-9]+/)[0]
65+
}
66+
67+
if (new_id) {
68+
new_url = new_url + new_id + new_anchor
69+
return new_url
70+
}
7671
}
77-
return str;
72+
return getRegexRedirectPath(regex_redirects, old_url)
7873
}
7974

80-
function setLink(url) {
81-
$('#js-redirection a').attr('href', url)
82-
$('#js-redirection').removeClass('tw-hidden')
75+
}(typeof exports === 'undefined' ? this.redirect = {} : exports))
76+
77+
// Run the redirection when loaded from the browser.
78+
if (typeof exports === 'undefined') {
79+
80+
var new_url = redirect.getMigratedURL(window.location.href)
81+
if (new_url) {
82+
document.getElementById('js-redirection-link').setAttribute('href', new_url)
83+
document.getElementById('js-redirection').classList.remove('tw-hidden')
84+
window.location = new_url
85+
}
8386
}

assets/js/redirect.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const redirect = require('./redirect');
2+
3+
4+
test('getMigratedURL', () => {
5+
var domain = 'https://twisted.org'
6+
var github_issues = 'https://github.com/twisted/twisted/issues/'
7+
8+
expect(redirect.getMigratedURL(domain+'/trac/ticket/1234')).toEqual(github_issues+'10828')
9+
expect(redirect.getMigratedURL(domain+'/trac/ticket/3232')).toEqual(github_issues+'3232')
10+
expect(redirect.getMigratedURL(domain+'/trac/ticket/10000#comment:2')).toEqual(github_issues+'10000#note_2')
11+
expect(redirect.getMigratedURL(domain+'/trac')).toEqual('/')
12+
expect(redirect.getMigratedURL(domain+'/trac/')).toEqual('/')
13+
expect(redirect.getMigratedURL(domain+'/trac/wiki')).toEqual('/')
14+
expect(redirect.getMigratedURL(domain+'/trac/wiki/')).toEqual('/')
15+
expect(redirect.getMigratedURL(domain+'/trac/wiki/WikiStart')).toEqual('/')
16+
expect(redirect.getMigratedURL(domain+'/trac/timeline')).toEqual('https://github.com/twisted/twisted/pulse')
17+
expect(redirect.getMigratedURL(domain+'/trac/roadmap')).toEqual('https://github.com/twisted/twisted/milestones')
18+
expect(redirect.getMigratedURL(domain+'/trac/newticket')).toEqual('https://github.com/twisted/twisted/issues/new')
19+
expect(redirect.getMigratedURL(domain+'/trac/search')).toEqual('https://github.com/twisted/twisted/issues')
20+
expect(redirect.getMigratedURL(domain+'/trac/search?q=query')).toEqual('https://github.com/twisted/twisted/issues')
21+
expect(redirect.getMigratedURL(domain+'/trac/report')).toEqual('https://github.com/twisted/twisted/issues')
22+
expect(redirect.getMigratedURL(domain+'/trac/report/1')).toEqual('https://github.com/twisted/twisted/issues')
23+
expect(redirect.getMigratedURL(
24+
domain+'/trac/wiki/TwistedDevelopment')).toEqual(
25+
'https://github.com/twisted/trac-wiki-archive/blob/trunk/TwistedDevelopment.mediawiki')
26+
})

assets/js/redirect_rules.mjs renamed to assets/js/redirect_trac_tickets.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
21
/*
3-
This files contains the mapping of Trac Ticket IDs as migrated
4-
to GitHub Issues ID for the tickets that don't have the same IDs.
2+
This file contains the mapping of Trac Ticket IDs as migrated
3+
to GitHub Issues ID.
54
6-
If an ID is not in this list, then both Trac and GitHub have same IDs.
5+
If an ID is not in this list, then it was not migrated.
76
*/
8-
export const migrated_tickets = new Map([
7+
(function(exports){
8+
9+
exports.trac_to_github = new Map([
910
[1741, 1741],
1011
[1742, 1742],
1112
[1743, 1743],
@@ -9827,3 +9828,5 @@ export const migrated_tickets = new Map([
98279828
[246, 11559],
98289829
[1647, 11560],
98299830
]);
9831+
9832+
}(typeof exports === 'undefined' ? this.redirect_trac_tickets = {} : exports))

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"bugs": {
1919
"url": "https://github.com/twisted/twisted.github.io/issues"
2020
},
21-
"homepage": "https://github.com/twisted/twisted.github.io#readme"
21+
"homepage": "https://github.com/twisted/twisted.github.io#readme",
22+
"devDependencies": {
23+
"jest": "^28.1.2"
24+
}
2225
}

0 commit comments

Comments
 (0)