Skip to content

Commit a2d810b

Browse files
authored
Fix link redirection for aka.ms URLs in documentation conversion (#63175)
1 parent c12b176 commit a2d810b

2 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎src/codeql-cli/scripts/convert-markdown-for-docs.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,11 @@ async function getRedirect(url: string): Promise<string> {
316316

317317
// Some of the aka.ms links have the /en language prefix.
318318
// This removes all language prefixes from the redirect url.
319-
const redirectNoLang = languageKeys.reduce((acc, lang) => {
320-
return acc.replace(`/${lang}`, ``)
321-
}, redirect)
322-
323-
if (!redirectNoLang) {
324-
const errorMsg = `The aka.ms redirected to an unexpected url: ${url}`
325-
throw new Error(errorMsg)
319+
const parts = redirect.split('/')
320+
if (parts.length > 1 && languageKeys.includes(parts[1])) {
321+
// delete language prefix (e.g., /en, /ja, etc.)
322+
parts.splice(1, 1)
326323
}
327324

328-
return redirectNoLang
325+
return parts.join('/')
329326
}

‎src/codeql-cli/tests/convert-markdown-for-docs.ts‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
import { describe, expect, test } from 'vitest'
1+
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
22
import { convertContentToDocs } from '../scripts/convert-markdown-for-docs'
33
import config from '@/codeql-cli/lib/config.json'
44

55
const RELATIVE_LINK_PATH = config.targetDirectory.replace('content', '')
66

77
describe('convertContentToDocs circular link handling', () => {
8+
const fetchMock = vi.spyOn(globalThis, 'fetch')
9+
10+
beforeAll(() => {
11+
fetchMock.mockResolvedValue(
12+
new Response(null, {
13+
status: 302,
14+
headers: {
15+
location:
16+
'https://docs.github.com/enterprise-server@latest/code-security/reference/code-scanning/sarif-files/sarif-support',
17+
},
18+
}),
19+
)
20+
})
21+
22+
afterAll(() => {
23+
vi.restoreAllMocks()
24+
})
25+
826
const testContent = `
927
# bqrs interpret
1028
@@ -21,7 +39,8 @@ metadata and generates output in the specified format.
2139
2240
This option has no effect when passed to \`codeql bqrs interpret<bqrs-interpret>\`{.interpreted-text role="doc"}.
2341
24-
For more information, see \`codeql database analyze<database-analyze>\`{.interpreted-text role="doc"}.
42+
For more information, see \`codeql database analyze<database-analyze>\`{.interpreted-text role="doc"} and
43+
<https://aka.ms/code-scanning-docs/sarif-support> for details about uploading SARIF files.
2544
`
2645

2746
test('converts circular links to plain text', async () => {
@@ -45,6 +64,37 @@ For more information, see \`codeql database analyze<database-analyze>\`{.interpr
4564
)
4665
})
4766

67+
test('converts aka.ms links', async () => {
68+
const result = await convertContentToDocs(testContent, {}, 'bqrs-interpret.md')
69+
70+
// Should convert aka.ms link to redirect
71+
expect(result.content).toContain(
72+
'[AUTOTITLE](/enterprise-server@latest/code-security/reference/code-scanning/sarif-files/sarif-support)',
73+
)
74+
75+
// Should not still contain aka.ms link
76+
expect(result.content).not.toContain('https://aka.ms/')
77+
})
78+
79+
test('strips language prefix from aka.ms redirect links', async () => {
80+
fetchMock.mockResolvedValue(
81+
new Response(null, {
82+
status: 302,
83+
headers: {
84+
location:
85+
'https://docs.github.com/en/enterprise-server@latest/code-security/reference/code-scanning/sarif-files/sarif-support',
86+
},
87+
}),
88+
)
89+
90+
const result = await convertContentToDocs(testContent, {}, 'bqrs-interpret.md')
91+
92+
// Should strip language prefix from aka.ms redirect link
93+
expect(result.content).toContain(
94+
'[AUTOTITLE](/enterprise-server@latest/code-security/reference/code-scanning/sarif-files/sarif-support)',
95+
)
96+
})
97+
4898
test('handles edge case: no filename provided', async () => {
4999
const result = await convertContentToDocs(testContent, {}, '')
50100

0 commit comments

Comments
 (0)