Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ci): update workflows #35

Merged
merged 20 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/actions/constants.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const BADGE =
'<img src="https://img.shields.io/badge/{{ CONCLUSION }}-{{ BADGE_COLOR }}?style=for-the-badge&label={{ BADGE_LABEL }}" alt="Badge" />'
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 }}',
badgeLabel: '{{ BADGE_LABEL }}',
jobLogs: '{{ JOB_LOGS }}',
}

module.exports = {
BADGE,
BROWSERS,
COLORS,
TEMPLATE_VARS,
}
67 changes: 67 additions & 0 deletions .github/actions/delete-artifacts.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* 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, core }) => {
if (context.payload.action !== 'closed') {
core.setFailed('This action only works on closed PRs.')
}

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}.`)
}
96 changes: 96 additions & 0 deletions .github/actions/get-workflow-artifacts.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
const fs = require('node:fs/promises')
const { COLORS, TEMPLATE_VARS, BADGE } = require('./constants.cjs')

const ARTIFACTS_DATA = {
chrome: {
name: 'Chrome',
url: null,
size: null,
},
firefox: {
name: 'Firefox',
url: null,
size: null,
},
opera: {
name: 'Opera',
url: null,
size: null,
},
edge: {
name: 'Edge',
url: null,
size: null,
},
}

function getBadge(conclusion, badgeColor, badgeLabel) {
return BADGE.replace(TEMPLATE_VARS.conslusion, conclusion)
.replace(TEMPLATE_VARS.badgeColor, badgeColor)
.replace(TEMPLATE_VARS.badgeLabel, badgeLabel)
}

function formatBytes(bytes, decimals = 2) {
if (!Number(bytes)) return '0B'
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 { owner, repo } = context.repo
const baseUrl = context.payload.repository.html_url
const suiteId = context.payload.workflow_run.check_suite_id
const runId = context.payload.workflow_run.id
const conclusion = context.payload.workflow_run.conclusion
const sha = context.payload.workflow_run.pull_requests[0].head.sha
const prNumber = context.payload.workflow_run.pull_requests[0].number
const jobLogsUrl = `${baseUrl}/actions/runs/${context.payload.workflow_run.id}`
const template = await fs.readFile('./.github/actions/templates/build-status.md', 'utf8')
const tableRows = []

core.setOutput('conclusion', conclusion)

if (conclusion === 'cancelled') {
return
}

const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runId,
})

artifacts.data.artifacts.forEach(artifact => {
const [, key] = artifact.name.split('-')
ARTIFACTS_DATA[key].url = `${baseUrl}/suites/${suiteId}/artifacts/${artifact.id}`
ARTIFACTS_DATA[key].size = formatBytes(artifact.size_in_bytes)
})

Object.keys(ARTIFACTS_DATA).forEach(k => {
const { name, url, size } = ARTIFACTS_DATA[k]
if (url === null && size === null) {
const badgeUrl = getBadge('failure', COLORS.red, name)
tableRows.push(`<tr><td align="center">${badgeUrl}</td><td align="center">N/A</td></tr>`)
} else {
const badgeUrl = getBadge('success', COLORS.green, `${name} (${size})`)
tableRows.push(
`<tr><td align="center">${badgeUrl}</td><td align="center"><a href="${url}">Download</a></td></tr>`,
)
}
})

const tableBody = tableRows.join('')
const commentBody = template
.replace(TEMPLATE_VARS.conslusion, conclusion)
.replace(TEMPLATE_VARS.sha, sha)
.replace(TEMPLATE_VARS.jobLogs, `<a href="${jobLogsUrl}">Run #${runId}</a>`)
.replace(TEMPLATE_VARS.tableBody, tableBody)

core.setOutput('comment_body', commentBody)
core.setOutput('pr_number', prNumber)
}
22 changes: 22 additions & 0 deletions .github/actions/templates/build-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- __WM_EXTENSION_BUILD_PREVIEWS__ -->
<h2>Extension builds preview</h2>

<table role="table">
<thead>
<tr>
<th>Name</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">Latest commit</td>
<td align="center">{{ SHA }}</td>
</tr>
<tr>
<td align="center">Latest job logs</td>
<td align="center">{{ JOB_LOGS }}</td>
</tr>
{{ TABLE_BODY }}
</tbody>
</table>
44 changes: 44 additions & 0 deletions .github/workflows/build-previews.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build previews

on:
workflow_run:
types:
- 'completed'
workflows:
- 'PR Checks'

jobs:
comment:
name: Add comment with extension preview builds
runs-on: ubuntu-22.04
if: github.repository == 'interledger/web-monetization-extension'
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Get workflow artifacts
uses: actions/github-script@v7
id: get-workflow-artifacts
with:
script: |
const script = require('./.github/actions/get-workflow-artifacts.cjs')
await script({ github, context, core })

- name: Find comment
if: ${{ steps.get-workflow-artifacts.outputs.conclusion != 'cancelled' }}
uses: peter-evans/find-comment@v2
id: find-comment
with:
issue-number: ${{ steps.get-workflow-artifacts.outputs.pr_number }}
comment-author: 'raducristianpopa'
body-includes: '<!-- __WM_EXTENSION_BUILD_PREVIEWS__ -->'

- name: Add/Update comment
if: ${{ steps.get-workflow-artifacts.outputs.conclusion != 'cancelled' }}
uses: peter-evans/create-or-update-comment@v2
with:
token: ${{ secrets.PAT }}
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ steps.get-workflow-artifacts.outputs.pr_number }}
body: ${{ steps.get-workflow-artifacts.outputs.comment_body }}
edit-mode: replace
26 changes: 0 additions & 26 deletions .github/workflows/ci.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ on:
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
permissions:
actions: read
contents: read
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/delete-pr-artifacts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Delete PR artifacts

on:
pull_request:
types:
- closed

jobs:
delete:
name: Delete artifacts
if: github.repository == 'interledger/web-monetization-extension'
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Delete arfiacts
uses: actions/github-script@v7
with:
script: |
const script = require('./.github/actions/delete-artifacts.cjs')
await script({ github, context, core })
45 changes: 45 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: PR Checks

on:
pull_request:
types:
- opened
- reopened
- synchronize

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# TODO(@raducristianpopa): add lint/format checks and tests

jobs:
build:
name: Build
strategy:
fail-fast: false
matrix:
browser: [chrome, firefox, opera, edge]
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'

- name: Install dependencies
run: rm -rf node_modules && yarn install --frozen-lockfile

- name: Build
run: yarn build:${{ matrix.browser }}

- name: Upload artifacts
uses: actions/[email protected]
with:
name: ${{ github.event.pull_request.number }}-${{ matrix.browser }}
path: dist/${{ matrix.browser }}/${{ matrix.browser }}.zip
if-no-files-found: error
6 changes: 3 additions & 3 deletions .github/workflows/pr-title-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name: Check PR title

on:
pull_request:
branches: ["**"]
branches: ['**']

jobs:
check-pr-title:
name: Check PR Title
runs-on: ubuntu-22.04
steps:
steps:
- uses: amannn/action-semantic-pull-request@v5
env:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Loading