Repository navigation
Expand file tree
/
Copy pathrules.ts
More file actions
134 lines (129 loc) · 4.27 KB
/
Copy pathrules.ts
File metadata and controls
134 lines (129 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type {
MessageForRuleSet,
NamedMessageForRuleSet,
RuleStringPattern,
RuleStringPatterns,
} from './interfaces'
import {
expectedAllSevens,
expectedCommitHits123,
expectedCommitHits666,
expectedCommitHits777,
expectedCommitHitsHexspeak,
expectedCommitHitsSameNumbers,
expectedPowersOfTwo,
expectedSingleNonzeroDigitPrNumbers,
} from './rarity'
import { validateRules } from './validate'
export type { RuleStringPattern }
/**
* Parse the rule pattern from JSON string
* this function does not validate the RegExp pattern
* @param json {string} the JSON string
* @returns {RuleStringPatterns} the rule pattern
* @throws {Error} if the JSON is invalid
* @throws {Error} if the rule pattern is invalid
*/
function parseRulePatternFromJson(json: string): RuleStringPatterns {
let parsed: unknown
try {
parsed = JSON.parse(json)
} catch (_e: unknown) {
throw new Error('Invalid JSON')
}
const validated = validateRules(parsed)
if (validated) {
return parsed as RuleStringPatterns
}
throw new Error(`Invalid rules: ${JSON.stringify(validateRules.errors)}`)
}
/**
* Parse the rule pattern from JSON string and convert it to rule set
* @param json {string} the JSON string
* @returns {MessageForRuleSet} the rule set
* @throws {Error} if the rule is invalid
* @throws {Error} if the JSON is invalid
* @throws {Error} if the rule set is invalid
*/
export function parseRules(json: string): MessageForRuleSet {
const parsed = parseRulePatternFromJson(json)
const rules: MessageForRuleSet = []
for (const rule of parsed) {
try {
rules.push({
kind: rule.kind,
rule: new RegExp(rule.rule),
message: rule.message,
})
} catch (_e: unknown) {
throw new Error(`Invalid rule: ${rule.rule}`)
}
}
return rules
}
export const Rules: NamedMessageForRuleSet = {
pr_reaches_contain_only_one_nonzero_digit: {
id: 'pr_reaches_contain_only_one_nonzero_digit',
kind: 'pr',
rule: /(?:^[1-9]0+$)/,
message: `Now pull request issue number reaches **{{prNum}}**. It's time to celebrate!`,
expectedOccurrences: ({ prNum }) =>
expectedSingleNonzeroDigitPrNumbers(prNum),
},
pr_reaches_power_of_2: {
id: 'pr_reaches_power_of_2',
kind: 'pr',
rule: /(?:^(512|1024|2048|4096|8192|16384|32768|65536)$)/,
message: `Now pull request issue number reaches **{{prNum}}** (power of 2). It's time to celebrate!`,
expectedOccurrences: ({ prNum }) => expectedPowersOfTwo(prNum),
},
pr_reaches_777: {
id: 'pr_reaches_777',
kind: 'pr',
rule: /(?:^7{3,}$)/,
message: `Now pull request issue number reaches **{{prNum}}** (777). It's time to celebrate!`,
expectedOccurrences: ({ prNum }) => expectedAllSevens(prNum),
},
commit_hits_777: {
id: 'commit_hits_777',
kind: 'commit',
rule: /(?:7{3,})/,
message: 'Commit `{{commitId}}` is lucky! It contains **{{matched}}**!.',
expectedOccurrences: ({ repositoryCommitCount }) =>
expectedCommitHits777(repositoryCommitCount),
},
commit_hits_same_numbers: {
id: 'commit_hits_same_numbers',
kind: 'commit',
rule: /(?:([0-9a-f])\1{4,})/,
message: 'Commit `{{commitId}}` is lucky! It contains **{{matched}}**!.',
expectedOccurrences: ({ repositoryCommitCount }) =>
expectedCommitHitsSameNumbers(repositoryCommitCount),
},
commit_hits_123: {
id: 'commit_hits_123',
kind: 'commit',
rule: /(?:123(?:4(?:5(?:6(?:7(?:8(?:9)?)?)?)?)?)?)/,
message: 'Commit `{{commitId}}` is lucky! It contains **{{matched}}**!.',
expectedOccurrences: ({ repositoryCommitCount }) =>
expectedCommitHits123(repositoryCommitCount),
},
commit_hits_hexspeak: {
id: 'commit_hits_hexspeak',
kind: 'commit',
rule: /(?:(?:f00d|feed|cafe|c0ffee|deadbeef))/,
message: 'Commit `{{commitId}}` is lucky! It contains **{{matched}}**!.',
expectedOccurrences: ({ repositoryCommitCount }) =>
expectedCommitHitsHexspeak(repositoryCommitCount),
},
commit_hits_666: {
id: 'commit_hits_666',
kind: 'commit',
rule: /(?:666)/,
message:
'Commit `{{commitId}}` is unlucky... It contains **{{matched}}**!.',
expectedOccurrences: ({ repositoryCommitCount }) =>
expectedCommitHits666(repositoryCommitCount),
},
} as const
export type RulesKey = keyof typeof Rules