From 40884f4823e36afd50dd732ea481d498f137bc8f Mon Sep 17 00:00:00 2001 From: emc-kk Date: Fri, 15 Jul 2022 02:17:10 +0900 Subject: [PATCH] Adding parameters to comment the PSI results once Pull Request occurred --- .github/workflows/pr.yml | 47 ++++++++++++++++++++++++++++++++++++++++ action.yml | 19 ++++++++++++++++ index.js | 32 ++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/pr.yml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 00000000..ee1a9daf --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,47 @@ +on: [pull_request] + +jobs: + run_psi: + runs-on: ubuntu-latest + name: Running Page Speed Insights + steps: + - name: Checkout + uses: actions/checkout@v1 + - name: Running Page Speed Insights + uses: ./ # Uses an action in the root directory + id: psi + with: + url: "https://jake.partus.ch" + threshold: 85 + strategy: mobile + activity_type: pull_request + - name: Outputting the performance results of a given URL + uses: mshick/add-pr-comment@v1 + with: + score: | + **PSI Score: ** + `${{ steps.psi.outputs.score }}` + + **[CoreWebVitals] FCP Weight: ** + `${{ steps.psi.outputs.fcp }}` + + **[CoreWebVitals] TTI Weight: ** + `${{ steps.psi.outputs.tti }}` + + **[CoreWebVitals] SI Weight: ** + `${{ steps.psi.outputs.si }}` + + **[CoreWebVitals] TBT Weight: ** + `${{ steps.psi.outputs.tbt }}` + + **[CoreWebVitals] LCP Weight: ** + `${{ steps.psi.outputs.lcp }}` + + **[CoreWebVitals] CLS Weight: ** + `${{ steps.psi.outputs.cls }}` + + **[CoreWebVitals] FMP Weight: ** + `${{ steps.psi.outputs.fmp }}` + repo-token: ${{ secrets.GITHUB_TOKEN }} + repo-token-user-login: 'github-actions[bot]' + allow-repeats: false diff --git a/action.yml b/action.yml index b3d69f79..01dd83ff 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,25 @@ inputs: description: "Strategy to use when analyzing the page (mobile/desktop)." key: description: "A PageSpeed Insights API key" + activity_type: + description: "The activity type to trigger workflows that run on this event" +outputs: + score: + description: "Performance score of a given URL" + fcp: + description: "FCP weight of a given URL" + tti: + description: "TTI weight of a given URL" + si: + description: "SI weight of a given URL" + tbt: + description: "TBT weight of a given URL" + lcp: + description: "LCP weight of a given URL" + cls: + description: "CLS weight of a given URL" + fmp: + description: "FMP weight of a given URL" runs: using: "node12" main: "index.js" diff --git a/index.js b/index.js index ee920270..47dc2a34 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,8 @@ const core = require("@actions/core"); const psi = require("psi"); +const coreWebVitalsTypes = ["FCP", "TTI", "SI", "TBT", "LCP", "CLS", "FMP"]; + const run = async () => { try { const url = core.getInput("url"); @@ -13,18 +15,42 @@ const run = async () => { const threshold = Number(core.getInput("threshold")) || 70; const strategy = core.getInput("strategy") || "mobile"; - // Output a formatted report to the terminal console.log(`Running Page Speed Insights for ${url}`); - await psi.output(url, { + + const params = { ...(key ? {key} : undefined), ...(key ? undefined : {nokey: "true"}), strategy, format: "cli", threshold - }); + }; + + // Output a formatted report to the terminal + await psi.output(url, params); + + if (activity_type === "pull_request") { + // Output a simple report to the pull request comment section + const output = await psi(url, params); + const performance = output.data.lighthouseResult.categories.performance; + core.setOutput("score", JSON.stringify(performance.score)); + const coreWebVitals = getCoreWebVitals(performance.auditRefs); + setCoreWebVitals(coreWebVitals); + } } catch (error) { core.setFailed(error.message); } }; +const getCoreWebVitals = (refs) => { + return refs.reduce((acc, ref, _) => { + if (coreWebVitalsTypes.includes(ref.acronym)) + acc[ref.acronym] = ref.weight; + }, {}); +} + +const setCoreWebVitals = (coreWebVitals) => { + for (const type of coreWebVitalsTypes) + core.setOutput(type.toLowerCase(), JSON.stringify(coreWebVitals[type])); +} + run();