Skip to content

Commit 4887043

Browse files
authored
Phase 1: Replace got with fetch in basic usage files (#57051)
1 parent 3c245f7 commit 4887043

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

src/codeql-cli/scripts/convert-markdown-for-docs.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFile } from 'fs/promises'
22
import path from 'path'
3-
import got from 'got'
3+
44
import { fromMarkdown } from 'mdast-util-from-markdown'
55
import { toMarkdown } from 'mdast-util-to-markdown'
66
import { visitParents } from 'unist-util-visit-parents'
@@ -233,15 +233,24 @@ export async function convertContentToDocs(
233233
async function getRedirect(url) {
234234
let response = null
235235
try {
236-
response = await got(url)
236+
response = await fetch(url, { redirect: 'manual' })
237+
if (!response.ok && response.status !== 301 && response.status !== 302) {
238+
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
239+
}
237240
} catch (error) {
238241
console.error(error)
239242
const errorMsg = `Failed to get redirect for ${url} when converting aka.ms links to docs.github.com links.`
240243
throw new Error(errorMsg)
241244
}
242245

243-
// The first entry of redirectUrls has the anchor if it exists
244-
const redirect = response.redirectUrls[0].pathname
246+
// Get the redirect location from the response header
247+
const redirectLocation = response.headers.get('location')
248+
if (!redirectLocation) {
249+
throw new Error(`No redirect location found for ${url}`)
250+
}
251+
252+
// Parse the URL to get the pathname
253+
const redirect = new URL(redirectLocation).pathname
245254

246255
// Some of the aka.ms links have the /en language prefix.
247256
// This removes all language prefixes from the redirect url.

src/workflows/find-past-built-pr.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import got from 'got'
2-
31
import { setOutput } from '@actions/core'
42

53
import github from './github'
@@ -39,10 +37,14 @@ async function main() {
3937
}
4038

4139
async function getBuiltSHA() {
42-
const r = await got('https://docs.github.com/_build')
43-
const sha = r.body.trim()
40+
const r = await fetch('https://docs.github.com/_build')
41+
if (!r.ok) {
42+
throw new Error(`HTTP ${r.status}: ${r.statusText}`)
43+
}
44+
const body = await r.text()
45+
const sha = body.trim()
4446
if (!/[a-f0-9]{40}/.test(sha)) {
45-
throw new Error(`Response body does not look like a SHA ('${r.body.slice(0, 100)}'...)`)
47+
throw new Error(`Response body does not look like a SHA ('${body.slice(0, 100)}'...)`)
4648
}
4749
return sha
4850
}

src/workflows/purge-edge-cache.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import got from 'got'
2-
31
// Because we use shielding, it's recommended that you purge twice
42
// so it purges the edge nodes *and* the origin.
53
// The documentation says:
@@ -32,7 +30,17 @@ async function purgeFastlyBySurrogateKey({
3230
'fastly-soft-purge': '1',
3331
}
3432
const requestPath = `https://api.fastly.com/service/${safeServiceId}/purge/${surrogateKey}`
35-
return got.post(requestPath, { headers, json: true })
33+
const response = await fetch(requestPath, {
34+
method: 'POST',
35+
headers: {
36+
...headers,
37+
'Content-Type': 'application/json',
38+
},
39+
})
40+
if (!response.ok) {
41+
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
42+
}
43+
return response
3644
}
3745

3846
export default async function purgeEdgeCache(

src/workflows/test-local-dev.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'node:assert/strict'
22
import fs from 'fs'
33

44
import cheerio from 'cheerio'
5-
import got from 'got'
65

76
/**
87
* A very basic script that tests the local dev server.
@@ -22,12 +21,18 @@ import got from 'got'
2221
main()
2322

2423
async function get(path: string, options?: Record<string, any>) {
25-
// By default, got() will use retries and follow redirects.
24+
// By default, fetch() will follow redirects.
2625
const t0 = new Date()
27-
const response = await got(makeURL(path), options)
26+
const response = await fetch(makeURL(path), options)
2827
const took = new Date().getTime() - t0.getTime()
29-
console.log(`GET ${path} => ${response.statusCode} (${took}ms)`)
30-
return response
28+
console.log(`GET ${path} => ${response.status} (${took}ms)`)
29+
30+
// Convert fetch response to have similar interface as got response
31+
const body = await response.text()
32+
return {
33+
statusCode: response.status,
34+
body: body,
35+
}
3136
}
3237

3338
function makeURL(path: string) {

0 commit comments

Comments
 (0)