Skip to content

Commit

Permalink
make default instead of configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman committed Oct 21, 2024
1 parent c45db8a commit a6e2491
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 101 deletions.
112 changes: 27 additions & 85 deletions __tests__/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentUpserter>
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>({
configurationReader = new Mock<ConfigurationReader>({
injectorConfig: new EqualMatchingInjectorConfig()
})
.setup(async instance => instance.read(repo, baseSha))
Expand All @@ -61,40 +54,31 @@ describe('Runner', () => {
'config/application.rb',
'.github/workflows/codemention.yml'
]
const filesChangedReader = new Mock<FilesChangedReader>({
filesChangedReader = new Mock<FilesChangedReader>({
injectorConfig: new EqualMatchingInjectorConfig()
})
.setup(async instance => instance.read(repo, prNumber))
.returnsAsync(filesChanged)
.object()

const commentUpserterMock = new Mock<CommentUpserter>({
commentUpserterMock = new Mock<CommentUpserter>({
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()),
Expand All @@ -104,62 +88,20 @@ 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'],
mentions: ['ci']
}
]
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'
})
)
})
})
})
})
2 changes: 0 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ export interface Configuration {
rules: MentionRule[]
/** Configuration for comment */
commentConfiguration?: CommentConfiguration
/** Whether to exclude PR author from mentions */
excludeAuthor?: boolean
}
27 changes: 13 additions & 14 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down

0 comments on commit a6e2491

Please sign in to comment.