Skip to content
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

fix: move codemention sentinel to end of comment #18

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
115 changes: 79 additions & 36 deletions __tests__/comment-upserter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods'
import {RestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types.d'
import {EqualMatchingInjectorConfig, It, Mock, Times} from 'moq.ts'

import {CommentUpserterImpl, DEFAULT_COMMENT_PREAMBLE, HEADER} from '../src/comment-upserter'
import {CommentUpserterImpl, DEFAULT_COMMENT_PREAMBLE, FOOTER} from '../src/comment-upserter'
import {Repo} from '../src/github-types'

describe('CommentUpserterImpl', () => {
Expand Down Expand Up @@ -69,12 +69,13 @@ describe('CommentUpserterImpl', () => {
it('creates a comment', async () => {
const expectedCommentBody =
[
HEADER,
DEFAULT_COMMENT_PREAMBLE,
'| File Patterns | Mentions |',
'| - | - |',
'| db/migrate/\\*\\* | @cto, @dba |',
'| .github/\\*\\*<br>spec/\\*.rb | @ci |'
'| .github/\\*\\*<br>spec/\\*.rb | @ci |',
'',
FOOTER
].join('\n')

issuesMock
Expand Down Expand Up @@ -103,13 +104,14 @@ describe('CommentUpserterImpl', () => {
}
const expectedCommentBody =
[
HEADER,
customContent.preamble,
'| File Patterns | Mentions |',
'| - | - |',
'| db/migrate/\\*\\* | @cto, @dba |',
'| .github/\\*\\*<br>spec/\\*.rb | @ci |',
`\n${customContent.epilogue}`
'',
customContent.epilogue,
FOOTER
].join('\n')

issuesMock
Expand All @@ -134,50 +136,91 @@ describe('CommentUpserterImpl', () => {

describe('when a codemention comment exists', () => {
describe('and the comment is different', () => {
it('updates the comment', async () => {
const expectedCommentBody =
[
HEADER,
DEFAULT_COMMENT_PREAMBLE,
'| File Patterns | Mentions |',
'| - | - |',
'| db/migrate/\\*\\* | @cto, @dba |',
'| .github/\\*\\*<br>spec/\\*.rb | @ci |'
].join('\n')

const existingComment = HEADER + '| config/brakeman.yml | @security |'
stubListComments(['First', existingComment])

issuesMock
.setup(instance => instance.updateComment(It.IsAny()))
.returnsAsync(
new Mock<
RestEndpointMethodTypes['issues']['updateComment']['response']
>().object()
describe('and the comment has the sentinel at the start', () => {
it('updates the comment', async () => {
const expectedCommentBody =
[
DEFAULT_COMMENT_PREAMBLE,
'| File Patterns | Mentions |',
'| - | - |',
'| db/migrate/\\*\\* | @cto, @dba |',
'| .github/\\*\\*<br>spec/\\*.rb | @ci |',
'',
FOOTER,
].join('\n')

// previous version of the action put the sentinel comment at the start
const existingComment = FOOTER + '| config/brakeman.yml | @security |'
stubListComments(['First', existingComment])

issuesMock
.setup(instance => instance.updateComment(It.IsAny()))
.returnsAsync(
new Mock<
RestEndpointMethodTypes['issues']['updateComment']['response']
>().object()
)

await upserter.upsert(repo, pullNumber, rules)

issuesMock.verify(instance =>
instance.updateComment({
...repo,
comment_id: 2,
body: expectedCommentBody
})
)
})
})

await upserter.upsert(repo, pullNumber, rules)

issuesMock.verify(instance =>
instance.updateComment({
...repo,
comment_id: 2,
body: expectedCommentBody
})
)
describe('and the comment has the sentinel at the end', () => {
it('updates the comment', async () => {
const expectedCommentBody =
[
DEFAULT_COMMENT_PREAMBLE,
'| File Patterns | Mentions |',
'| - | - |',
'| db/migrate/\\*\\* | @cto, @dba |',
'| .github/\\*\\*<br>spec/\\*.rb | @ci |',
'',
FOOTER,
].join('\n')

const existingComment = '| config/brakeman.yml | @security |' + FOOTER
stubListComments(['First', existingComment])

issuesMock
.setup(instance => instance.updateComment(It.IsAny()))
.returnsAsync(
new Mock<
RestEndpointMethodTypes['issues']['updateComment']['response']
>().object()
)

await upserter.upsert(repo, pullNumber, rules)

issuesMock.verify(instance =>
instance.updateComment({
...repo,
comment_id: 2,
body: expectedCommentBody
})
)
})
})
})

describe('and the comment is the same', () => {
it('does not update the comment', async () => {
const commentBody =
[
HEADER,
DEFAULT_COMMENT_PREAMBLE,
'| File Patterns | Mentions |',
'| - | - |',
'| db/migrate/\\*\\* | @cto, @dba |',
'| .github/\\*\\*<br>spec/\\*.rb | @ci |'
'| .github/\\*\\*<br>spec/\\*.rb | @ci |',
'',
FOOTER
].join('\n')

stubListComments(['First', commentBody])
Expand Down
14 changes: 8 additions & 6 deletions src/comment-upserter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import markdownEscape from 'markdown-escape'
import {CommentConfiguration, MentionRule} from './configuration'
import {Repo} from './github-types'

export const HEADER = '<!-- codemention header -->'
export const FOOTER = '<!-- codemention header -->'

export const DEFAULT_COMMENT_PREAMBLE =
'[CodeMention](https://github.com/tobyhs/codemention):'
Expand Down Expand Up @@ -50,7 +50,10 @@ export class CommentUpserterImpl implements CommentUpserter {
issue_number: pullNumber
})
const existingComment = listResponse.data.find(
c => c.body !== undefined && c.body.startsWith(HEADER)
c =>
c.body !== undefined &&
// keep backwards compatibility with existing comments that have the comment first
(c.body.startsWith(FOOTER) || c.body.endsWith(FOOTER))
)
const commentBody = this.createCommentBody(rules, commentConfiguration)

Expand Down Expand Up @@ -96,14 +99,13 @@ export class CommentUpserterImpl implements CommentUpserter {
return `| ${patterns} | ${mentions} |`
})
return [
HEADER,
commentConfiguration?.preamble ?? DEFAULT_COMMENT_PREAMBLE,
'| File Patterns | Mentions |',
'| - | - |',
...mentionsTableRows,
commentConfiguration?.epilogue
? `\n${commentConfiguration.epilogue}` // need two line breaks to finish table before epilogue
: undefined
'', // need an extra line break after table before epilogue/footer
commentConfiguration?.epilogue,
FOOTER
]
.filter(elem => elem !== undefined)
.join('\n')
Expand Down
Loading