Skip to content

Commit f99600f

Browse files
author
Rafa Moral
authored
Merge pull request #1 from SUI-Components/feat/use-dynamic-urls
feat(Root): use dynamic urls
2 parents ccdfe66 + 0d9b907 commit f99600f

14 files changed

+384
-106
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@ name: Test this action
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ main ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ main ]
88

99
jobs:
1010
check-skip:
11-
runs-on: small
11+
runs-on: ubuntu-latest
1212
steps:
1313
- name: Cancel Previous Redundant Builds
1414
uses: styfle/cancel-workflow-action@0.6.0
1515
with:
1616
access_token: ${{ github.token }}
1717

18-
test-action:
19-
runs-on: small
18+
tests:
19+
runs-on: ubuntu-latest
2020
steps:
21-
- uses: actions/checkout@v3
22-
- uses: ./
21+
- name: Checkout Code
22+
uses: actions/checkout@v3
23+
24+
- name: Setup node
25+
uses: actions/setup-node@v2
2326
with:
24-
number: ${{ github.event.issue.number }}
25-
LHCI_GITHUB_TOKEN: ${{ secrets.LHCI_GITHUB_TOKEN }}
26-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
node-version: 16
28+
29+
- name: Install dependencies
30+
run: npm run phoenix
31+
32+
- name: Run Tests
33+
run: npm run test
34+
35+
- name: Run Lighthouse pull request action
36+
uses: ./pull-request
37+
with:
38+
urls: |
39+
http://localhost:8000/
40+
http://localhost:8000/listing

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
package-lock.json
12
node_modules

.lighthouserc.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,22 @@ const assertions = {
143143
viewport: ['off', {}]
144144
}
145145

146+
147+
const assert = {
148+
assertions
149+
}
150+
146151
const collect = {
147152
chromePath: '/opt/hostedtoolcache/chromium/latest/x64/chrome',
148153
startServerCommand:
149-
'node server',
154+
'npm run serve:lhci',
150155
startServerReadyPattern: 'listening',
151156
settings: {chromeFlags: ['--disable-gpu', '--no-sandbox', '-no-zygote']}
152157
}
153158

154159
module.exports = {
155160
ci: {
156-
assert: {
157-
assertions
158-
},
159-
collect,
160-
upload: {
161-
githubApiHost: 'https://github.mpi-internal.com/api/v3'
162-
}
161+
assert,
162+
collect
163163
}
164164
}

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
# frontend-common--lighthouse-pr-action
1+
# lighthouse actions
2+
3+
## Actions
4+
5+
| Name | Description |
6+
|------|-------------|
7+
| [pull-request](pull-request) | Analyse how Pull Request changes affects performance and add a comment |

action.yml

Lines changed: 0 additions & 72 deletions
This file was deleted.

package.json

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
{
2-
"name": "frontend-common--lighthouse-pr-action",
2+
"name": "@s-ui/lighthouse-action",
33
"version": "1.0.0",
44
"description": "Action for running Lighthouse CI on a PR and posts the results as a comment",
5-
"scripts": {
6-
"co": "npx @s-ui/mono@2 commit"
7-
},
85
"repository": {
96
"type": "git",
10-
"url": "https://github.mpi-internal.com/rafael-moral/frontend-common--lighthouse-pr-action.git"
7+
"url": "https://github.com/SUI-Components/lighthouse-action.git"
8+
},
9+
"license": "MIT",
10+
"scripts": {
11+
"co": "npx @s-ui/mono@2 commit",
12+
"phoenix": "npx @s-ui/mono phoenix",
13+
"serve:lhci": "NODE_ENV=production node server",
14+
"test": "npx @s-ui/test server -P './test/**/*.js'"
15+
},
16+
"dependencies": {
17+
"@s-ui/lint": "4",
18+
"@s-ui/test": "8",
19+
"chai": "4.3.6"
20+
},
21+
"eslintConfig": {
22+
"extends": [
23+
"./node_modules/@s-ui/lint/eslintrc.js"
24+
]
1125
},
12-
"author": "Web Platform <enablers-frontend@adevinta.com>"
26+
"prettier": "./node_modules/@s-ui/lint/.prettierrc.js",
27+
"stylelint": {
28+
"extends": "./node_modules/@s-ui/lint/stylelint.config.js"
29+
}
1330
}

