diff --git a/__tests__/runner.test.ts b/__tests__/runner.test.ts index 02e4840..2ed2799 100644 --- a/__tests__/runner.test.ts +++ b/__tests__/runner.test.ts @@ -14,43 +14,36 @@ import {Repo} from '../src/github-types' import Runner from '../src/runner' describe('Runner', () => { - function setUpDependencies({ - draft, - prNumber, - baseSha, - excludeAuthor - }: { - draft: boolean - prNumber: number - baseSha: string - excludeAuthor?: boolean - }) { - const repo = {owner: 'tobyhs', repo: 'codemention'} - const pullRequest = { + let configurationReader: ConfigurationReader + let filesChangedReader: FilesChangedReader + let commentUpserterMock: Mock + let runner: Runner + + let repo: Repo + let context: Context + const prNumber = 123 + const baseSha = 'bfc5b2d29cfa2db8ce40f6c60bc9629490fe1225' + let pullRequest: PullRequest + const configuration: Configuration = yaml.load( + fs.readFileSync(path.join(__dirname, 'fixtures', 'codemention.yml'), 'utf8') + ) as Configuration + + beforeEach(() => { + repo = {owner: 'tobyhs', repo: 'codemention'} + pullRequest = { number: prNumber, base: {sha: baseSha}, - draft, + draft: false, user: { login: 'testlogin' } } as PullRequest - const context = { + context = { repo, payload: {pull_request: pullRequest} } as unknown as Context - const configuration: Configuration = yaml.load( - fs.readFileSync( - path.join(__dirname, 'fixtures', 'codemention.yml'), - 'utf8' - ) - ) as Configuration - - if (excludeAuthor) { - configuration.excludeAuthor = true - } - - const configurationReader = new Mock({ + configurationReader = new Mock({ injectorConfig: new EqualMatchingInjectorConfig() }) .setup(async instance => instance.read(repo, baseSha)) @@ -61,40 +54,31 @@ describe('Runner', () => { 'config/application.rb', '.github/workflows/codemention.yml' ] - const filesChangedReader = new Mock({ + filesChangedReader = new Mock({ injectorConfig: new EqualMatchingInjectorConfig() }) .setup(async instance => instance.read(repo, prNumber)) .returnsAsync(filesChanged) .object() - const commentUpserterMock = new Mock({ + commentUpserterMock = new Mock({ injectorConfig: new EqualMatchingInjectorConfig() }) commentUpserterMock .setup(instance => instance.upsert(It.IsAny(), It.IsAny(), It.IsAny())) .returns(Promise.resolve()) - const runner = new Runner( + runner = new Runner( configurationReader, filesChangedReader, commentUpserterMock.object() ) - - return {repo, runner, context, commentUpserterMock} - } + }) describe('.run', () => { describe('when the pull request is a draft', () => { it('does not upsert a comment', async () => { - const prNumber = 123 - const baseSha = 'bfc5b2d29cfa2db8ce40f6c60bc9629490fe1225' - const {runner, context, commentUpserterMock} = setUpDependencies({ - draft: true, - prNumber, - baseSha - }) - + pullRequest.draft = true await runner.run(context) commentUpserterMock.verify( instance => instance.upsert(It.IsAny(), It.IsAny(), It.IsAny()), @@ -104,19 +88,11 @@ describe('Runner', () => { }) it('runs main logic of the GitHub action', async () => { - const prNumber = 123 - const baseSha = 'bfc5b2d29cfa2db8ce40f6c60bc9629490fe1225' - const {repo, runner, context, commentUpserterMock} = setUpDependencies({ - draft: false, - prNumber, - baseSha - }) - await runner.run(context) const matchingRules = [ { patterns: ['config/**'], - mentions: ['sysadmin', 'testlogin'] + mentions: ['sysadmin'] }, { patterns: ['.github/**', 'spec/*.rb'], @@ -124,42 +100,8 @@ describe('Runner', () => { } ] commentUpserterMock.verify(instance => - instance.upsert(repo, prNumber, matchingRules, { - preamble: 'testing preamble', - epilogue: 'testing epilogue' - }) + instance.upsert(repo, prNumber, matchingRules, {preamble: 'testing preamble', epilogue: 'testing epilogue'}) ) }) - - describe('when excludeAuthor configuration is specified', () => { - it('does not include the author in the comment', async () => { - const prNumber = 123 - const baseSha = 'bfc5b2d29cfa2db8ce40f6c60bc9629490fe1225' - const {repo, runner, context, commentUpserterMock} = setUpDependencies({ - draft: false, - prNumber, - baseSha, - excludeAuthor: true - }) - - await runner.run(context) - const matchingRules = [ - { - patterns: ['config/**'], - mentions: ['sysadmin'] - }, - { - patterns: ['.github/**', 'spec/*.rb'], - mentions: ['ci'] - } - ] - commentUpserterMock.verify(instance => - instance.upsert(repo, prNumber, matchingRules, { - preamble: 'testing preamble', - epilogue: 'testing epilogue' - }) - ) - }) - }) }) }) diff --git a/src/configuration.ts b/src/configuration.ts index 106ef74..f6f77b0 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -27,6 +27,4 @@ export interface Configuration { rules: MentionRule[] /** Configuration for comment */ commentConfiguration?: CommentConfiguration - /** Whether to exclude PR author from mentions */ - excludeAuthor?: boolean } diff --git a/src/runner.ts b/src/runner.ts index 5489c2d..1f7acdc 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -41,23 +41,22 @@ 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 filteredRules = configuration.excludeAuthor - ? matchingRules - .map((rule: MentionRule) => ({ - ...rule, - mentions: rule.mentions.filter( - mention => mention !== pullRequest.user.login - ) - })) - .filter(rule => rule.mentions.length > 0) - : matchingRules + + // filter out the PR author so that they don't get double-notified + const matchingRules = configuration.rules + .filter(rule => micromatch(filesChanged, rule.patterns).length > 0) + .map((rule: MentionRule) => ({ + ...rule, + mentions: rule.mentions.filter( + mention => mention !== pullRequest.user.login + ) + })) + .filter(rule => rule.mentions.length > 0) + await this.commentUpserter.upsert( repo, pullRequest.number, - filteredRules, + matchingRules, configuration.commentConfiguration ) }