Skip to content

Commit

Permalink
Add domains test (#8347)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkamran80 authored Dec 19, 2024
1 parent 8336ab3 commit 9f3fa8e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
70 changes: 70 additions & 0 deletions tests/domains.js
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 9f3fa8e

Please sign in to comment.