Skip to content

update README Text and Fix 'Body is Too Long' Error #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
</p>

# action-check-typescript
# action-check-typescript

A Github action that compile ts files and display the errors found in whole codebase and in files changed in PR.
`action-check-typescript` is a GitHub action designed to compile TypeScript files and display the errors found throughout the entire codebase as well as in files changed in pull requests (PRs).

**Used in production at [Arhia](https://github.com/Arhia)**
This action is currently used in production at [Arhia](https://github.com/Arhia).

- Automatically uses `yarn` or `npm ci` when lockfiles are present
- Builds your PR, then run tsc to detect errors 🔍
- Compare ts errors in base branch and in PR branch to detect new errors (handle automatically line numbers offset) 💪
- Doesn't upload anything or rely on centralized storage 👐
## Features

- Detects and utilizes `yarn` or `npm ci` automatically when lockfiles are detected
- Builds your PR, then runs `tsc` to detect any errors 🔍
- Compares TypeScript errors between the base branch and PR branch to identify new errors (automatically handles line number offsets) 💪
- Does not upload any data or rely on centralized storage 👐

## Usage

action-check-typescript rely on two other actions (big thanks to the authors ! ❤️) to :
- Equip-Collaboration/diff-line-numbers : retrieve line numbers of added and removed lines in files changed
- futuratrepadeira/changed-files : list files added, modified and deleted
`action-check-typescript` relies on two other actions (big thanks to the authors ! ❤️) to:

- `Equip-Collaboration/diff-line-numbers`: Retrieve line numbers of added and removed lines in changed files.
- `futuratrepadeira/changed-files`: List files added, modified, and deleted.

Add a workflow (eg `.github/workflows/check_ts.yml`):
To use `action-check-typescript`, add a workflow (for example, `.github/workflows/check_ts.yml`):

```yaml
name: 'check-ts'
name: check-ts
on:
pull_request:
jobs:
Expand All @@ -46,7 +49,7 @@ jobs:
echo 'Files modified: ${{steps.files.outputs.files_updated}}'
echo 'Files added: ${{steps.files.outputs.files_created}}'
echo 'Files removed: ${{steps.files.outputs.files_deleted}}'
- uses: Arhia/action-check-typescript@v1.0.0
- uses: Arhia/action-check-typescript@v1.1.0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
use-check: true
Expand All @@ -58,49 +61,48 @@ jobs:
output-behaviour: both
comment-behaviour: new
```
## Customize the check

By default, this action doesn't perform a status check (aka pass/fail).
## Customizing the Check

By default, this action does not perform a status check (i.e., pass or fail).

You need to set `use-check` on true to run a status check.
You need to set `use-check` to `true` to enable a status check:

```yaml
use-check: true
use-check: true
```

How the check status is determined depends on the value of `check-fail-mode`
The determination of check status is based on the value of `check-fail-mode`.

Value|Behaviour
-- | --
`added`| Check fails if some errors are added in the files added/modified in the PR branch.
`errors_in_pr`| Check fails if any errors are present in the files added/modified in the PR branch (even if already in base branche).
`errors_in_code`| Check fails if any errors are present in the whole branch.
| Value | Behaviour |
| ---------------- | -------------------------------------------------------------------------------------------------------------------- |
| `added` | Check fails if some errors are added in the files added/modified in the PR branch. |
| `errors_in_pr` | Check fails if any errors are present in the files added/modified in the PR branch (even if already in base branch). |
| `errors_in_code` | Check fails if any errors are present in the entire branch. |

The output behaviour depends on the value of `output-behaviour`
The output behaviour is determined by the value of `output-behaviour`:

Value|Behaviour
-- | --
`comment` | Default, comments on the PR with the errors found for this run.
`annotate` | Uses github line annotations with the errors found for this run.
`both` | Does both of the above.
| Value | Behaviour |
| ---------- | ---------------------------------------------------------------- |
| `comment` | Default, comments on the PR with the errors found for this run. |
| `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`
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.
| 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
## Using a Specific tsconfig File

By default, this actions uses tsconfig file located at './tsconfig.json'
By default, this action uses the tsconfig file located at `./tsconfig.json`.

You may want to use a different file for this action, in order to change tsc behaviour.
For example, if you use `watch:true` in your regular tsconfig file, you should disable watching mode.

In order to do this, you would create a specific tsconfig file (eg name `tsconfig.check.ts`) and setting accordingly the
parameter `ts-config-path` :
If you need to use a different file for this action to change the behavior of `tsc`, you can create a specific tsconfig file (e.g., `tsconfig.check.json`) and set the `ts-config-path` parameter accordingly:

```yml
ts-config-path: './tsconfig.check.json'
ts-config-path: "./tsconfig.check.json"
```

For instance, if you use `watch:true` in your regular tsconfig file, you should disable the watching mode for this action.
24 changes: 12 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
name: "Action Check Typescript errors"
description: "Check tsc errors and compare errors wih base branch"
name: "Check TypeScript Errors Action"
description: "Check TypeScript errors and compare them with the base branch"
author: "Arhia"
icon: box
inputs:
repo-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 '.'"
description: "Directory to run install and build in. Default is '.'"
default: "."
files-changed:
description: "List of files changed in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that"
description: "List of files changed in the current PR (separated by space). Use action 'futuratrepadeira/changed-files'"
default: ""
files-added:
description: "List of files added in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that"
description: "List of files added in the current PR (separated by space). Use action 'futuratrepadeira/changed-files'"
default: ""
files-deleted:
description: "List of files deleted in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that"
description: "List of files deleted in the current PR (separated by space). Use action 'futuratrepadeira/changed-files'"
default: ""
line-numbers:
description: "List of files with added and removed lines, use action 'Equip-Collaboration/diff-line-numbers' for that"
description: "List of files with added and removed lines. Use action 'Equip-Collaboration/diff-line-numbers'"
ts-config-path:
description: "Path of tsconfig file. default is './tsconfig.json'"
description: "Path of tsconfig file. Default is './tsconfig.json'"
default: "./tsconfig.json"
use-check:
description: "Report status as a CI Check"
check-fail-mode:
description: "Allowed values : added, errors_in_pr, errors_in_code"
description: "Allowed values: added, errors_in_pr, errors_in_code"
required: true
output-behaviour:
description: "Allowed values : comment, annotate, both"
description: "Allowed values: comment, annotate, both"
default: "comment"
comment-behaviour:
description: "Allowed values : new, edit"
description: "Allowed values: new, edit"
default: "new"
debug:
description: "Set true to log ts errors in base branch and pr branch"
description: "Set true to log TypeScript errors in base branch and pull request branch"
default: false
runs:
using: "node16"
Expand Down
31 changes: 29 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ exports.getAndValidateArgs = getAndValidateArgs;
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.escapeForMarkdown = exports.getBodyComment = exports.COMMENT_TITLE = void 0;
exports.shortenMessage = exports.escapeForMarkdown = exports.getBodyComment = exports.COMMENT_TITLE = void 0;
const BLANK_LINE = ' \n';
exports.COMMENT_TITLE = '## Typescript errors check';
function getBodyComment({ errorsInProjectBefore, errorsInProjectAfter, errorsInModifiedFiles, newErrorsInProject }) {
Expand Down Expand Up @@ -267,6 +267,10 @@ function getBodyComment({ errorsInProjectBefore, errorsInProjectAfter, errorsInM
s += BLANK_LINE;
s += BLANK_LINE;
s += '<a href="https://github.com/Arhia/action-check-typescript"><sub>Arhia/action-check-typescript</sub></a>';
// Github comment body maximum is 65536 characters
if (s.length > 60000) {
s = s.substring(0, 59997) + '...';
}
return s;
}
exports.getBodyComment = getBodyComment;
Expand All @@ -284,7 +288,8 @@ function getListOfErrors(title, errors, thresholdCollapse = 5) {
s += `\nFilename|Location|Message\n`;
s += `-- | -- | -- \n`;
s += errors.map(err => {
return `${err.fileName}|${err.line}, ${err.column}|${escapeForMarkdown(err.message)}`;
const message = escapeForMarkdown(shortenMessage(err.message));
return `${err.fileName}|${err.line}, ${err.column}|${message}`;
}).join('\n');
if (shouldUseCollapsible) {
s += BLANK_LINE;
Expand Down Expand Up @@ -335,6 +340,28 @@ function getNbOfErrorsByFile(title, errors, thresholdCollapse = 5) {
}
return s;
}
/**
* Function to intelligently shorten TypeScript error messages.
* It shortens the quoted types within the messages to a specified length (default 100 characters).
*
* @param {string} s - The TypeScript error message to be shortened.
* @param {number} [maxLength=100] - The maximum length for the error message.
*
* @returns {string} - The shortened TypeScript error message.
*/
function shortenMessage(s, maxLength = 100) {
const trimmedStr = s.replace(/'(.*?)'($|[\s.,])/g, (match, p1, p2) => {
if (p1.length > 50) {
return `'${p1.substring(0, 47)}...'${p2}`;
}
return `'${p1}'${p2}`;
});
if (trimmedStr.length > maxLength) {
return `${trimmedStr.substring(0, maxLength - 3)}...`;
}
return trimmedStr;
}
exports.shortenMessage = shortenMessage;


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

35 changes: 32 additions & 3 deletions src/getBodyComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ export function getBodyComment({ errorsInProjectBefore, errorsInProjectAfter, er
s += BLANK_LINE
s += '<a href="https://github.com/Arhia/action-check-typescript"><sub>Arhia/action-check-typescript</sub></a>'

return s

// Github comment body maximum is 65536 characters
if(s.length > 60000) {
s = s.substring(0, 59997) + '...';
}
return s;
}

function getListOfErrors(title: string, errors: ErrorTs[], thresholdCollapse = 5): string {
Expand All @@ -88,7 +91,8 @@ function getListOfErrors(title: string, errors: ErrorTs[], thresholdCollapse = 5
s += `\nFilename|Location|Message\n`
s += `-- | -- | -- \n`
s += errors.map(err => {
return `${err.fileName}|${err.line}, ${err.column}|${escapeForMarkdown(err.message)}`
const message = escapeForMarkdown(shortenMessage(err.message))
return `${err.fileName}|${err.line}, ${err.column}|${message}`
}).join('\n')


Expand Down Expand Up @@ -155,3 +159,28 @@ function getNbOfErrorsByFile(title: string, errors: ErrorTs[], thresholdCollapse
return s

}


/**
* Function to intelligently shorten TypeScript error messages.
* It shortens the quoted types within the messages to a specified length (default 100 characters).
*
* @param {string} s - The TypeScript error message to be shortened.
* @param {number} [maxLength=100] - The maximum length for the error message.
*
* @returns {string} - The shortened TypeScript error message.
*/
export function shortenMessage(s: string, maxLength: number = 100): string {
const trimmedStr = s.replace(/'(.*?)'($|[\s.,])/g, (match, p1: string, p2: string) => {
if(p1.length > 50) {
return `'${p1.substring(0, 47)}...'${p2}`;
}
return `'${p1}'${p2}`;
});

if(trimmedStr.length > maxLength) {
return `${trimmedStr.substring(0, maxLength-3)}...`;
}

return trimmedStr;
}