From 6133eed31c3f7843b41155b257c778195dbac3a1 Mon Sep 17 00:00:00 2001 From: Radu-Cristian Popa Date: Tue, 5 Dec 2023 16:52:04 +0200 Subject: [PATCH] Add custom scripts and preview template --- scripts/constants.cjs | 18 ++++++++ scripts/delete-artifacts.cjs | 68 ++++++++++++++++++++++++++++ scripts/get-workflow-artifacts.cjs | 72 ++++++++++++++++++++++++++++++ scripts/templates/build-status.md | 21 +++++++++ 4 files changed, 179 insertions(+) create mode 100644 scripts/constants.cjs create mode 100644 scripts/delete-artifacts.cjs create mode 100644 scripts/get-workflow-artifacts.cjs create mode 100644 scripts/templates/build-status.md diff --git a/scripts/constants.cjs b/scripts/constants.cjs new file mode 100644 index 00000000..ad804d29 --- /dev/null +++ b/scripts/constants.cjs @@ -0,0 +1,18 @@ +const BROWSERS = ['chrome', 'firefox', 'opera', 'edge'] +const COLORS = { + green: '3fb950', + red: 'd73a49', +} +const TEMPLATE_VARS = { + tableBody: '{{ TABLE_BODY }}', + sha: '{{ SHA }}', + conslusion: '{{ CONCLUSION }}', + badgeColor: '{{ BADGE_COLOR }}', + jobLogs: '{{ JOB_LOGS }}', +} + +module.exports = { + BROWSERS, + COLORS, + TEMPLATE_VARS, +} diff --git a/scripts/delete-artifacts.cjs b/scripts/delete-artifacts.cjs new file mode 100644 index 00000000..4d25ad24 --- /dev/null +++ b/scripts/delete-artifacts.cjs @@ -0,0 +1,68 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +/* eslint-disable no-console */ +const { BROWSERS } = require('./constants.cjs') + +async function getBrowserArfifacts({ github, owner, repo, name }) { + const artifacts = [] + const result = await github.rest.actions.listArtifactsForRepo({ + owner, + repo, + name, + }) + + for (let i = 0; i < result.data.total_count; i++) { + artifacts.push(result.data.artifacts[i].id) + } + + return artifacts +} + +async function getPRArtifacts({ github, owner, repo, prNumber }) { + const promises = [] + const artifacts = [] + + BROWSERS.forEach(browser => + promises.push( + getBrowserArfifacts({ + github, + owner, + repo, + name: `${prNumber}-${browser}`, + }), + ), + ) + + const data = await Promise.all(promises) + + for (let i = 0; i < data.length; i++) { + artifacts.push.apply(artifacts, data[i]) + } + + return artifacts +} + +module.exports = async ({ github, context }) => { + if (context.payload.action !== 'closed') { + console.error('This action only works on closed PRs.') + process.exit(1) + } + + const { owner, repo } = context.repo + const prNumber = context.payload.number + const promises = [] + + const artifacts = await getPRArtifacts({ github, owner, repo, prNumber }) + + for (let i = 0; i < artifacts.length; i++) { + promises.push( + github.rest.actions.deleteArtifact({ + owner, + repo, + artifact_id: artifacts[i], + }), + ) + } + + await Promise.all(promises) + console.log(`Deleted ${artifacts.length} artifacts for PR #${prNumber}.`) +} diff --git a/scripts/get-workflow-artifacts.cjs b/scripts/get-workflow-artifacts.cjs new file mode 100644 index 00000000..7ae35e55 --- /dev/null +++ b/scripts/get-workflow-artifacts.cjs @@ -0,0 +1,72 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +/* eslint-disable no-console */ + +const fs = require('node:fs/promises') +const { COLORS, TEMPLATE_VARS } = require('./constants.cjs') + +function capitalizeArtifactName(artifactName) { + const [, browser] = artifactName.split('-') + return browser.charAt(0).toUpperCase() + browser.slice(1) +} + +function formatBytes(bytes, decimals = 2) { + if (!Number(bytes)) return '0 bytes' + const k = 1024 + const dm = decimals < 0 ? 0 : decimals + const sizes = ['B', 'KB', 'MB', 'GB'] + const i = Math.floor(Math.log(bytes) / Math.log(k)) + return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))}${sizes[i]}` +} + +module.exports = async ({ github, context, core }) => { + const workflowRun = context.payload.workflow_run + const runId = workflowRun.id + const conclusion = workflowRun.conclusion + const baseUrl = context.payload.repository.html_url + const sha = workflowRun.pull_requests[0].head.sha + const prNumber = workflowRun.pull_requests[0].number + const jobLogsUrl = `${baseUrl}/actions/runs/${workflowRun.id}` + const template = await fs.readFile('./scripts/templates/build-status.md', 'utf8') + const tableRows = [] + + let tableBody = '' + let badgeColor = COLORS.red + + if (conclusion === 'success') { + const { owner, repo } = context.repo + const suiteId = workflowRun.check_suite_id + badgeColor = COLORS.green + + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner, + repo, + run_id: runId, + }) + + artifacts.data.artifacts.forEach(artifact => { + const browser = capitalizeArtifactName(artifact.name) + const artifactUrl = `${baseUrl}/suites/${suiteId}/artifacts/${artifact.id}` + tableRows.push(` + + ${browser} (${formatBytes(artifact.size_in_bytes)}) + + + Download + + + `) + }) + + tableBody = tableRows.join('') + } + + const commentBody = template + .replace(TEMPLATE_VARS.conslusion, conclusion) + .replace(TEMPLATE_VARS.badgeColor, badgeColor) + .replace(TEMPLATE_VARS.sha, sha) + .replace(TEMPLATE_VARS.jobLogs, `${jobLogsUrl}}`) + .replace(TEMPLATE_VARS.tableBody, tableBody) + + core.setOutput('comment_body', commentBody) + core.setOutput('pr_number', prNumber) +} diff --git a/scripts/templates/build-status.md b/scripts/templates/build-status.md new file mode 100644 index 00000000..dfbdd4d1 --- /dev/null +++ b/scripts/templates/build-status.md @@ -0,0 +1,21 @@ +Badge + + + + + + + + + + + + + + + + + + {{ TABLE_BODY }} + +
NameLink
Latest commit{{ SHA }}
Latest job logs{{ JOB_LOGS }}