From e92ca0bf37ffeda90ac58f12753b8c6cb19764b8 Mon Sep 17 00:00:00 2001 From: Tofandel Date: Wed, 11 Dec 2024 15:15:10 +0100 Subject: [PATCH] Cleanup --- .github/workflows/test-annotation.yml | 27 +++++++++++++++++++++++++++ .github/workflows/test.yml | 4 ++-- __tests__/utils/github.test.ts | 12 ++++++++++++ src/action.ts | 7 ++----- src/utils/github.ts | 18 ++++-------------- 5 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/test-annotation.yml diff --git a/.github/workflows/test-annotation.yml b/.github/workflows/test-annotation.yml new file mode 100644 index 0000000..597146a --- /dev/null +++ b/.github/workflows/test-annotation.yml @@ -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 + diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d07a4bd..2768f82 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/__tests__/utils/github.test.ts b/__tests__/utils/github.test.ts index 88ac29e..73e7567 100644 --- a/__tests__/utils/github.test.ts +++ b/__tests__/utils/github.test.ts @@ -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') @@ -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 diff --git a/src/action.ts b/src/action.ts index fa0c8ff..d11480c 100644 --- a/src/action.ts +++ b/src/action.ts @@ -54,7 +54,7 @@ export async function play(): Promise { 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( @@ -91,10 +91,7 @@ export async function play(): Promise { ) // 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) diff --git a/src/utils/github.ts b/src/utils/github.ts index 53d2343..32748f4 100644 --- a/src/utils/github.ts +++ b/src/utils/github.ts @@ -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 { - 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( @@ -97,11 +92,6 @@ export class GithubUtil { } } -type InputAnnotateParams = { - referenceCommitHash: string - annotations: Annotations[] -} - type Annotations = { path: string start_line: number