Skip to content

Commit 95e8e8d

Browse files
committed
Create annotations from relevant issues
1 parent 7beaee2 commit 95e8e8d

12 files changed

Lines changed: 318 additions & 25 deletions

File tree

__tests__/fixtures/code-pushup.config.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ const config: CoreConfig = {
2020
directory: fileURLToPath(dirname(import.meta.url)),
2121
pattern: /\.[jt]s$/
2222
})
23-
const jsFileCount = paths.filter(path => path.endsWith('.js')).length
24-
const tsFileCount = paths.filter(path => path.endsWith('.ts')).length
23+
const jsPaths = paths.filter(path => path.endsWith('.js'))
24+
const tsPaths = paths.filter(path => path.endsWith('.ts'))
25+
const jsFileCount = jsPaths.length
26+
const tsFileCount = tsPaths.length
2527
const ratio = tsFileCount / (jsFileCount + tsFileCount)
2628
const percentage = Math.round(ratio * 100)
2729
return [
2830
{
2931
slug: 'ts-files',
3032
value: percentage,
3133
score: ratio,
32-
displayValue: `${percentage}% converted`
34+
displayValue: `${percentage}% converted`,
35+
details: {
36+
issues: jsPaths.map(file => ({
37+
message: 'Use .ts file extension instead of .js',
38+
severity: 'warning',
39+
source: { file }
40+
}))
41+
}
3342
}
3443
]
3544
}

__tests__/main.test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ describe('code-pushup action', () => {
5959
return true
6060
case 'artifacts':
6161
return true
62+
case 'annotations':
63+
return true
6264
default:
6365
return false
6466
}
@@ -195,28 +197,31 @@ describe('code-pushup action', () => {
195197

196198
describe('pull request event', () => {
197199
beforeEach(async () => {
198-
github.context.payload = {
199-
pull_request: {
200-
number: 42,
201-
base: {
202-
ref: 'main'
203-
}
204-
}
205-
}
206-
207200
await git.checkoutLocalBranch('feature-1')
208201

209202
await rename(join(workDir, 'index.js'), join(workDir, 'index.ts'))
210203

211204
await git.add('index.ts')
212205
await git.commit('Convert JS file to TS')
206+
207+
github.context.payload = {
208+
pull_request: {
209+
number: 42,
210+
head: { ref: 'feature-1', sha: await git.revparse('feature-1') },
211+
base: { ref: 'main', sha: await git.revparse('main') }
212+
}
213+
}
213214
})
214215

215216
it('should compare reports', async () => {
216217
await run(artifact, git)
217218

218219
expect(core.setFailed).not.toHaveBeenCalled()
219220

221+
expect(core.error).toHaveBeenCalledTimes(0)
222+
expect(core.warning).toHaveBeenCalledTimes(0)
223+
expect(core.notice).toHaveBeenCalledTimes(0)
224+
220225
expect(core.setOutput).toHaveBeenCalledWith('comment-id', 10)
221226

222227
const mdPromise = readFile(

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ inputs:
3131
retention:
3232
description: Artifact retention period in days
3333
required: false
34+
annotations:
35+
description: Create file annotations in GitHub
36+
required: true
37+
default: true
3438

3539
outputs:
3640
artifact-id:

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 145 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/annotations.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as core from '@actions/core'
2+
import type { SourceFileIssue } from './issues'
3+
4+
export function createAnnotationsFromIssues(issues: SourceFileIssue[]): void {
5+
for (const issue of issues) {
6+
const message = issue.message
7+
const properties: core.AnnotationProperties = {
8+
title: issue.audit.title,
9+
file: issue.source.file,
10+
startLine: issue.source.position?.startLine,
11+
startColumn: issue.source.position?.startColumn,
12+
endLine: issue.source.position?.endLine,
13+
endColumn: issue.source.position?.endColumn
14+
}
15+
16+
switch (issue.severity) {
17+
case 'error':
18+
core.error(message, properties)
19+
break
20+
case 'warning':
21+
core.warning(message, properties)
22+
break
23+
case 'info':
24+
core.notice(message, properties)
25+
break
26+
}
27+
}
28+
}

src/diff.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { DiffNameStatus, simpleGit } from 'simple-git'
2+
3+
export async function listChangedFiles(
4+
refs: {
5+
base: { ref: string; sha: string }
6+
head: { ref: string; sha: string }
7+
},
8+
git = simpleGit()
9+
): Promise<string[]> {
10+
const statuses: DiffNameStatus[] = [
11+
DiffNameStatus.ADDED,
12+
DiffNameStatus.MODIFIED
13+
]
14+
const { files } = await git.diffSummary([
15+
`--diff-filter=${statuses.join('')}`,
16+
refs.base.sha,
17+
refs.head.sha
18+
])
19+
return files.filter(({ binary }) => !binary).map(({ file }) => file)
20+
}

src/inputs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type ActionInputs = {
99
silent: boolean
1010
artifacts: boolean
1111
retention: number | null
12+
annotations: boolean
1213
}
1314

1415
export function parseInputs(): ActionInputs {
@@ -19,6 +20,7 @@ export function parseInputs(): ActionInputs {
1920
const silent = core.getBooleanInput('silent')
2021
const artifacts = core.getBooleanInput('artifacts')
2122
const retention = parseInteger(core.getInput('retention'))
23+
const annotations = core.getBooleanInput('annotations')
2224

2325
return {
2426
token,
@@ -27,7 +29,8 @@ export function parseInputs(): ActionInputs {
2729
directory,
2830
silent,
2931
artifacts,
30-
retention
32+
retention,
33+
annotations
3134
}
3235
}
3336

0 commit comments

Comments
 (0)