Skip to content

Commit

Permalink
Add custom scripts and preview template
Browse files Browse the repository at this point in the history
  • Loading branch information
raducristianpopa committed Dec 5, 2023
1 parent f808a80 commit 6133eed
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
18 changes: 18 additions & 0 deletions scripts/constants.cjs
Original file line number Diff line number Diff line change
@@ -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,
}
68 changes: 68 additions & 0 deletions scripts/delete-artifacts.cjs
Original file line number Diff line number Diff line change
@@ -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}.`)
}
72 changes: 72 additions & 0 deletions scripts/get-workflow-artifacts.cjs
Original file line number Diff line number Diff line change
@@ -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(`
<tr>
<td>${browser} (${formatBytes(artifact.size_in_bytes)})
</td>
<td>
<a href="${artifactUrl}">Download</a>
</td>
</tr>
`)
})

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, `<a href="${jobLogsUrl}">${jobLogsUrl}}</a>`)
.replace(TEMPLATE_VARS.tableBody, tableBody)

core.setOutput('comment_body', commentBody)
core.setOutput('pr_number', prNumber)
}
21 changes: 21 additions & 0 deletions scripts/templates/build-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<img src="https://img.shields.io/badge/{{ CONCLUSION }}-{{ BADGE_COLOR }}?style=for-the-badge&label=build" alt="Badge" />

<table role="table">
<thead>
<tr>
<th>Name</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td>Latest commit</td>
<td>{{ SHA }}</td>
</tr>
<tr>
<td>Latest job logs</td>
<td>{{ JOB_LOGS }}</td>
</tr>
{{ TABLE_BODY }}
</tbody>
</table>

0 comments on commit 6133eed

Please sign in to comment.