Skip to content

Commit

Permalink
Merge pull request #2 from phntmxyz/develop
Browse files Browse the repository at this point in the history
Add custom todo comment matcher
  • Loading branch information
rehlma authored May 6, 2024
2 parents 414fafd + f2381c6 commit 912ba8e
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 82 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ stay organized and track tasks that require attention within your codebase.

## Usage

To use this action in your workflow, add the following step:
To use this action in your workflow, add the following step. By default, the
action will search for `//`, `*` and `#` followed by `TODO` or `FIXME`.

```yaml
name: PR Todo Checker
Expand All @@ -36,11 +37,31 @@ jobs:
uses: actions/checkout@v4

- name: Check for Todos
uses: phntmxyz/pr_todo_checker@latest
uses: phntmxyz/pr_todo_checker@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
```
Example of matching Todos:
```js
// TODO - upper case with much space
// todo - lower case with space
//todo - lower case no space

/ no comment
// also todo comment
// fixme found todo
/*
* todo - In comment block
*/
```

```bash
# TODO with hashtag comment
# FIXME with hashtag comment
```

## Configuration

You can configure the action further by providing inputs:
Expand All @@ -53,14 +74,16 @@ You can configure the action further by providing inputs:
`{todo}` to insert the Todo content.
- `comment_checkbox`: (**optional**) The text to use for the checkbox in the
comment. Use `{todo}` to insert the Todo content
- `custom_todo_matcher`: (**optional**) Add custom comment indicators to match
TODOs. Default matches `//`, `*` and `#` followed by `TODO` or `FIXME`.

```yaml
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Check for Todos
uses: phntmxyz/pr_todo_checker@latest
uses: phntmxyz/pr_todo_checker@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
exclude: |
Expand All @@ -71,4 +94,5 @@ steps:
"A new Todo was discovered. If it is not a priority right now,\
consider marking it for later attention.\n{todo}\n"
comment_checkbox: 'Ignore'
custom_todo_matcher: "{'js': ['//', '/*'], 'py': ['#']}"
```
30 changes: 30 additions & 0 deletions __test__/comment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { generateComment } from '../src/comment'

