Skip to content

Commit

Permalink
External link checker: Add an HTTP header for the auth for GitHub (Qi…
Browse files Browse the repository at this point in the history
…skit#1392)

Part of Qiskit#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 <[email protected]>
  • Loading branch information
arnaucasau and Eric-Arellano authored May 15, 2024
1 parent 7831eac commit 4a0e523
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/weekly-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down
17 changes: 16 additions & 1 deletion scripts/lib/links/ExternalLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

0 comments on commit 4a0e523

Please sign in to comment.