Skip to content

Commit e3f8a33

Browse files
josephluckabenhamdine
authored andcommitted
(ISSUELESS) Support editing existing comment if one exists
1 parent bea17d7 commit e3f8a33

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
files-deleted: ${{steps.files.outputs.files_deleted}}
5757
line-numbers: ${{steps.diff.outputs.lineNumbers}}
5858
output-behaviour: both
59+
comment-behaviour: new
5960
```
6061
## Customize the check
6162
@@ -83,6 +84,13 @@ Value|Behaviour
8384
`annotate` | Uses github line annotations with the errors found for this run.
8485
`both` | Does both of the above.
8586

87+
The comment behaviour depends on the value of `comment-behaviour`
88+
89+
Value|Behaviour
90+
-- | --
91+
`new` | Default, adds a new comment for every run of the action.
92+
`edit` | Updates a previous run's comment, if one exists, otherwise creates a new comment.
93+
8694
## Use a specific tsconfig file
8795

8896
By default, this actions uses tsconfig file located at './tsconfig.json'

action.yml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
name: 'Action Check Typescript errors'
2-
description: 'Check tsc errors and compare errors wih base branch'
3-
author: 'Arhia'
1+
name: "Action Check Typescript errors"
2+
description: "Check tsc errors and compare errors wih base branch"
3+
author: "Arhia"
44
icon: box
55
inputs:
66
repo-token:
7-
description: 'Token for the repository. Can be passed in using {{ secrets.GITHUB_TOKEN }}'
7+
description: "Token for the repository. Can be passed in using {{ secrets.GITHUB_TOKEN }}"
88
required: true
99
directory:
1010
description: "Directory where to run install and build. Default is '.'"
11-
default: '.'
11+
default: "."
1212
files-changed:
1313
description: "List of files changed in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that"
14-
default: ''
14+
default: ""
1515
files-added:
1616
description: "List of files added in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that"
17-
default: ''
17+
default: ""
1818
files-deleted:
1919
description: "List of files deleted in the current PR (separated by space), use action 'futuratrepadeira/changed-files' for that"
20-
default: ''
20+
default: ""
2121
line-numbers:
2222
description: "List of files with added and removed lines, use action 'Equip-Collaboration/diff-line-numbers' for that"
2323
ts-config-path:
2424
description: "Path of tsconfig file. default is './tsconfig.json'"
25-
default: './tsconfig.json'
25+
default: "./tsconfig.json"
2626
use-check:
27-
description: 'Report status as a CI Check'
27+
description: "Report status as a CI Check"
2828
check-fail-mode:
2929
description: "Allowed values : added, errors_in_pr, errors_in_code"
3030
required: true
3131
output-behaviour:
3232
description: "Allowed values : comment, annotate, both"
3333
default: "comment"
34+
comment-behaviour:
35+
description: "Allowed values : new, edit"
36+
default: "new"
3437
debug:
3538
description: "Set true to log ts errors in base branch and pr branch"
3639
default: false
3740
runs:
38-
using: 'node16'
39-
main: 'dist/index.js'
41+
using: "node16"
42+
main: "dist/index.js"

src/getAndValidateArgs.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export const enum OUTPUT_BEHAVIOUR {
1212
COMMENT_AND_ANNOTATE = 'both'
1313
}
1414

15+
export const enum COMMENT_BEHAVIOUR {
16+
NEW = 'new',
17+
EDIT = 'edit'
18+
}
19+
1520
type Args = {
1621
repoToken: string
1722
directory: string
@@ -47,6 +52,7 @@ type Args = {
4752
useCheck: boolean
4853
checkFailMode: CHECK_FAIL_MODE
4954
outputBehaviour: OUTPUT_BEHAVIOUR
55+
commentBehaviour: COMMENT_BEHAVIOUR
5056
debug: boolean
5157
}
5258

@@ -62,6 +68,7 @@ export function getAndValidateArgs(): Args {
6268
useCheck: getBooleanInput('use-check'),
6369
checkFailMode: getInput('check-fail-mode', { required: true }) as CHECK_FAIL_MODE,
6470
outputBehaviour: getInput('output-behaviour') as OUTPUT_BEHAVIOUR,
71+
commentBehaviour: getInput('comment-behaviour') as COMMENT_BEHAVIOUR,
6572
debug: getBooleanInput('debug')
6673
}
6774

@@ -81,5 +88,12 @@ export function getAndValidateArgs(): Args {
8188
throw new Error(`Invalid value ${args.outputBehaviour} for input output-behaviour`)
8289
}
8390

91+
if (![
92+
COMMENT_BEHAVIOUR.NEW,
93+
COMMENT_BEHAVIOUR.EDIT,
94+
].includes(args.commentBehaviour)) {
95+
throw new Error(`Invalid value ${args.commentBehaviour} for input comment-behaviour`)
96+
}
97+
8498
return args
8599
}

src/getBodyComment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ErrorTs } from "./main"
22

33
const BLANK_LINE = ' \n'
4+
export const COMMENT_TITLE = '## Typescript errors check'
45

56
type Input = {
67
errorsInProjectBefore: ErrorTs[]
@@ -13,7 +14,7 @@ type Input = {
1314
export function getBodyComment({ errorsInProjectBefore, errorsInProjectAfter, errorsInModifiedFiles, newErrorsInProject }: Input): string {
1415

1516
const delta = errorsInProjectAfter.length - errorsInProjectBefore.length
16-
let s = `## Typescript errors check \n`
17+
let s = `${COMMENT_TITLE} \n`
1718

