Skip to content

Commit

Permalink
feat: allow error to depend on title
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Mar 14, 2021
1 parent 6846ad8 commit e325e0d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 35 deletions.
26 changes: 19 additions & 7 deletions dist/index-node12-dev.cjs.js

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

2 changes: 1 addition & 1 deletion dist/index-node12-dev.cjs.js.map

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions dist/index-node12.cjs.js

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

2 changes: 1 addition & 1 deletion dist/index-node12.cjs.js.map

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/accountConfigs/ornikar.ts
Expand Up @@ -53,10 +53,12 @@ const config: Config<'dev' | 'design', 'ops' | 'frontends' | 'backends'> = {
// eslint-disable-next-line unicorn/no-unsafe-regex
regExp: /^(?<revert>revert-\d+-)?(?<type>build|chore|ci|docs|feat|fix|perf|refactor|style|test)(?<scope>\/[a-z-]+)?\/(?<breaking>!)?(?<subject>.*)-(?<jiraIssue>[A-Z][\dA-Z]+-(\d+))$/,
warning: true,
error: {
title: 'Branch name does not match commitlint conventional',
error: ({ title }) => ({
title: `Branch name does not match commitlint conventional, ideal branch name would have been: ${title
.replace(/(\(|\):|:)\s*/g, '/')
.replace(/[\s,]+/g, '-')}`,
summary: '',
},
}),
},
],
base: [
Expand Down
2 changes: 1 addition & 1 deletion src/accountConfigs/types.ts
Expand Up @@ -26,7 +26,7 @@ export interface Team {
export interface ParsePRRule {
bot?: false;
regExp: RegExp;
error: StatusError;
error: StatusError | ((prInfos: { title: string }) => StatusError);
warning?: boolean;

status?: string;
Expand Down
45 changes: 30 additions & 15 deletions src/events/pr-handlers/actions/editOpenedPR.ts
@@ -1,11 +1,7 @@
import type { EventPayloads } from '@octokit/webhooks';
import type { Context } from 'probot';
import type { RepoContext } from 'context/repoContext';
import type {
ParsePRRule,
StatusError,
StatusInfo,
} from '../../../accountConfigs/types';
import type { ParsePRRule, StatusInfo } from '../../../accountConfigs/types';
import { getKeys } from '../../../context/utils';
import { ExcludesFalsy } from '../../../utils/Excludes';
import type { PullRequestWithDecentData } from '../utils/PullRequestData';
Expand All @@ -31,7 +27,7 @@ interface StatusWithInfo {

interface StatusWithError {
name: string;
error: StatusError;
error: ParsePRRule['error'];
info?: undefined;
}

Expand Down Expand Up @@ -60,7 +56,7 @@ export const editOpenedPR = async <
const isPrFromBot = pullRequest.user && pullRequest.user.type === 'Bot';

const statuses: Status[] = [];
const warnings: StatusError[] = [];
const warnings: ParsePRRule['error'][] = [];

let errorRule: ParsePRRule | undefined;
getKeys(repoContext.config.parsePR).find((parsePRKey) => {
Expand Down Expand Up @@ -108,6 +104,13 @@ export const editOpenedPR = async <
(check): boolean => check.name === `${process.env.REVIEWFLOW_NAME}/lint-pr`,
);

const errorStatus = errorRule
? // eslint-disable-next-line unicorn/no-nested-ternary
typeof errorRule.error === 'function'
? errorRule.error({ title })
: errorRule.error
: undefined;

const promises: Promise<unknown>[] = [
...statuses.map(
({ name, error, info }): Promise<void> =>
Expand All @@ -116,7 +119,9 @@ export const editOpenedPR = async <
name,
pullRequest.head.sha,
error ? 'failure' : 'success',
error ? error.title : (info as StatusInfo).title,
error
? (typeof error === 'function' ? error({ title }) : error).title
: (info as StatusInfo).title,
error ? undefined : (info as StatusInfo).url,
),
),
Expand All @@ -140,7 +145,7 @@ export const editOpenedPR = async <
context.repo({
name: `${process.env.REVIEWFLOW_NAME}/lint-pr`,
head_sha: pullRequest.head.sha,
status: 'completed' as const,
status: 'completed',
conclusion: (errorRule ? 'failure' : 'success') as
| 'failure'
| 'success',
Expand All @@ -153,7 +158,11 @@ export const editOpenedPR = async <
warnings.length === 0
? '✓ Your PR is valid'
: `warnings: ${warnings
.map((error) => error.title)
.map((error) =>
typeof error === 'function'
? error({ title }).title
: error.title,
)
.join(',')}`,
summary: '',
},
Expand All @@ -174,15 +183,21 @@ export const editOpenedPR = async <
'lint-pr',
pullRequest.head.sha,
errorRule ? 'failure' : 'success',
errorRule
? errorRule.error.title
errorStatus
? errorStatus.title
: // eslint-disable-next-line unicorn/no-nested-ternary
warnings.length === 0
? '✓ Your PR is valid'
: `warning${warnings.length === 1 ? '' : 's'}: ${warnings
.map((error) => error.title)
: `warning${
warnings.length === 1 ? '' : 's'
}: ${warnings
.map((error) =>
typeof error === 'function'
? error({ title }).title
: error.title,
)
.join(',')}`,
errorRule ? errorRule.error.url : undefined,
errorStatus ? errorStatus.url : undefined,
),
].filter(ExcludesFalsy);

Expand Down

0 comments on commit e325e0d

Please sign in to comment.