From 9f3fa8e438f4bcbd4fac6f0bc1d73ff68f5ed747 Mon Sep 17 00:00:00 2001 From: "H. Kamran" Date: Thu, 19 Dec 2024 06:28:44 -0800 Subject: [PATCH] Add domains test (#8347) --- .github/workflows/pull_request.yml | 4 ++ tests/domains.js | 70 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/domains.js diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ad8bf4fbae0..269a627316e 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -54,6 +54,10 @@ jobs: if: steps.diff.outputs.entries run: node tests/categories.js ${{ steps.diff.outputs.entries }} + - name: Validate Domains + if: steps.diff.outputs.entries + run: node tests/domains.js ${{ steps.diff.outputs.entries }} + - name: Validate Images if: ${{ steps.diff.outputs.entries || steps.diff.outputs.images }} run: | diff --git a/tests/domains.js b/tests/domains.js new file mode 100644 index 00000000000..e9219e0d44a --- /dev/null +++ b/tests/domains.js @@ -0,0 +1,70 @@ +const fs = require("fs").promises; +const core = require("@actions/core"); + +/** + * A list of [ccSLDs](https://icannwiki.org/Second_Level_Domain#ccSLDs) that should be used to omit false positives from the subdomain check + * + * @constant + * @type {string} + * @default + */ +const CCSLDS = ["ac", "co", "com", "gov", "net", "org"]; + +/** + * Get subdomains for a given domain + * + * @param {string} domain The domain to retrieve subdomains for + * @returns {string|null} The subdomains + */ +function getSubdomains(domain) { + const parts = domain.split("."); + + if (parts.length <= 2) return null; + + return CCSLDS.includes(parts.slice(-3)[1]) + ? parts.slice(0, -3).join(".") + : parts.slice(0, -2).join("."); +} + +async function main(files) { + await Promise.all( + files.map(async (file) => { + const json = JSON.parse(await fs.readFile(file)); + const entry = json[Object.keys(json)[0]]; + + // WWW prefix + if (entry.domain.startsWith("www.")) + core.warning("Domains should not start with `www.`", { file }); + + // Subdomains + if (getSubdomains(entry.domain)) + core.warning("Consider using the base domain as the domain.", { file }); + + // Additional domains + let duplicateDomains = false, + duplicateAdditionalDomains = false; + entry["additional-domains"]?.forEach((domain) => { + if (domain.includes(entry.domain)) duplicateDomains = true; + if ( + entry["additional-domains"].some((additionalDomain) => + domain.includes(additionalDomain) + ) + ) + duplicateAdditionalDomains = true; + }); + + if (duplicateDomains) + core.warning( + "If the main domain is listed, subdomains are not necessary.", + { file } + ); + + if (duplicateAdditionalDomains) + core.warning("Please remove duplicate additional domains.", { file }); + }) + ); + + return true; +} + +main(process.argv.slice(2)).then(() => process.exit(0));