Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,421 additions
and
935 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { HandlerContext, PullRequestInfo } from './models' | ||
|
||
export type Condition = (context: HandlerContext, pullRequestInfo: PullRequestInfo) => ConditionResult | ||
export type ConditionResult = { status: 'success' } | { status: 'fail', message: string } | { status: 'pending', message?: string } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
import { groupByLastMap } from '../utils' | ||
|
||
export default function doesNotHaveBlockingChecks ( | ||
context: HandlerContext, | ||
pullRequestInfo: PullRequestInfo | ||
): ConditionResult { | ||
const checkRuns = pullRequestInfo.checkRuns | ||
const allChecksCompleted = checkRuns.every( | ||
checkRun => checkRun.status === 'completed' | ||
) | ||
if (!allChecksCompleted) { | ||
return { | ||
status: 'pending', | ||
message: 'There are still pending checks' | ||
} | ||
} | ||
const checkConclusions = groupByLastMap( | ||
checkRun => checkRun.conclusion, | ||
_ => true, | ||
checkRuns | ||
) | ||
const checksBlocking = | ||
checkConclusions.failure || | ||
checkConclusions.cancelled || | ||
checkConclusions.timed_out || | ||
checkConclusions.action_required | ||
if (checksBlocking) { | ||
return { | ||
status: 'fail', | ||
message: 'There are blocking checks' | ||
} | ||
} | ||
return { | ||
status: 'success' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
|
||
export default function doesNotHaveBlockingLabels ( | ||
context: HandlerContext, | ||
pullRequestInfo: PullRequestInfo | ||
): ConditionResult { | ||
const { config } = context | ||
const pullRequestLabels = new Set(pullRequestInfo.labels.nodes.map(label => label.name)) | ||
const foundBlockingLabels = config.blockingLabels | ||
.filter(blockingLabel => pullRequestLabels.has(blockingLabel)) | ||
|
||
if (foundBlockingLabels.length > 0) { | ||
return { | ||
status: 'fail', | ||
message: `Blocking labels are missing (${ | ||
foundBlockingLabels.join(', ') | ||
})` | ||
} | ||
} | ||
return { | ||
status: 'success' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ConditionResult } from './../condition' | ||
import open from './open' | ||
import mergeable from './mergeable' | ||
import requiredLabels from './requiredLabels' | ||
import blockingLabels from './blockingLabels' | ||
import blockingChecks from './blockingChecks' | ||
import minimumApprovals from './minimumApprovals' | ||
import maximumChangesRequested from './maximumChangesRequested' | ||
import upToDateBranch from './upToDateBranch' | ||
import { keysOf } from '../utils' | ||
|
||
export const conditions = { | ||
open, | ||
mergeable, | ||
requiredLabels, | ||
blockingLabels, | ||
minimumApprovals, | ||
maximumChangesRequested, | ||
blockingChecks, | ||
upToDateBranch | ||
} | ||
export const conditionNames: ConditionName[] = keysOf<ConditionName>(conditions) | ||
export type ConditionName = keyof (typeof conditions) | ||
export type ConditionResults = { [key in ConditionName]: ConditionResult } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
import { getLatestReviews, arrayToMap, mapToArray, or, get } from '../utils' | ||
import { associations, getAssociationPriority } from '../association' | ||
|
||
export default function doesNotHaveMaximumChangesRequested (context: HandlerContext, pullRequestInfo: PullRequestInfo): ConditionResult { | ||
const { config } = context | ||
|
||
const latestReviews = getLatestReviews(pullRequestInfo) | ||
const changesRequestedCountByAssociation = | ||
arrayToMap(associations, | ||
(association) => association, | ||
(association) => latestReviews | ||
.filter(review => getAssociationPriority(review.authorAssociation) >= getAssociationPriority(association)) | ||
.filter(review => review.state === 'CHANGES_REQUESTED') | ||
.length | ||
) | ||
|
||
return mapToArray(config.maxRequestedChanges) | ||
.some(([association, maxRequestedChanges]) => or(get(changesRequestedCountByAssociation, association), 0) > maxRequestedChanges) | ||
? { | ||
status: 'fail', | ||
message: `There are changes requested by a reviewer.` | ||
} | ||
: { | ||
status: 'success' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
|
||
export default function isMergeable (context: HandlerContext, pullRequestInfo: PullRequestInfo): ConditionResult { | ||
switch (pullRequestInfo.mergeable) { | ||
case 'MERGEABLE': | ||
return { | ||
status: 'success' | ||
} | ||
case 'CONFLICTING': | ||
return { | ||
status: 'fail', | ||
message: 'Pull request has conflicts' | ||
} | ||
case 'UNKNOWN': | ||
return { | ||
status: 'pending', | ||
message: 'Github could not yet determine the mergeable status of the pull request' | ||
} | ||
default: | ||
throw new Error(`Invalid mergeable state for pull request. mergeable=${pullRequestInfo.mergeable}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
import { getLatestReviews, arrayToMap, or, get, mapToArray } from '../utils' | ||
import { associations, getAssociationPriority } from '../association' | ||
|
||
export default function hasMinimumApprovals (context: HandlerContext, pullRequestInfo: PullRequestInfo): ConditionResult { | ||
const { config } = context | ||
|
||
const latestReviews = getLatestReviews(pullRequestInfo) | ||
const approvalCountByAssociation = | ||
arrayToMap(associations, | ||
(association) => association, | ||
(association) => latestReviews | ||
.filter(review => getAssociationPriority(review.authorAssociation) >= getAssociationPriority(association)) | ||
.filter(review => review.state === 'APPROVED') | ||
.length | ||
) | ||
|
||
return mapToArray(config.minApprovals) | ||
.some(([association, minApproval]) => or(get(approvalCountByAssociation, association), 0) < minApproval) | ||
? { | ||
status: 'fail', | ||
message: 'There are not enough approvals by reviewers' | ||
} | ||
: { | ||
status: 'success' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
|
||
export default function isOpen (context: HandlerContext, pullRequestInfo: PullRequestInfo): ConditionResult { | ||
return pullRequestInfo.state === 'OPEN' | ||
? { | ||
status: 'success' | ||
} | ||
: { | ||
status: 'fail', | ||
message: `State of pull request is ${pullRequestInfo.state}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
|
||
export default function hasRequiredLabels ( | ||
context: HandlerContext, | ||
pullRequestInfo: PullRequestInfo | ||
): ConditionResult { | ||
const { config } = context | ||
const pullRequestLabels = new Set(pullRequestInfo.labels.nodes.map(label => label.name)) | ||
|
||
const missingRequiredLabels = config.requiredLabels | ||
.filter(requiredLabel => !pullRequestLabels.has(requiredLabel)) | ||
|
||
if (missingRequiredLabels.length > 0) { | ||
return { | ||
status: 'fail', | ||
message: `Required labels are missing (${ | ||
missingRequiredLabels.join(', ') | ||
})` | ||
} | ||
} | ||
return { | ||
status: 'success' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { HandlerContext, PullRequestInfo } from '../models' | ||
import { ConditionResult } from '../condition' | ||
|
||
export default function hasUpToDateBranch ( | ||
context: HandlerContext, | ||
pullRequestInfo: PullRequestInfo | ||
): ConditionResult { | ||
const protectedBranch = pullRequestInfo.repository.protectedBranches.nodes | ||
.filter(protectedBranch => protectedBranch.name === pullRequestInfo.baseRef.name)[0] | ||
|
||
if (protectedBranch | ||
&& protectedBranch.hasStrictRequiredStatusChecks | ||
&& pullRequestInfo.baseRef.target.oid !== pullRequestInfo.baseRefOid) { | ||
return { | ||
status: 'fail', | ||
message: `Pull request is based on a strict protected branch (${ | ||
pullRequestInfo.baseRef.name | ||
}) and base sha of pull request (${ | ||
pullRequestInfo.baseRefOid | ||
}) differs from sha of branch (${pullRequestInfo.baseRef.target.oid})` | ||
} | ||
} | ||
|
||
return { | ||
status: 'success' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
declare module "probot-config" { | ||
export default function getConfig<TContext, TConfig>(context: TContext, fileName: String, defaultConfig: TConfig): Promise<TConfig> | ||
declare module 'probot-config' { | ||
export default function getConfig<TContext, TConfig> (context: TContext, fileName: String, defaultConfig: TConfig): Promise<TConfig> | ||
} |
Oops, something went wrong.