-
Notifications
You must be signed in to change notification settings - Fork 365
132 lines (108 loc) · 5.36 KB
/
stats-comment.yml
File metadata and controls
132 lines (108 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: Stats comment
on:
workflow_call:
workflow_dispatch:
permissions:
contents: read
pull-requests: write # For writing comments
jobs:
generate-stats:
name: Generate stats
runs-on: ubuntu-22.04
# Skip workflows other than PRs such as merges to `main` but
# also when token write permissions are unavailable on forks
if: ${{ github.event.pull_request && !github.event.pull_request.head.repo.fork }}
steps:
- name: Cache stats
uses: actions/cache@v5.0.4
id: stats-cache
with:
# Restore build cache, unless commit SHA or base ref changes
key: build-stats-${{ github.base_ref }}-${{ github.sha }}
path: |
./file-sizes-base.json
./modules-sizes-base.json
./file-sizes-head.json
./modules-sizes-head.json
- name: Checkout base branch
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v6.0.2
with:
ref: ${{ github.base_ref }}
- name: Restore base dependencies
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: ./.github/workflows/actions/install-node
- name: Build npm package and GitHub release for base
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
run: |
npm run build:package --workspace govuk-frontend
npm run build:release --workspace govuk-frontend
- name: Get base file stats
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: actions/github-script@v8.0.0
with:
script: |
const { writeFileSync } = await import('fs')
const { getModuleFileSizes, getAllFileSizes } = await import('${{ github.workspace }}/shared/stats/src/index.mjs')
const fileSizes = await getAllFileSizes('${{ github.workspace }}')
const modulesSizes = await getModuleFileSizes()
writeFileSync('./file-sizes-base.json', JSON.stringify(fileSizes))
writeFileSync('./modules-sizes-base.json', JSON.stringify(modulesSizes))
- name: Checkout head
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v6.0.2
with:
ref: ${{ github.head_ref }}
clean: false # So that we retain the stats generated from base
- name: Restore head dependencies
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: ./.github/workflows/actions/install-node
- name: Build npm package and GitHub release for head
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
run: |
npm run build:package --workspace govuk-frontend
npm run build:release --workspace govuk-frontend
- name: Get head file stats
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: actions/github-script@v8.0.0
with:
script: |
const { writeFileSync } = await import('fs')
const { getModuleFileSizes, getAllFileSizes } = await import('${{ github.workspace }}/shared/stats/src/index.mjs')
const fileSizes = await getAllFileSizes('${{ github.workspace }}')
const modulesSizes = await getModuleFileSizes()
writeFileSync('./file-sizes-head.json', JSON.stringify(fileSizes))
writeFileSync('./modules-sizes-head.json', JSON.stringify(modulesSizes))
- name: Compare base and head and add comment to PR
# Skip build when we’ve built this SHA before
if: steps.stats-cache.outputs.cache-hit != 'true'
uses: actions/github-script@v8.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { readFileSync } = await import('fs')
const { commentStats } = await import('${{ github.workspace }}/shared/github-scripts/comments.mjs')
const { getFileSizeComparison } = await import('${{ github.workspace }}/shared/lib/files.js')
// Get base and head temp files and compare
const baseFileSizes = JSON.parse(readFileSync('./file-sizes-base.json', 'utf8'))
const baseModulesSizes = JSON.parse(readFileSync('./modules-sizes-base.json', 'utf8'))
const headFileSizes = JSON.parse(readFileSync('./file-sizes-head.json', 'utf8'))
const headModulesSizes = JSON.parse(readFileSync('./modules-sizes-head.json', 'utf8'))
const comparedFileSizes = getFileSizeComparison(headFileSizes, baseFileSizes)
const comparedModulesSizes = getFileSizeComparison(headModulesSizes, baseModulesSizes)
// PR information
const issueNumber = ${{ github.event.pull_request.number }}
const commit = '${{ github.event.pull_request.head.sha }}'
const options = {
titleText: ':clipboard: Stats',
markerText: 'stats'
}
await commentStats(comparedFileSizes, comparedModulesSizes, { github, context, commit }, issueNumber, options)