Skip to content

Commit

Permalink
Merge branch 'master' into pr-report-status
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvanderlinden committed Oct 12, 2018
2 parents b08c808 + b593d3c commit b0286e5
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 32 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'
42 changes: 29 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"raven": "^2.6.4"
},
"devDependencies": {
"@types/debug": "0.0.30",
"@types/debug": "0.0.31",
"@types/jest": "^23.3.2",
"@types/node": "^10.11.3",
"@types/p-queue": "^2.3.1",
Expand All @@ -57,6 +57,9 @@
]
},
"jest": {
"setupFiles": [
"./test/jest-setup.ts"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
Expand Down
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 @@ -47,7 +48,8 @@ export const defaultRuleConfig: ConditionConfig = {
NONE: 0
},
blockingLabels: [],
requiredLabels: []
requiredLabels: [],
blockingTitleRegex: undefined
}

export const defaultConfig: Config = {
Expand All @@ -73,7 +75,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 @@ -82,6 +85,7 @@ const configDecoder: Decoder<Config> = object({
maxRequestedChanges: reviewConfigDecover,
requiredLabels: array(string()),
blockingLabels: array(string()),
blockingTitleRegex: optional(string()),
updateBranch: boolean(),
deleteBranchAfterMerge: boolean(),
reportStatus: boolean(),
Expand Down
1 change: 1 addition & 0 deletions src/github-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface PullRequestQueryResult {
name: string
}>
},
title: string,
authorAssociation: CommentAuthorAssociation,
baseRef: {
repository: {
Expand Down
9 changes: 7 additions & 2 deletions src/pull-request-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { conditions } from './conditions/index'
import Raven from 'raven'
import { conditions } from './conditions/index'
import { HandlerContext, PullRequestReference, PullRequestInfo } from './models'
import { result } from './utils'
import { getPullRequestStatus, PullRequestStatus } from './pull-request-status'
Expand All @@ -21,7 +21,12 @@ export async function handlePullRequest (
context.github,
pullRequestReference
)
context.log.debug('pullRequestInfo:', pullRequestInfo)

Raven.mergeContext({
extra: {
pullRequestInfo
}
})

const pullRequestStatus = getPullRequestStatus(
context,
Expand Down
29 changes: 20 additions & 9 deletions src/pull-request-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { PullRequestReference, CheckRun, PullRequestInfo, PullRequestQueryResult
import { Context } from 'probot'
import { result } from './utils'

function assertPullRequest (pullRequest: PullRequestReference, condition: boolean, errorMessage: string) {
if (!condition) {
const error: any = new Error(errorMessage)
error.pullRequest = `${pullRequest.owner}/${pullRequest.repo}#${pullRequest.number}`
throw error
}
}

export async function queryPullRequest (github: Context['github'], { owner, repo, number: pullRequestNumber }: PullRequestReference): Promise<PullRequestInfo> {
const response = await github.query(`
query PullRequestQuery($owner:String!, $repo:String!, $pullRequestNumber:Int!) {
Expand All @@ -27,6 +35,7 @@ export async function queryPullRequest (github: Context['github'], { owner, repo
name
}
}
title
authorAssociation
baseRef {
repository {
Expand Down Expand Up @@ -71,15 +80,17 @@ export async function queryPullRequest (github: Context['github'], { owner, repo
'repo': repo,
'pullRequestNumber': pullRequestNumber
}) as any
if (!response) {
throw new Error(`Could not query pull request ${owner}/${repo}#${pullRequestNumber}`)
}
if (!response.repository) {
const error: any = new Error(`Query result does not have repository`)
error.pullRequest = `${owner}/${repo}#${pullRequestNumber}`
error.response = response
throw error
}

const assert = assertPullRequest.bind(null, {
owner,
repo,
number: pullRequestNumber
})

assert(response, 'Could not query pull request')
assert(response.repository, 'Query result does not have repository')
assert(response.repository.pullRequest.headRef && response.repository.pullRequest.mergeable, 'No permission to source repository of pull request')

const queryResult = response as PullRequestQueryResult

const checks = result<{ check_runs: CheckRun[] }>(await github.checks.listForRef({
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')
})
})
Loading

0 comments on commit b0286e5

Please sign in to comment.