1819
const areStillErrors = !!errorsInProjectAfter.length
1920

src/main.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { createCheck } from './createCheck'
55
import * as github from '@actions/github'
66
import * as fs from 'fs'
77
import { parseTsConfigFile } from './tscHelpers/parseTsConfigFileToCompilerOptions'
8-
import { getAndValidateArgs, CHECK_FAIL_MODE, OUTPUT_BEHAVIOUR } from './getAndValidateArgs'
8+
import { getAndValidateArgs, CHECK_FAIL_MODE, OUTPUT_BEHAVIOUR, COMMENT_BEHAVIOUR } from './getAndValidateArgs'
99
import { exec } from '@actions/exec'
10-
import { getBodyComment } from './getBodyComment'
10+
import { COMMENT_TITLE, getBodyComment } from './getBodyComment'
1111
import { checkoutAndInstallBaseBranch } from './checkoutAndInstallBaseBranch'
1212
import { compareErrors } from './compareErrors'
1313
import { runTscCli } from './tscHelpers/runTscCli'
@@ -174,9 +174,11 @@ async function run(): Promise<void> {
174174
if ([OUTPUT_BEHAVIOUR.COMMENT, OUTPUT_BEHAVIOUR.COMMENT_AND_ANNOTATE].includes(args.outputBehaviour)) {
175175
startGroup(`Creating comment`)
176176

177+
const issueNumber = context.payload.pull_request!.number
178+
177179
const commentInfo = {
178180
...context.repo,
179-
issue_number: context.payload.pull_request!.number
181+
issue_number: issueNumber
180182
}
181183

182184
const comment = {
@@ -192,7 +194,17 @@ async function run(): Promise<void> {
192194
info(`comment body obtained`)
193195

194196
try {
195-
await octokit.rest.issues.createComment(comment)
197+
const existingComments = await octokit.rest.issues.listComments({owner: context.repo.owner, repo: context.repo.repo, issue_number: issueNumber})
198+
const existingComment = existingComments.data.find(c => !!c.body?.includes(COMMENT_TITLE))
199+
200+
if (args.commentBehaviour === COMMENT_BEHAVIOUR.EDIT && existingComment) {
201+
await octokit.rest.issues.updateComment({
202+
comment_id: existingComment.id,
203+
...comment
204+
})
205+
} else {
206+
await octokit.rest.issues.createComment(comment)
207+
}
196208
} catch (e) {
197209
info(`Error creating comment: ${(e as Error).message}`)
198210
info(`Submitting a PR review comment instead...`)

0 commit comments

Comments
 (0)