From 585a9d3e5e56ffb61248044e3e3545e6bba7c4da Mon Sep 17 00:00:00 2001 From: Benjamin Lannon Date: Wed, 29 May 2019 12:19:22 -0400 Subject: [PATCH] feat(actions): Add Site Showcase Validator Action (#14363) * feat(actions): Add Gatsby Site Showcase Validator Action * Update workflow file --- .../gatsby-site-showcase-validator/Dockerfile | 15 ++++ .../gatsby-site-showcase-validator/README.md | 5 ++ .../gatsby-site-showcase-validator/index.js | 80 +++++++++++++++++++ .../package.json | 15 ++++ .github/main.workflow | 9 +++ 5 files changed, 124 insertions(+) create mode 100644 .github/actions/gatsby-site-showcase-validator/Dockerfile create mode 100644 .github/actions/gatsby-site-showcase-validator/README.md create mode 100644 .github/actions/gatsby-site-showcase-validator/index.js create mode 100644 .github/actions/gatsby-site-showcase-validator/package.json diff --git a/.github/actions/gatsby-site-showcase-validator/Dockerfile b/.github/actions/gatsby-site-showcase-validator/Dockerfile new file mode 100644 index 0000000000000..c9b34b47226e6 --- /dev/null +++ b/.github/actions/gatsby-site-showcase-validator/Dockerfile @@ -0,0 +1,15 @@ +FROM node:10-slim + +LABEL com.github.actions.name="gatsby-site-showcase-validator" +LABEL com.github.actions.description="Check Gatsby's Site Showcase to validate all sites are built with Gatsby" +LABEL com.github.actions.icon="monitor" +LABEL com.github.actions.color="purple" + +# Copy the action's code into the container +COPY . . + +# Install dependencies +RUN yarn + +# Start node app +ENTRYPOINT [ "node", "/index.js" ] diff --git a/.github/actions/gatsby-site-showcase-validator/README.md b/.github/actions/gatsby-site-showcase-validator/README.md new file mode 100644 index 0000000000000..2751dcfe61983 --- /dev/null +++ b/.github/actions/gatsby-site-showcase-validator/README.md @@ -0,0 +1,5 @@ +# Gatsby Site Showcase Validator + +A simple node script that visits and checks all of the sites in the [Site Showcase](https://www.gatsbyjs.org/showcase/) for Gatsby. + +Run locally [using act](https://github.com/nektos/act): `act -a "gatsby-site-showcase-validator"` diff --git a/.github/actions/gatsby-site-showcase-validator/index.js b/.github/actions/gatsby-site-showcase-validator/index.js new file mode 100644 index 0000000000000..df8f003bb8b9d --- /dev/null +++ b/.github/actions/gatsby-site-showcase-validator/index.js @@ -0,0 +1,80 @@ +// Load in modules +const fetch = require("node-fetch") +const yaml = require("js-yaml") +const cheerio = require("cheerio") +const chalk = require("chalk") + +async function run() { + // Grab down sites.yml + let url = + "https://raw.githubusercontent.com/gatsbyjs/gatsby/master/docs/sites.yml" + + let yamlStr + + try { + yamlStr = await fetch(url).then(resp => resp.text()) + } catch (err) { + console.log(`[Err]: ${err.message}`) + process.exit(1) + } + + // Parse YAML + let parsedYaml = yaml.safeLoad(yamlStr, "utf8") + + let sitesVisited = 0 + let nonGatsbySiteCount = 0 + let erroredOut = 0 + let totalSitesCount = parsedYaml.length + + // Loop over each site + for (let site of parsedYaml) { + let siteUrl = site.main_url + + let siteHtml + + // Fetch site + try { + siteHtml = await fetch(siteUrl).then(resp => resp.text()) + } catch (err) { + console.log( + `${chalk.red(`[Err]`)}: ${site.title} (${siteUrl}) ran into an error: ${ + err.message + }` + ) + sitesVisited++ + erroredOut++ + continue // Skip the rest of the check for this particular site + } + + // Pass html into a parser + let $ = cheerio.load(siteHtml) + + // Most Gatsby sites have an id of "___gatsby" + let gatsbyContainer = $("#___gatsby") + + if (gatsbyContainer.length !== 0) { + // The site is a gatsby site don't do anything + sitesVisited++ + } else { + // The site is not a gatsby site, print out some info + console.log( + `${chalk.yellow(`[Notice]`)}: ${ + site.title + } (${siteUrl}) is not a Gatsby site` + ) + sitesVisited++ + nonGatsbySiteCount++ + } + } + + console.log( + chalk.green( + `We visited ${sitesVisited}/${totalSitesCount} sites. Out of them, ${nonGatsbySiteCount} sites were not a Gatsby site and ${erroredOut} errored out when visiting it.` + ) + ) + + // If there are any non Gatsby sites, fail (non-zero exit code) + process.exit(nonGatsbySiteCount > 0 ? 1 : 0) +} + +run() diff --git a/.github/actions/gatsby-site-showcase-validator/package.json b/.github/actions/gatsby-site-showcase-validator/package.json new file mode 100644 index 0000000000000..d05d9ed6b18bb --- /dev/null +++ b/.github/actions/gatsby-site-showcase-validator/package.json @@ -0,0 +1,15 @@ +{ + "name": "gatsby-site-showcase-validator-action", + "version": "1.0.0", + "main": "index.js", + "private": true, + "dependencies": { + "chalk": "^2.4.2", + "cheerio": "^1.0.0-rc.2", + "js-yaml": "^3.12.2", + "node-fetch": "^2.3.0" + }, + "scripts": { + "start": "node ./index.js" + } +} diff --git a/.github/main.workflow b/.github/main.workflow index aee298318982f..309e460d8bb87 100644 --- a/.github/main.workflow +++ b/.github/main.workflow @@ -11,3 +11,12 @@ action "high-priority-prs" { "SLACK_CHANNEL_ID", ] } + +workflow "Site Showcase Validator workflow" { + resolves = ["gatsby-site-showcase-validator"] + on = "schedule(0 0 * * *)" +} + +action "gatsby-site-showcase-validator" { + uses = "./.github/actions/gatsby-site-showcase-validator" +}