Skip to content

Commit

Permalink
Fix redirect checker (#44417)
Browse files Browse the repository at this point in the history
The checker assumed that values of the `source` field in each redirect
configuration begin with `/docs/`, but they do not. Remove `/docs/` from
docs site URLs in the target directory before checking them against
configured redirects.

Fix an issue with the first item in the output of the program
always being `undefined` by correctly concatenating the result arrays.

Finally, fix a bug in which the checker would not accept redirects for
docs URLs with fragments.
  • Loading branch information
ptgott authored Jul 23, 2024
1 parent 40c1955 commit 9441e58
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 20 deletions.
33 changes: 15 additions & 18 deletions docs/check-redirects/check-redirects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const yaml = require('yaml');
const path = require('path');

const teleportDomain = 'https://goteleport.com';
const teleportDocsURL = 'https://goteleport.com/docs';
// RedirectChecker checks for Teleport docs site domains and paths within a
// given file tree and determines whether a given docs configuration requires
// redirects.
Expand Down Expand Up @@ -38,21 +38,14 @@ class RedirectChecker {
// URLs that do not correspond to an existing docs page or redirect. It
// returns a list of problematic URLs.
check() {
const results = this.checkDir(this.otherRepoRoot);
let deduped = {};
if (results != undefined) {
results.forEach(r => {
deduped[r] = true;
});
return Object.keys(deduped);
}
return this.checkDir(this.otherRepoRoot);
}

// checkDir recursively checks for docs URLs with missing docs paths or
// redirects at dirPath. It returns an array of missing URLs.
checkDir(dirPath) {
const files = this.fs.readdirSync(dirPath, 'utf8');
let result = [];
let result = new Set();
files.forEach(f => {
for (let e = 0; e < this.exclude.length; e++) {
if (f.endsWith(this.exclude[e])) {
Expand All @@ -62,26 +55,30 @@ class RedirectChecker {
const fullPath = path.join(dirPath, f);
const info = this.fs.statSync(fullPath);
if (!info.isDirectory()) {
result = result.concat(this.checkFile(fullPath));
for (const r of this.checkFile(fullPath)) {
result.add(r);
}
return;
}
result = result.concat(this.checkDir(fullPath));
for (const r of this.checkDir(fullPath)) {
result.add(r);
}
});
return result;
return [...result];
}

// checkFile determines whether docs URLs found in the file
// match either an actual docs file path or a redirect source.
// Returns an array of URLs with missing files or redirects.
checkFile(filePath) {
const docsPattern = new RegExp(
/https:\/\/goteleport.com\/docs\/(ver\/[0-9]+\.x\/)?[\w\/_#-]+/,
/https:\/\/goteleport.com\/docs(\/ver\/[0-9]+\.x)?(\/[\w\/_-]+)\/?#?/,
'gm'
);
const text = this.fs.readFileSync(filePath, 'utf8');
const docsURLs = [...text.matchAll(docsPattern)];
if (docsURLs.length === 0) {
return;
return [];
}
let result = [];
docsURLs.forEach(url => {
Expand All @@ -92,10 +89,10 @@ class RedirectChecker {
}) == undefined;

if (!missingEntry) {
return;
return [];
}

let pathPart = url[0].slice(teleportDomain.length);
let pathPart = url[2];
if (pathPart[pathPart.length - 1] != '/') {
pathPart += '/';
}
Expand All @@ -109,7 +106,7 @@ class RedirectChecker {

urlToDocsPath(url) {
let nofrag = url.split('#')[0]; // Remove the fragment
let rest = nofrag.slice((teleportDomain + '/docs/').length);
let rest = nofrag.slice(teleportDocsURL.length);
if (rest.length == 0) {
return path.join(this.docsRoot, 'docs', 'pages', 'index.mdx');
}
Expand Down
78 changes: 76 additions & 2 deletions docs/check-redirects/check-redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ This is a link to a [documentation page](https://goteleport.com/docs/subdirector
Here is a link to a [missing page](https://goteleport.com/docs/page3).
Here is a link to a [missing page](https://goteleport.com/docs/page3).
`,
'/docs/content/1.x/docs/pages/page1.mdx': `---
title: "Sample Page 1"
Expand Down Expand Up @@ -71,8 +73,80 @@ title: "Desktop Access Getting Started"
const fs = createFsFromVolume(vol);
const checker = new RedirectChecker(fs, '/blog', '/docs/content/1.x', [
{
source: '/docs/page3/',
destination: '/docs/page1',
source: '/page3/',
destination: '/page1/',
permanent: true,
},
]);
const results = checker.check();
expect(results).toEqual([]);
});

test(`allows missing docs pages for links with fragments if there is a redirect`, () => {
const vol = Volume.fromJSON({
'/blog/content2.mdx': `---
title: "Sample Page 2"
---
Here is a link to a [missing page](https://goteleport.com/docs/page3/#my-fragment).
`,
'/docs/content/1.x/docs/pages/page1.mdx': `---
title: "Sample Page 1"
---
`,
});
const fs = createFsFromVolume(vol);
const checker = new RedirectChecker(fs, '/blog', '/docs/content/1.x', [
{
source: '/page3/',
destination: '/page1/',
permanent: true,
},
]);
const results = checker.check();
expect(results).toEqual([]);
});

test(`catches missing redirects for URLs with query strings`, () => {
const vol = Volume.fromJSON({
'/blog/content2.mdx': `---
title: "Sample Page 2"
---
Here is a link to a [missing page](https://goteleport.com/docs/access-controls/device-trust/guide/?scope=enterprise#step-12-register-a-trusted-device").
`,
'/docs/content/1.x/docs/pages/page1.mdx': `---
title: "Sample Page 1"
---
`,
});
const fs = createFsFromVolume(vol);
const checker = new RedirectChecker(fs, '/blog', '/docs/content/1.x', []);
const results = checker.check();
expect(results).toEqual(['https://goteleport.com/docs/access-controls/device-trust/guide/']);
});

test(`allows a docs URL with a query string if there is a redirect`, () => {
const vol = Volume.fromJSON({
'/blog/content2.mdx': `---
title: "Sample Page 2"
---
Here is a link to a [missing page](https://goteleport.com/docs/access-controls/device-trust/guide/?scope=enterprise#step-12-register-a-trusted-device").
`,
'/docs/content/1.x/docs/pages/page1.mdx': `---
title: "Sample Page 1"
---
`,
});
const fs = createFsFromVolume(vol);
const checker = new RedirectChecker(fs, '/blog', '/docs/content/1.x', [
{
source: '/access-controls/device-trust/guide/',
destination: '/access-controls/device-trust/',
permanent: true,
},
]);
Expand Down

0 comments on commit 9441e58

Please sign in to comment.