Skip to content

Commit

Permalink
Merge pull request #66 from luoxi/master
Browse files Browse the repository at this point in the history
add conditions, checking title
  • Loading branch information
bobvanderlinden committed Oct 12, 2018
2 parents fa9a656 + 8c08201 commit b593d3c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
3 changes: 3 additions & 0 deletions auto-merge.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ blockingLabels:
# all of these labels are attached to a pull request.
requiredLabels:
- merge

# Automatic merges will be blocked if there is a match between the regular expression and title
blockingTitleRegex: '\bwip\b'
26 changes: 26 additions & 0 deletions src/conditions/blockingTitle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ConditionConfig } from './../config'
import { PullRequestInfo } from '../models'
import { ConditionResult } from '../condition'

export default function doesNotHaveBlockingTitle (
config: ConditionConfig,
pullRequestInfo: PullRequestInfo
): ConditionResult {
if (config.blockingTitleRegex === undefined) {
return {
status: 'success'
}
}

const regexObj = new RegExp(config.blockingTitleRegex, 'i')

if (regexObj.test(pullRequestInfo.title)) {
return {
status: 'fail',
message: `Blocking words are found in title (${ pullRequestInfo.title })`
}
}
return {
status: 'success'
}
}
4 changes: 3 additions & 1 deletion src/conditions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import blockingLabels from './blockingLabels'
import blockingChecks from './blockingChecks'
import minimumApprovals from './minimumApprovals'
import maximumChangesRequested from './maximumChangesRequested'
import blockingTitle from './blockingTitle'
import { keysOf } from '../utils'

export const conditions = {
Expand All @@ -15,7 +16,8 @@ export const conditions = {
blockingLabels,
minimumApprovals,
maximumChangesRequested,
blockingChecks
blockingChecks,
blockingTitle
}

export type Conditions = typeof conditions
Expand Down
10 changes: 7 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export type ConditionConfig = {
minApprovals: { [key in CommentAuthorAssociation]?: number },
maxRequestedChanges: { [key in CommentAuthorAssociation]?: number },
requiredLabels: string[],
blockingLabels: string[]
blockingLabels: string[],
blockingTitleRegex: string | undefined
}

export type Config = {
Expand All @@ -46,7 +47,8 @@ export const defaultRuleConfig: ConditionConfig = {
NONE: 0
},
blockingLabels: [],
requiredLabels: []
requiredLabels: [],
blockingTitleRegex: undefined
}

export const defaultConfig: Config = {
Expand All @@ -71,7 +73,8 @@ const conditionConfigDecoder: Decoder<ConditionConfig> = object({
minApprovals: reviewConfigDecover,
maxRequestedChanges: reviewConfigDecover,
requiredLabels: array(string()),
blockingLabels: array(string())
blockingLabels: array(string()),
blockingTitleRegex: optional(string())
})

const configDecoder: Decoder<Config> = object({
Expand All @@ -80,6 +83,7 @@ const configDecoder: Decoder<Config> = object({
maxRequestedChanges: reviewConfigDecover,
requiredLabels: array(string()),
blockingLabels: array(string()),
blockingTitleRegex: optional(string()),
updateBranch: boolean(),
deleteBranchAfterMerge: boolean(),
mergeMethod: oneOf(
Expand Down
1 change: 1 addition & 0 deletions src/github-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface PullRequestQueryResult {
name: string
}>
},
title: string,
authorAssociation: CommentAuthorAssociation,
baseRef: {
repository: {
Expand Down
1 change: 1 addition & 0 deletions src/pull-request-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export async function queryPullRequest (github: Context['github'], { owner, repo
name
}
}
title
authorAssociation
baseRef {
repository {
Expand Down
38 changes: 38 additions & 0 deletions test/conditions/blockingTitle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import blockingTitle from '../../src/conditions/blockingTitle'
import { createPullRequestInfo, createConditionConfig } from '../mock'

describe('blockingTitle', () => {
it('returns success if with default configuration(undefined)', async () => {
const result = blockingTitle(
createConditionConfig(),
createPullRequestInfo({
title: '[WIP] help needed'
})
)
expect(result.status).toBe('success')
})

it('returns success if there is a match in title', async () => {
const result = blockingTitle(
createConditionConfig({
blockingTitleRegex: 'wip'
}),
createPullRequestInfo({
title: 'Add some feature'
})
)
expect(result.status).toBe('success')
})

it('returns fail if there is not a match in title', async () => {
const result = blockingTitle(
createConditionConfig({
blockingTitleRegex: 'wip'
}),
createPullRequestInfo({
title: '[WIP] help needed'
})
)
expect(result.status).toBe('fail')
})
})
3 changes: 2 additions & 1 deletion test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const defaultPullRequestInfo: PullRequestInfo = {
nodes: []
}
},
checkRuns: []
checkRuns: [],
title: 'Add some feature'
}

export function createPullRequestInfo (pullRequestInfo?: Partial<PullRequestInfo>): PullRequestInfo {
Expand Down

0 comments on commit b593d3c

Please sign in to comment.