Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Dec 23, 2024
1 parent b91d9fe commit e92ca0b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test-annotation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'test-annotation'
on: # rebuild any PRs and main branch changes
pull_request:
push:

jobs:
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
if: contains(github.event.head_commit.message, 'check annotations')
steps:
- uses: actions/checkout@v3

- name: Set Node.js 22.x
uses: actions/setup-node@v3
with:
node-version: 22.x

- name: Install dependencies
run: npm ci

- name: Rebuild the dist/ directory
run: |
npm run build
npm run package
- run: |
npm run test:annotations
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set Node.js 21.x
- name: Set Node.js 22.x
uses: actions/setup-node@v3
with:
node-version: 21.x
node-version: 22.x

- name: Install dependencies
run: npm ci
Expand Down
12 changes: 12 additions & 0 deletions __tests__/utils/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {test} from '@jest/globals'
import {GithubUtil} from '../../src/utils/github'
import exp = require('node:constants')

test('github init successfully', async function () {
const githubUtil = new GithubUtil('1234')
Expand Down Expand Up @@ -65,6 +66,17 @@ test('build annotations', function () {
message: 'This line is not covered by a test'
}
])

const consoleMock = jest.spyOn(console, 'log').mockImplementation()

githubUtil.annotate(annotations)
expect(console.log).toHaveBeenCalledTimes(4)
expect(console.log).toHaveBeenCalledWith("::warning file=file1.txt,line=132,endLine=132::This line is not covered by a test")
expect(console.log).toHaveBeenCalledWith("::warning file=file1.txt,line=134,endLine=136::These lines are not covered by a test")
expect(console.log).toHaveBeenCalledWith("::warning file=file1.txt,line=1007,endLine=1007::This line is not covered by a test")
expect(console.log).toHaveBeenCalledWith("::warning file=test/dir/file1.txt,line=22,endLine=22::This line is not covered by a test")

consoleMock.mockRestore()
})

// @todo test for rest of github class
7 changes: 2 additions & 5 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function play(): Promise<void> {
var parsedCov = await parseGoCoverage(COVERAGE_FILE_PATH, 'go.mod')
} else {
// lcov default
var parsedCov = await parseLCov(COVERAGE_FILE_PATH, workspacePath)
var parsedCov = await parseLCov(COVERAGE_FILE_PATH)
}
// Sum up lines.found for each entry in parsedCov
const totalLines = parsedCov.reduce(
Expand Down Expand Up @@ -91,10 +91,7 @@ export async function play(): Promise<void> {
)

// 4. Annotate in github
await githubUtil.annotate({
referenceCommitHash: githubUtil.getPullRequestRef(),
annotations
})
githubUtil.annotate(annotations)
core.info('Annotation done')
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
Expand Down
18 changes: 4 additions & 14 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,14 @@ export class GithubUtil {
}

/**
* https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run
* https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#update-a-check-run
* https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-warning-message
*/
async annotate(input: InputAnnotateParams): Promise<number> {
if (input.annotations.length === 0) {
return 0
}
for (const ann of input.annotations) {
annotate(annotations: Annotations[]): void {
for (const ann of annotations) {
console.log(
`::warning file=${ann.path},line=${ann.start_line},endLine=${ann.end_line}::${ann.message}`
`::${ann.annotation_level} file=${ann.path},line=${ann.start_line},endLine=${ann.end_line}::${ann.message}`
)
}
return 0
}

buildAnnotations(
Expand Down Expand Up @@ -97,11 +92,6 @@ export class GithubUtil {
}
}

type InputAnnotateParams = {
referenceCommitHash: string
annotations: Annotations[]
}

type Annotations = {
path: string
start_line: number
Expand Down

0 comments on commit e92ca0b

Please sign in to comment.