diff --git a/__tests__/comment-upserter.test.ts b/__tests__/comment-upserter.test.ts index 2c18215..7fe71d1 100644 --- a/__tests__/comment-upserter.test.ts +++ b/__tests__/comment-upserter.test.ts @@ -3,7 +3,11 @@ 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, FOOTER} from '../src/comment-upserter' +import { + CommentUpserterImpl, + DEFAULT_COMMENT_PREAMBLE, + FOOTER +} from '../src/comment-upserter' import {Repo} from '../src/github-types' describe('CommentUpserterImpl', () => { @@ -67,16 +71,15 @@ describe('CommentUpserterImpl', () => { }) it('creates a comment', async () => { - const expectedCommentBody = - [ - DEFAULT_COMMENT_PREAMBLE, - '| File Patterns | Mentions |', - '| - | - |', - '| db/migrate/\\*\\* | @cto, @dba |', - '| .github/\\*\\*
spec/\\*.rb | @ci |', - '', - FOOTER - ].join('\n') + const expectedCommentBody = [ + DEFAULT_COMMENT_PREAMBLE, + '| File Patterns | Mentions |', + '| - | - |', + '| db/migrate/\\*\\* | @cto, @dba |', + '| .github/\\*\\*
spec/\\*.rb | @ci |', + '', + FOOTER + ].join('\n') issuesMock .setup(instance => instance.createComment(It.IsAny())) @@ -102,17 +105,16 @@ describe('CommentUpserterImpl', () => { preamble: 'Added you as a subscriber.', epilogue: '> [CodeMention](https://github.com/tobyhs/codemention)' } - const expectedCommentBody = - [ - customContent.preamble, - '| File Patterns | Mentions |', - '| - | - |', - '| db/migrate/\\*\\* | @cto, @dba |', - '| .github/\\*\\*
spec/\\*.rb | @ci |', - '', - customContent.epilogue, - FOOTER - ].join('\n') + const expectedCommentBody = [ + customContent.preamble, + '| File Patterns | Mentions |', + '| - | - |', + '| db/migrate/\\*\\* | @cto, @dba |', + '| .github/\\*\\*
spec/\\*.rb | @ci |', + '', + customContent.epilogue, + FOOTER + ].join('\n') issuesMock .setup(instance => instance.createComment(It.IsAny())) @@ -138,19 +140,19 @@ describe('CommentUpserterImpl', () => { describe('and the comment is different', () => { describe('and the comment has the sentinel at the start', () => { it('updates the comment', async () => { - const expectedCommentBody = - [ + const expectedCommentBody = [ DEFAULT_COMMENT_PREAMBLE, '| File Patterns | Mentions |', '| - | - |', '| db/migrate/\\*\\* | @cto, @dba |', '| .github/\\*\\*
spec/\\*.rb | @ci |', '', - FOOTER, + FOOTER ].join('\n') // previous version of the action put the sentinel comment at the start - const existingComment = FOOTER + '| config/brakeman.yml | @security |' + const existingComment = + FOOTER + '| config/brakeman.yml | @security |' stubListComments(['First', existingComment]) issuesMock @@ -175,18 +177,18 @@ describe('CommentUpserterImpl', () => { describe('and the comment has the sentinel at the end', () => { it('updates the comment', async () => { - const expectedCommentBody = - [ + const expectedCommentBody = [ DEFAULT_COMMENT_PREAMBLE, '| File Patterns | Mentions |', '| - | - |', '| db/migrate/\\*\\* | @cto, @dba |', '| .github/\\*\\*
spec/\\*.rb | @ci |', '', - FOOTER, + FOOTER ].join('\n') - const existingComment = '| config/brakeman.yml | @security |' + FOOTER + const existingComment = + '| config/brakeman.yml | @security |' + FOOTER stubListComments(['First', existingComment]) issuesMock @@ -212,8 +214,7 @@ describe('CommentUpserterImpl', () => { describe('and the comment is the same', () => { it('does not update the comment', async () => { - const commentBody = - [ + const commentBody = [ DEFAULT_COMMENT_PREAMBLE, '| File Patterns | Mentions |', '| - | - |', diff --git a/__tests__/fixtures/codemention.yml b/__tests__/fixtures/codemention.yml index 5d8f069..1027443 100644 --- a/__tests__/fixtures/codemention.yml +++ b/__tests__/fixtures/codemention.yml @@ -3,7 +3,7 @@ commentConfiguration: epilogue: 'testing epilogue' rules: - patterns: ['config/**'] - mentions: ['sysadmin'] + mentions: ['sysadmin', 'testlogin'] - patterns: ['db/migrate/**'] mentions: ['cto', 'dba'] - patterns: ['.github/**', 'spec/*.rb'] diff --git a/__tests__/runner.test.ts b/__tests__/runner.test.ts index c1aa8ea..2ed2799 100644 --- a/__tests__/runner.test.ts +++ b/__tests__/runner.test.ts @@ -33,7 +33,10 @@ describe('Runner', () => { pullRequest = { number: prNumber, base: {sha: baseSha}, - draft: false + draft: false, + user: { + login: 'testlogin' + } } as PullRequest context = { repo, diff --git a/src/runner.ts b/src/runner.ts index b8a1165..3a75094 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -6,6 +6,7 @@ import micromatch from 'micromatch' import {CommentUpserter} from './comment-upserter' import {ConfigurationReader} from './configuration-reader' import {FilesChangedReader} from './files-changed-reader' +import {MentionRule} from './configuration' /** * @see {@link run} @@ -40,9 +41,20 @@ export default class Runner { this.configurationReader.read(repo, pullRequest.base.sha), this.filesChangedReader.read(repo, pullRequest.number) ]) - const matchingRules = configuration.rules.filter( - rule => micromatch(filesChanged, rule.patterns).length > 0 - ) + + const matchingRules = configuration.rules + // filter to rules that match + .filter(rule => micromatch(filesChanged, rule.patterns).length > 0) + // filter out the PR author from mentions so that they don't get double-notified + .map((rule: MentionRule) => ({ + ...rule, + mentions: rule.mentions.filter( + mention => mention !== pullRequest.user.login + ) + })) + // filter out the rules that no longer have mentions due to author filtering + .filter(rule => rule.mentions.length > 0) + await this.commentUpserter.upsert( repo, pullRequest.number,