Skip to content
This repository has been archived by the owner on Sep 29, 2022. It is now read-only.

Commit

Permalink
Guard from empty reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Aug 8, 2022
1 parent 9248da4 commit 30c3aec
Show file tree
Hide file tree
Showing 9 changed files with 668 additions and 61 deletions.
41 changes: 24 additions & 17 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import {expect, test} from '@jest/globals'
import {median, sum} from 'simple-statistics'
import dedent from 'dedent'
import * as github from '@actions/github'
import * as fs from 'fs'
import * as path from 'path'

github.context.ref = 'refs/heads/main'
github.context.sha = '1234'
const PR = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'pr.json')).toString()
)

github.context.ref = PR.ref
github.context.sha = PR.sha
github.context.payload = PR.payload

test('get fixture stats', async () => {
const results: Map<string, Array<number>> = await getDefaultBranchStats(
Expand All @@ -26,49 +33,49 @@ test('get fixture stats', async () => {
})

test('fromTemplate rendering ok', async () => {
const res = fromTemplate(42, 300, 200, 250, 190)
const res = fromTemplate(200, 300, 42, 250, 190)
expect(res).toEqual(dedent`### BlueRacer unit tests performance report: ✅
Everything looks great, carry on!
Here are some details:
| Branch | Number of tests | Total duration
| Branch | Number of tests | Total duration | Duration per 100 tests |
|-|-|-|-|
| \`default branch\`[^1]|200|300.0s
| \`refs/heads/main\`[^2]|190 (-5%)|250.0s (-18%)
| \`master\`[^1]|200|300.0s | 150s |
| \`test/blueracer-v1\`[^2]|190 (-5%)|250.0s (-18%) | 131.6s (-13%) |
[^1]: The previous 42 runs.
[^2]: More specifically, [commit \`1234\`](https://github.com/teamniteo/blueracer/commit/1234).`)
[^2]: More specifically, [commit \`f9a8408029fa50468a4c98fefa42b8acd6eb8220\`](https://github.com/teamniteo/minisites/commit/f9a8408029fa50468a4c98fefa42b8acd6eb8220).`)
})

test('fromTemplate rendering meh', async () => {
const res = fromTemplate(42, 300, 200, 350, 215)
const res = fromTemplate(200, 300, 42, 350, 215)
expect(res).toEqual(dedent`### BlueRacer unit tests performance report: 👀
Everything looks great, carry on!
Here are some details:
| Branch | Number of tests | Total duration
| Branch | Number of tests | Total duration | Duration per 100 tests |
|-|-|-|-|
| \`default branch\`[^1]|200|300.0s
| \`refs/heads/main\`[^2]|215 (7%)|350.0s (15%)
| \`master\`[^1]|200|300.0s | 150s |
| \`test/blueracer-v1\`[^2]|215 (7%)|350.0s (15%) | 162.8s (8%) |
[^1]: The previous 42 runs.
[^2]: More specifically, [commit \`1234\`](https://github.com/teamniteo/blueracer/commit/1234).`)
[^2]: More specifically, [commit \`f9a8408029fa50468a4c98fefa42b8acd6eb8220\`](https://github.com/teamniteo/minisites/commit/f9a8408029fa50468a4c98fefa42b8acd6eb8220).`)
})

test('fromTemplate rendering fail', async () => {
const res = fromTemplate(42, 300, 200, 950, 215)
const res = fromTemplate(200, 300, 42, 950, 215)
expect(res).toEqual(dedent`### BlueRacer unit tests performance report: ❌
Everything looks great, carry on!
Here are some details:
| Branch | Number of tests | Total duration
| Branch | Number of tests | Total duration | Duration per 100 tests |
|-|-|-|-|
| \`default branch\`[^1]|200|300.0s
| \`refs/heads/main\`[^2]|215 (7%)|950.0s (104%)
| \`master\`[^1]|200|300.0s | 150s |
| \`test/blueracer-v1\`[^2]|215 (7%)|950.0s (104%) | 441.9s (99%) |
[^1]: The previous 42 runs.
[^2]: More specifically, [commit \`1234\`](https://github.com/teamniteo/blueracer/commit/1234).`)
[^2]: More specifically, [commit \`f9a8408029fa50468a4c98fefa42b8acd6eb8220\`](https://github.com/teamniteo/minisites/commit/f9a8408029fa50468a4c98fefa42b8acd6eb8220).`)
})
505 changes: 505 additions & 0 deletions __tests__/pr.json

Large diffs are not rendered by default.

89 changes: 72 additions & 17 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as cache from '@actions/cache'
import * as io from '@actions/io'
import * as C from './constants'
import * as path from 'path'
import * as fs from 'fs'

export async function restore(): Promise<boolean> {
io.mkdirP(C.reportsPath)
return cache.restoreCache([C.reportsPath], C.reportsKey) !== undefined
const cacheHit =
(await cache.restoreCache([C.reportsPath], C.reportsKey)) !== undefined
const reportsPath = path.join(process.cwd(), C.reportsPath)
const files = fs.readdirSync(reportsPath)
core.debug(`Current reportsPath ${reportsPath}`)
core.debug(`Current cacheHit ${cacheHit}`)
core.debug(`Current reportsPath`)
core.debug(files.join('\n'))

return cacheHit && files.length > 0
}

export async function storeAndGC(file: string): Promise<void> {
Expand Down
9 changes: 8 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
import * as github from '@actions/github'

const defualtBranchSHA =
(github.context.payload.pull_request &&
github.context.payload.pull_request.base.sha) ||
github.context.sha

export const reportsPath = '.blueracer-reports'
export const reportsKey = 'blueracer-reports'
export const reportsKey = `blueracer-reports-${defualtBranchSHA}`
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function run(): Promise<void> {
const file: string = path.join(cwd(), core.getInput('durations-file'))

const ref = github.context.ref
core.debug(`Current github.context ${JSON.stringify(github.context)}`)

// on default branch
if (ref.includes('refs/heads/')) {
Expand All @@ -27,7 +28,7 @@ async function run(): Promise<void> {
if (!masterDataPresent) {
core.setOutput(
'report',
'No data yet, run this action in default branch.'
'### BlueRacer unit tests performance report: 🔎\n\nNo data yet, run this action in default branch.'
)
return
}
Expand All @@ -40,6 +41,7 @@ async function run(): Promise<void> {
}
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
if (error instanceof Error) core.debug(error.stack || 'No stacktrace')
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export async function report(file: string): Promise<string> {
const currentSum = sum(current)

return fromTemplate(
medianNumberOfTests,
medianDefault,
results.size,
currentSum,
current.length
medianNumberOfTests, // median number of tests
medianDefault, // median duration of tests
results.size, // number of commits
currentSum, // current tests duration
current.length // current number of tests
)
}
Loading

0 comments on commit 30c3aec

Please sign in to comment.