diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 82b5486..88726d9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,11 +11,12 @@ jobs: uses: Equip-Collaboration/diff-line-numbers@v1.0.0 with: include: '["\\.ts$"]' + node-version: '16' - name: Print line numbers of changed lines run: echo Line numbers = ${{ toJSON(steps.diff.outputs.lineNumbers) }} - name: Detecting files changed id: files - uses: futuratrepadeira/changed-files@v3.2.1 + uses: umani/changed-files@v4.0.1 with: repo-token: ${{ github.token }} pattern: '^.*\.ts$' @@ -34,3 +35,4 @@ jobs: files-deleted: ${{steps.files.outputs.files_deleted}} line-numbers: ${{steps.diff.outputs.lineNumbers}} debug: true + output-behaviour: 'both' diff --git a/README.md b/README.md index 96f9de3..4ea7a73 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ jobs: files-deleted: ${{steps.files.outputs.files_deleted}} line-numbers: ${{steps.diff.outputs.lineNumbers}} output-behaviour: both + comment-behaviour: new ``` ## Customize the check @@ -83,6 +84,13 @@ Value|Behaviour `annotate` | Uses github line annotations with the errors found for this run. `both` | Does both of the above. +The comment behaviour depends on the value of `comment-behaviour` + +Value|Behaviour +-- | -- +`new` | Default, adds a new comment for every run of the action. +`edit` | Updates a previous run's comment, if one exists, otherwise creates a new comment. + ## Use a specific tsconfig file By default, this actions uses tsconfig file located at './tsconfig.json' diff --git a/action.yml b/action.yml index c4fe6ae..d01ec7c 100644 --- a/action.yml +++ b/action.yml @@ -1,39 +1,42 @@ -name: 'Action Check Typescript errors' -description: 'Check tsc errors and compare errors wih base branch' -author: 'Arhia' +name: "Action Check Typescript errors" +description: "Check tsc errors and compare errors wih base branch" +author: "Arhia" icon: box inputs: repo-token: - description: 'Token for the repository. Can be passed in using {{ secrets.GITHUB_TOKEN }}' + description: "Token for the repository. Can be passed in using {{ secrets.GITHUB_TOKEN }}" required: true directory: description: "Directory where to run install and build. Default is '.'" - default: '.' + default: "." files-changed: description: "List of files changed in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that" - default: '' + default: "" files-added: description: "List of files added in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that" - default: '' + default: "" files-deleted: description: "List of files deleted in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that" - default: '' + default: "" line-numbers: description: "List of files with added and removed lines, use action 'Equip-Collaboration/diff-line-numbers' for that" ts-config-path: description: "Path of tsconfig file. default is './tsconfig.json'" - default: './tsconfig.json' + default: "./tsconfig.json" use-check: - description: 'Report status as a CI Check' + description: "Report status as a CI Check" check-fail-mode: description: "Allowed values : added, errors_in_pr, errors_in_code" required: true output-behaviour: description: "Allowed values : comment, annotate, both" default: "comment" + comment-behaviour: + description: "Allowed values : new, edit" + default: "new" debug: description: "Set true to log ts errors in base branch and pr branch" default: false runs: - using: 'node16' - main: 'dist/index.js' + using: "node16" + main: "dist/index.js" diff --git a/src/fakeErrors.ts b/src/fakeErrors.ts index 3c60c64..01a392f 100644 --- a/src/fakeErrors.ts +++ b/src/fakeErrors.ts @@ -1,8 +1,11 @@ let foo: string = 'EE' -//foo = 1 +foo = 1 const bar = 1 -//bar = 2 \ No newline at end of file +bar = 2 + +let foo2: string | number = true +// again again again \ No newline at end of file diff --git a/src/getAndValidateArgs.ts b/src/getAndValidateArgs.ts index 8d14dba..9f7805c 100644 --- a/src/getAndValidateArgs.ts +++ b/src/getAndValidateArgs.ts @@ -12,6 +12,11 @@ export const enum OUTPUT_BEHAVIOUR { COMMENT_AND_ANNOTATE = 'both' } +export const enum COMMENT_BEHAVIOUR { + NEW = 'new', + EDIT = 'edit' +} + type Args = { repoToken: string directory: string @@ -47,6 +52,7 @@ type Args = { useCheck: boolean checkFailMode: CHECK_FAIL_MODE outputBehaviour: OUTPUT_BEHAVIOUR + commentBehaviour: COMMENT_BEHAVIOUR debug: boolean } @@ -62,6 +68,7 @@ export function getAndValidateArgs(): Args { useCheck: getBooleanInput('use-check'), checkFailMode: getInput('check-fail-mode', { required: true }) as CHECK_FAIL_MODE, outputBehaviour: getInput('output-behaviour') as OUTPUT_BEHAVIOUR, + commentBehaviour: getInput('comment-behaviour') as COMMENT_BEHAVIOUR, debug: getBooleanInput('debug') } @@ -81,5 +88,12 @@ export function getAndValidateArgs(): Args { throw new Error(`Invalid value ${args.outputBehaviour} for input output-behaviour`) } + if (![ + COMMENT_BEHAVIOUR.NEW, + COMMENT_BEHAVIOUR.EDIT, + ].includes(args.commentBehaviour)) { + throw new Error(`Invalid value ${args.commentBehaviour} for input comment-behaviour`) + } + return args } diff --git a/src/getBodyComment.ts b/src/getBodyComment.ts index 5fda8b8..3995513 100644 --- a/src/getBodyComment.ts +++ b/src/getBodyComment.ts @@ -1,6 +1,7 @@ import { ErrorTs } from "./main" const BLANK_LINE = ' \n' +export const COMMENT_TITLE = '## Typescript errors check' type Input = { errorsInProjectBefore: ErrorTs[] @@ -13,7 +14,7 @@ type Input = { export function getBodyComment({ errorsInProjectBefore, errorsInProjectAfter, errorsInModifiedFiles, newErrorsInProject }: Input): string { const delta = errorsInProjectAfter.length - errorsInProjectBefore.length - let s = `## Typescript errors check \n` + let s = `${COMMENT_TITLE} \n` const areStillErrors = !!errorsInProjectAfter.length diff --git a/src/main.ts b/src/main.ts index eb327aa..5897153 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,9 +5,9 @@ import { createCheck } from './createCheck' import * as github from '@actions/github' import * as fs from 'fs' import { parseTsConfigFile } from './tscHelpers/parseTsConfigFileToCompilerOptions' -import { getAndValidateArgs, CHECK_FAIL_MODE, OUTPUT_BEHAVIOUR } from './getAndValidateArgs' +import { getAndValidateArgs, CHECK_FAIL_MODE, OUTPUT_BEHAVIOUR, COMMENT_BEHAVIOUR } from './getAndValidateArgs' import { exec } from '@actions/exec' -import { getBodyComment } from './getBodyComment' +import { COMMENT_TITLE, getBodyComment } from './getBodyComment' import { checkoutAndInstallBaseBranch } from './checkoutAndInstallBaseBranch' import { compareErrors } from './compareErrors' import { runTscCli } from './tscHelpers/runTscCli' @@ -174,9 +174,11 @@ async function run(): Promise { if ([OUTPUT_BEHAVIOUR.COMMENT, OUTPUT_BEHAVIOUR.COMMENT_AND_ANNOTATE].includes(args.outputBehaviour)) { startGroup(`Creating comment`) + const issueNumber = context.payload.pull_request!.number + const commentInfo = { ...context.repo, - issue_number: context.payload.pull_request!.number + issue_number: issueNumber } const comment = { @@ -192,7 +194,17 @@ async function run(): Promise { info(`comment body obtained`) try { - await octokit.rest.issues.createComment(comment) + const existingComments = await octokit.rest.issues.listComments({owner: context.repo.owner, repo: context.repo.repo, issue_number: issueNumber}) + const existingComment = existingComments.data.find(c => !!c.body?.includes(COMMENT_TITLE)) + + if (args.commentBehaviour === COMMENT_BEHAVIOUR.EDIT && existingComment) { + await octokit.rest.issues.updateComment({ + comment_id: existingComment.id, + ...comment + }) + } else { + await octokit.rest.issues.createComment(comment) + } } catch (e) { info(`Error creating comment: ${(e as Error).message}`) info(`Submitting a PR review comment instead...`)