describe('setting comment body and checkbox', () => {
it('should be used correctly', () => {
const commentBodyTemplate = `A new Todo was discovered. If it is not a priority right now, consider marking it for later attention.\n{todo}\n`
const commentCheckboxTemplate = 'Ignore'

const todo = {
filename: 'included/other.txt',
line: 22,
content: 'TODO - in included directory',
isAdded: true
}

const comment = generateComment(
commentBodyTemplate,
commentCheckboxTemplate,
todo
)

const expectedComment = [
'A new Todo was discovered. If it is not a priority right now, consider marking it for later attention.',
'TODO - in included directory',
'',
'- [ ] Ignore'
].join('\n')

expect(comment).toEqual(expectedComment)
})
})
48 changes: 47 additions & 1 deletion __test__/test_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const mixedTodoDiff: PrDiff = [
+// todo - lower case with space
+//todo - lower case no space
+
+// comment
+// also a todo comment
+// comment
+/*
+ * todo - In comment block
Expand Down Expand Up @@ -231,3 +231,49 @@ export const excludeFilesDiff: PrDiff = [
previous_filename: undefined
}
]

export const htmlTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'first.html',
status: 'modified',
additions: 2,
deletions: 0,
changes: 1,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -2,0 +1,27 @@
<html>
+ <!-- TODO first todo -->
<body>
- <!-- TODO second todo -->
+ <!-- TODO third todo -->
+ <!-- TODO fourth todo -->
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>`
}
]

export const mixedTodoMatcherDiff: PrDiff = [
{
sha: '111111111',
filename: 'first.any',
status: 'modified',
additions: 2,
deletions: 0,
changes: 1,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -0,0 +1,27 @@
+ <!-- TODO first todo -->
+ / todo no todo
+ // todo second todo
+ # todo third todo
+ -- todo fourth todo
+ ; todo fifth todo
+ // fixme sixth todo`
}
]
133 changes: 95 additions & 38 deletions __test__/main.test.ts → __test__/todo-finder.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { findTodos, generateComment } from '../src/tools'
import { findTodos } from '../src/todo-finder'
import { Todo } from '../src/types'
import {
excludeFilesDiff,
mixedTodoDiff,
newFileTodoDiff,
startWithRemovedLineTodoDiff,
updateTodoDiff
} from './test_data'

describe('extractTodos', () => {
import * as testData from './test_data'

describe('extract Todos', () => {
it('should correctly extract mixed todos', () => {
const fileTodos = findTodos(mixedTodoDiff)
const fileTodos = findTodos(testData.mixedTodoDiff)

const expectedTodos: Todo[] = [
{
Expand Down Expand Up @@ -43,6 +37,12 @@ describe('extractTodos', () => {
content: 'todo - lower case no space',
isAdded: true
},
{
filename: 'lib/second.js',
line: 30,
content: 'todo comment',
isAdded: true
},
{
filename: 'lib/second.js',
line: 33,
Expand All @@ -54,7 +54,7 @@ describe('extractTodos', () => {
})

it('should correctly extract updated todos', () => {
const fileTodos = findTodos(updateTodoDiff)
const fileTodos = findTodos(testData.updateTodoDiff)

const expectedTodos: Todo[] = [
{
Expand All @@ -74,7 +74,7 @@ describe('extractTodos', () => {
})

it('should correctly extract todos from a new file', () => {
const fileTodos = findTodos(newFileTodoDiff)
const fileTodos = findTodos(testData.newFileTodoDiff)

const expectedTodos: Todo[] = [
{
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('extractTodos', () => {
})

it('should correctly extract todos when diff starts with removed lines', () => {
const fileTodos = findTodos(startWithRemovedLineTodoDiff)
const fileTodos = findTodos(testData.startWithRemovedLineTodoDiff)

const expectedTodos: Todo[] = [
{
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('extractTodos', () => {
it('should correctly exclude files', () => {
const exclude = ['**/*.yml', '**/excluded/*']

const fileTodos = findTodos(excludeFilesDiff, exclude)
const fileTodos = findTodos(testData.excludeFilesDiff, exclude)

const expectedTodos: Todo[] = [
{
Expand All @@ -184,31 +184,88 @@ describe('extractTodos', () => {
})
})

describe('setting comment body and checkbox', () => {
it('should be used correctly', () => {
const commentBodyTemplate = `A new Todo was discovered. If it is not a priority right now, consider marking it for later attention.\n{todo}\n`
const commentCheckboxTemplate = 'Ignore'

const todo = {
filename: 'included/other.txt',
line: 22,
content: 'TODO - in included directory',
isAdded: true
}

const comment = generateComment(
commentBodyTemplate,
commentCheckboxTemplate,
todo
describe('custom todo matcher', () => {
it('should match html comment', () => {
const customMatcher = "{'html': ['<!--']}"
const fileTodos = findTodos(testData.htmlTodoDiff, [], customMatcher)

const expectedTodos: Todo[] = [
{
filename: 'first.html',
line: 2,
content: 'TODO first todo',
isAdded: true
},
{
filename: 'first.html',
line: 4,
content: 'TODO second todo',
isAdded: false
},
{
filename: 'first.html',
line: 4,
content: 'TODO third todo',
isAdded: true
},
{
filename: 'first.html',
line: 5,
content: 'TODO fourth todo',
isAdded: true
}
]

expect(fileTodos).toEqual(expectedTodos)
})

it('should match mixed comment indicators', () => {
const customMatcher = "{'any': ['<!--', '//', '#', '--', ';']}"
const fileTodos = findTodos(
testData.mixedTodoMatcherDiff,
[],
customMatcher
)

const expectedComment = [
'A new Todo was discovered. If it is not a priority right now, consider marking it for later attention.',
'TODO - in included directory',
'',
'- [ ] Ignore'
].join('\n')
const expectedTodos: Todo[] = [
{
filename: 'first.any',
line: 1,
content: 'TODO first todo',
isAdded: true
},
{
filename: 'first.any',
line: 3,
content: 'todo second todo',
isAdded: true
},
{
filename: 'first.any',
line: 4,
content: 'todo third todo',
isAdded: true
},
{
filename: 'first.any',
line: 5,
content: 'todo fourth todo',
isAdded: true
},
{
filename: 'first.any',
line: 6,
content: 'todo fifth todo',
isAdded: true
},
{
filename: 'first.any',
line: 7,
content: 'fixme sixth todo',
isAdded: true
}
]

expect(comment).toEqual(expectedComment)
expect(fileTodos).toEqual(expectedTodos)
})
})
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ inputs:
message.'
required: false
default: 'Ignore'
custom_todo_matcher:
description: "Add custom comment indicators for specific file types. Use
JSON format.\
Example: {'js': ['//', '/*'], 'py': ['#']}"
required: false
default: {}

runs:
using: 'node20'
Expand Down
Loading

0 comments on commit 912ba8e

Please sign in to comment.