From 4a0e523ec180eca330c219a88439087e22f90ef2 Mon Sep 17 00:00:00 2001 From: Arnau Casau <47946624+arnaucasau@users.noreply.github.com> Date: Wed, 15 May 2024 19:09:20 +0200 Subject: [PATCH] External link checker: Add an HTTP header for the auth for GitHub (#1392) Part of #823 This PR adds a new HTTP header to the external link checker to set the auth for GitHub when we are checking GitHub links. The new header is only used for GitHub links following what we do in the closed-source repo. In order to be able to get the environment variable from typescript with `process.env.GITHUB_TOKEN`, we need to define it in the workflows. In the case of running the script locally, the token could be undefined. --------- Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --- .github/workflows/weekly-checks.yml | 2 ++ scripts/lib/links/ExternalLink.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/weekly-checks.yml b/.github/workflows/weekly-checks.yml index bb3bf9f6d9f..35f53e840d7 100644 --- a/.github/workflows/weekly-checks.yml +++ b/.github/workflows/weekly-checks.yml @@ -53,6 +53,8 @@ jobs: - name: Install Node.js dependencies run: npm ci - name: Check external links + env: + GITHUB_TOKEN: ${{ github.token }} run: > npm run check:external-links -- 'docs/**/*.{md,mdx,ipynb}' diff --git a/scripts/lib/links/ExternalLink.ts b/scripts/lib/links/ExternalLink.ts index 07e0e11537e..a435a39aa7a 100644 --- a/scripts/lib/links/ExternalLink.ts +++ b/scripts/lib/links/ExternalLink.ts @@ -37,7 +37,7 @@ export class ExternalLink { let error: string = ""; try { const response = await fetch(this.value, { - headers: { "User-Agent": "qiskit-documentation-broken-links-finder" }, + headers: getHeaders(this.value), method: "HEAD", }); if (response.status >= 300) { @@ -57,3 +57,18 @@ export class ExternalLink { return `❌ ${error}. Appears in:\n${fileList.join("\n")}`; } } + +function getHeaders(link: string) { + const headers: HeadersInit = { + "User-Agent": "qiskit-documentation-broken-links-finder", + }; + + if (link.startsWith("https://github.com")) { + const ghToken = process.env.GITHUB_TOKEN; + if (ghToken) { + headers["Authorization"] = `token ${ghToken}`; + } + } + + return headers; +}