pull-request/action.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: 'Lighthouse CI for Pull Requests'
2+
description: 'Runs Lighthouse CI on a PR and posts the results as a comment'
3+
4+
inputs:
5+
urls:
6+
description: 'List of URL(s) to analyze'
7+
required: true
8+
9+
runs:
10+
using: "composite"
11+
steps:
12+
- run: |
13+
sudo apt-get update
14+
sudo apt-get install -y libgconf-2-4 libatk1.0-0 libatk-bridge2.0-0 libgdk-pixbuf2.0-0 libgtk-3-0 libgbm-dev libnss3-dev libxss-dev libasound2
15+
shell: bash
16+
- uses: browser-actions/setup-chrome@latest
17+
- run: |
18+
chrome --version
19+
which chrome
20+
shell: bash
21+
- name: Audit preview URL with Lighthouse
22+
id: lhci
23+
uses: treosh/lighthouse-ci-action@v3
24+
with:
25+
configPath: ./.lighthouserc.js
26+
urls: ${{inputs.urls}}
27+
- name: Format lighthouse score
28+
id: format_lhci_score
29+
uses: actions/github-script@v6
30+
with:
31+
script: |
32+
const path = require('path')
33+
const generateCommentFromResults = require(path.resolve('./src/generateCommentFromResults.js'))
34+
const results = ${{ steps.lhci.outputs.manifest }}
35+
const comment = generateCommentFromResults({results})
36+
core.setOutput("comment", comment);
37+
- name: Add comment to PR
38+
id: comment_to_pr
39+
uses: marocchino/sticky-pull-request-comment@v2
40+
with:
41+
hide_and_recreate: true
42+
hide_classify: "OUTDATED"
43+
number: ${{ github.event.issue.number }}
44+
message: |
45+
${{ steps.format_lhci_score.outputs.comment }}

server/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
var http = require('http'),
2-
fs = require('fs');
1+
const http = require('http')
32

3+
const html =
4+
'<!DOCTYPE html><html><head><title>Page Title</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>'
45

5-
const html = '<!DOCTYPE html><html><head><title>Page Title</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>';
6-
7-
http.createServer(function(request, response) {
8-
response.writeHeader(200, {"Content-Type": "text/html"});
9-
response.write(html);
10-
response.end();
11-
}).listen(8000);
6+
http
7+
.createServer(function (request, response) {
8+
response.writeHeader(200, {'Content-Type': 'text/html'})
9+
response.write(html)
10+
response.end()
11+
})
12+
.listen(8000)

src/fromResultsToCommentMapper.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// '⚡️ [Lighthouse report]'
2+
// `(${Object.values(links)[0]}) for the changes in this PR:`,
3+
4+
const getScore = res => {
5+
if (res >= 90) return '🟢'
6+
return res >= 50 ? '🟠' : '🔴'
7+
}
8+
9+
const getResultPart = ({summary, url}) => {
10+
return [
11+
'| Category | Score |',
12+
'| --- | --- |',
13+
`| ${getScore(summary.performance)} Performance | ${summary.performance} |`,
14+
`| ${getScore(summary.accessibility)} Accessibility | ${
15+
summary.accessibility
16+
} |`,
17+
`| ${getScore(summary['best-practices'])} Best practices | ${
18+
summary['best-practices']
19+
} |`,
20+
`| ${getScore(summary.seo)} SEO | ${summary.seo} |`,
21+
`| ${getScore(summary.pwa)} PWA | ${summary.pwa} |`,
22+
' ',
23+
`*Lighthouse ran on ${url}*`,
24+
' '
25+
].join('\n')
26+
}
27+
28+
const fromResultsToCommentMapper = (results = []) => {
29+
const comments = [
30+
'⚡️ [Lighthouse report]',
31+
' ',
32+
...results.map(getResultPart)
33+
]
34+
35+
const comment = comments.join('\n')
36+
37+
return comment
38+
}
39+
40+
module.exports = fromResultsToCommentMapper

src/generateCommentFromResults.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mapLhciResults = require('./mapLhciResults.js')
2+
const fromResultsToCommentMapper = require('./fromResultsToCommentMapper.js')
3+
4+
module.exports = ({results = []}) => {
5+
const formattedResults = mapLhciResults(results)
6+
const comment = fromResultsToCommentMapper(formattedResults)
7+
8+
return comment
9+
}

0 commit comments

Comments
 (0)