Skip to content

Commit

Permalink
Add comment template
Browse files Browse the repository at this point in the history
  • Loading branch information
rehlma committed Apr 17, 2024
1 parent 69f4769 commit 9602604
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 23 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ comments found.
You can configure the action further by providing inputs:
- `token`: The GitHub token to use for commenting on the PR. This is required.
- `exclude`: A list of glob patterns to exclude from the search. This is
optional.
- `exclude`: (**optional**) A list of glob patterns to exclude from the search.
- `comment_on_todo`: (**optional**) Whether to comment on the PR with the TODOs
found. Default is `true`.
- `comment_body`: (**optional**) The body of the comment to post on the PR. Use
`{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

```yaml
steps:
Expand All @@ -63,4 +68,8 @@ steps:
exclude: |
*.md
**/config/*.yml
comment_on_todo: true
comment_body: |
'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'
```
32 changes: 31 additions & 1 deletion __test__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { findTodos } from '../src/tools'
import exp from 'constants'

Check failure on line 1 in __test__/main.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'exp' is defined but never used

Check failure on line 1 in __test__/main.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'exp' is defined but never used
import { findTodos, generateComment } from '../src/tools'
import { Todo } from '../src/types'
import { excludeFilesPrDiff, todoPrDiff } from './test_data'

Expand Down Expand Up @@ -69,3 +70,32 @@ describe('extractTodos', () => {
expect(fileTodos).toEqual(expectedTodos)
})
})

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)
})
})
17 changes: 17 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ inputs:
description:
'Exclude file patterns. User list of globs separated by new line.'
required: false
comment_on_todo:
description: 'Comment on the TODO in the PR'
required: false
default: 'true'
comment_body:
description:
'Comment body to use when a TODO is found. Use {todo} to insert the TODO
message.'
required: false
default: "A new Todo was discovered. If it is not a priority right now,\
consider marking it for later attention.\n{todo}\n"
comment_checkbox:
description:
'Checkbox message to ignore the TODO. Use {todo} to insert the TODO
message.'
required: false
default: 'Ignore'

runs:
using: 'node20'
Expand Down
21 changes: 12 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29302,6 +29302,9 @@ function run() {
const token = core.getInput('token');
// Get the user's glbbing input and split it into an array of patterns
const excludePatterns = core.getInput('exclude').split('\n');
const commentOnTodo = core.getInput('comment_on_todo') === 'true';
const commentBodyTemplate = core.getInput('comment_body');
const commentCheckboxTemplate = core.getInput('comment_checkbox');
const octokit = github.getOctokit(token);
const botName = 'github-actions[bot]';
const pr = github.context.payload.pull_request;
Expand All @@ -29318,11 +29321,11 @@ function run() {
console.log('Comment change detected');
yield updateCommitStatus(octokit, pr.number, botName);
}
else {
else if (commentOnTodo) {
const prDiff = yield getPrDiff(octokit, pr.base.sha, pr.head.sha);
const todos = (0, tools_1.findTodos)(prDiff, excludePatterns);
console.log('Todos:', JSON.stringify(todos));
yield commentPr(octokit, pr.number, botName, todos);
yield commentPr(octokit, pr.number, botName, todos, commentBodyTemplate, commentCheckboxTemplate);
}
}
catch (error) {
Expand All @@ -29349,7 +29352,7 @@ function getPrDiff(octokit, base, head) {
return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.files) || [];
});
}
function commentPr(octokit, prNumber, botName, todos) {
function commentPr(octokit, prNumber, botName, todos, commentBodyTemplate, commentCheckboxTemplate) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const { owner, repo } = github.context.repo;
Expand Down Expand Up @@ -29391,7 +29394,7 @@ function commentPr(octokit, prNumber, botName, todos) {
owner,
repo,
pull_number: prNumber,
body: (0, tools_1.generateComment)(todo),
body: (0, tools_1.generateComment)(commentBodyTemplate, commentCheckboxTemplate, todo),
commit_id: headSha,
path: todo.filename,
side: 'RIGHT',
Expand Down Expand Up @@ -29526,14 +29529,14 @@ function getTodoIfFound(line) {
return;
return match[1];
}
function generateComment(todo) {
let comment = 'A new Todo was found. If you want to fix it later on, mark it as ignore.\n';
comment += `*${todo.content}*\n`;
function generateComment(bodyTemplate, checkboxTemplate, todo) {
let comment = bodyTemplate.replace('{todo}', todo.content);
comment += '\n';
if (todo.isAdded) {
comment += `- [ ] Ignore`;
comment += `- [ ] ${checkboxTemplate.replace('{todo}', todo.content)}`;
}
else {
comment += `- [x] Ignore`;
comment += `- [x] ${checkboxTemplate.replace('{todo}', todo.content)}`;
}
console.log(comment);
return comment;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export async function run(): Promise<void> {

// Get the user's glbbing input and split it into an array of patterns
const excludePatterns = core.getInput('exclude').split('\n')
const commentOnTodo = core.getInput('comment_on_todo') === 'true'
const commentBodyTemplate = core.getInput('comment_body')
const commentCheckboxTemplate = core.getInput('comment_checkbox')

const octokit = github.getOctokit(token)
const botName = 'github-actions[bot]'
Expand All @@ -30,12 +33,19 @@ export async function run(): Promise<void> {
console.log('User:', user)
console.log('Comment change detected')
await updateCommitStatus(octokit, pr.number, botName)
} else {
} else if (commentOnTodo) {
const prDiff = await getPrDiff(octokit, pr.base.sha, pr.head.sha)

const todos = findTodos(prDiff, excludePatterns)
console.log('Todos:', JSON.stringify(todos))
await commentPr(octokit, pr.number, botName, todos)
await commentPr(
octokit,
pr.number,
botName,
todos,
commentBodyTemplate,
commentCheckboxTemplate
)
}
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -67,7 +77,9 @@ async function commentPr(
octokit: ReturnType<typeof github.getOctokit>,
prNumber: number,
botName: string,
todos: Todo[]
todos: Todo[],
commentBodyTemplate: string,
commentCheckboxTemplate: string
): Promise<void> {
const { owner, repo } = github.context.repo
const issueNumber = github.context.payload.pull_request?.number
Expand Down Expand Up @@ -119,7 +131,7 @@ async function commentPr(
owner,
repo,
pull_number: prNumber,
body: generateComment(todo),
body: generateComment(commentBodyTemplate, commentCheckboxTemplate, todo),
commit_id: headSha,
path: todo.filename,
side: 'RIGHT',
Expand Down
15 changes: 9 additions & 6 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ function getTodoIfFound(line: string): string | undefined {
return match[1]
}

export function generateComment(todo: Todo): string {
let comment =
'A new Todo was found. If you want to fix it later on, mark it as ignore.\n'
comment += `*${todo.content}*\n`
export function generateComment(
bodyTemplate: string,
checkboxTemplate: string,
todo: Todo
): string {
let comment = bodyTemplate.replace('{todo}', todo.content)
comment += '\n'
if (todo.isAdded) {
comment += `- [ ] Ignore`
comment += `- [ ] ${checkboxTemplate.replace('{todo}', todo.content)}`
} else {
comment += `- [x] Ignore`
comment += `- [x] ${checkboxTemplate.replace('{todo}', todo.content)}`
}
console.log(comment)
return comment
Expand Down

0 comments on commit 9602604

Please sign in to comment.