-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathconfig.ts
206 lines (185 loc) Β· 7.26 KB
/
config.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {Config, getDefaultConfig} from "./entities/Config";
import {Context} from "probot";
/**
* The code in this block was inspired by (but heavily modified): https://github.com/tcbyrd/probot-report-error
*/
/* === */
const issueTitle = 'Error in Create Issue Branch app configuration'
async function findConfigurationErrorIssue(ctx: Context<any>) {
const fullName = ctx.payload.repository.full_name
const result = await ctx.octokit.search.issuesAndPullRequests(
{q: `${issueTitle} repo:${fullName} in:title type:issue state:open`})
return result.data.items
}
async function createConfigurationErrorIssue(ctx: Context<any>, err: string) {
const errorBody = (err: string) => {
return `
Error in app configuration:
\`\`\`
${err}
\`\`\`
Please check the syntax of your \`.issue-branch.yml\`
`
}
try {
await ctx.octokit.issues.create(ctx.repo({
title: issueTitle, body: errorBody(err)
}));
} catch (e: any) {
ctx.log.error(`Error creating configuration error issue: ${e.message}`)
}
}
async function handleError(ctx: Context<any>, err: string) {
ctx.log.info(`Error in app configuration: ${err}`)
const issues = await findConfigurationErrorIssue(ctx)
if (issues.length > 0) {
ctx.log.info(`Error issue already exists for repo: ${ctx.payload.repository.full_name}`)
} else {
await createConfigurationErrorIssue(ctx, err)
}
}
/* === */
export async function loadConfig(ctx: Context<any>): Promise<Config | undefined> {
try {
let result = await ctx.config<Config>('issue-branch.yml');
if (!result) {
result = await ctx.config<Config>('issue-branch.yaml');
}
if (!result) {
result = getDefaultConfig();
} else {
result = {...getDefaultConfig(), ...result};
}
for (const branchConfiguration of result.branches) {
if (!branchConfiguration.label) {
await handleError(ctx, `Branch configuration is missing label: ${JSON.stringify(branchConfiguration)}`);
return undefined;
}
}
return result
} catch (e: any) {
await handleError(ctx, `Exception while parsing configuration YAML: ${e.message}`)
return undefined;
}
}
export function isModeAuto(config: Config) {
return !isModeChatOps(config) && !isModeImmediate(config);
}
export function isModeImmediate(config: Config) {
return config.mode === 'immediate';
}
export function isModeChatOps(config: Config) {
return config.mode === 'chatops';
}
export function isExperimentalBranchNameArgument(config: Config) {
return config.experimental.branchNameArgument;
}
export function shouldOpenPR(config: Config) {
return config.openPR || config.openDraftPR;
}
export function prSkipCI(config: Config) {
return config.prSkipCI;
}
export function showFreePlanWarning(config: Config) {
return config.freePlanWarning;
}
export function isChatOpsCommand(s?: string) {
if (s) {
const parts = s.trim().toLowerCase().split(/\s/)
return ['/create-issue-branch', '/cib'].includes(parts[0])
} else {
return false
}
}
export function getChatOpsCommandArgument(s: string) {
const argumentIndex = s.trim().search(/\s/)
if (argumentIndex > 0) {
return s.substring(argumentIndex + 1)
} else {
return undefined
}
}
export function getCommentMessage(config: Config) {
if (config && config.commentMessage) {
return config.commentMessage
} else {
// eslint-disable-next-line no-template-curly-in-string
return 'Branch [${branchName}](${repository.html_url}/tree/${branchName}) created!'
}
}
export function getDefaultBranch(config: Config) {
if (config && config.defaultBranch) {
return config.defaultBranch
} else {
return undefined
}
}
export function getConventionalPrTitlePrefix(config: Config, labels: Array<string>) {
const mapping = getConventionalLabelMapping(config)
const conventionalLabels = Object.keys(mapping).filter(k => labels.includes(k)).map(k => mapping[k]);
const featureLabels = conventionalLabels.filter(cl => cl.prefix === 'feat')
if (featureLabels.length > 0) {
const emoji = featureLabels[0].emoji
if (config.conventionalStyle === 'semver') {
return `feat${isBreakingPr(labels, mapping) ? '!' : ''}: ${emoji}`
} else if (config.conventionalStyle === 'semver-no-gitmoji') {
return `feat${isBreakingPr(labels, mapping) ? '!' : ''}:`
} else {
return emoji
}
} else if (conventionalLabels.length > 0) {
const emoji = conventionalLabels[0].emoji
if (config.conventionalStyle === 'semver') {
return `${conventionalLabels[0].prefix}${isBreakingPr(labels, mapping) ? '!' : ''}: ${emoji}`
} else if (config.conventionalStyle === 'semver-no-gitmoji') {
return `${conventionalLabels[0].prefix}${isBreakingPr(labels, mapping) ? '!' : ''}:`
} else {
return emoji
}
} else {
if (config.conventionalStyle === 'semver') {
return `feat${isBreakingPr(labels, mapping) ? '!' : ''}: β¨`
} else if (config.conventionalStyle === 'semver-no-gitmoji') {
return `feat${isBreakingPr(labels, mapping) ? '!' : ''}:`
} else {
return 'β¨'
}
}
}
interface ConventionalLabelMapping {
[label: string]: { prefix: string, emoji: string, breaking: boolean }
}
export function getConventionalLabelMapping(config: Config) {
const mapping: ConventionalLabelMapping = {
bug: {prefix: 'fix', emoji: 'π', breaking: false},
dependencies: {prefix: 'fix', emoji: 'β¬οΈ', breaking: false},
security: {prefix: 'fix', emoji: 'π', breaking: false},
enhancement: {prefix: 'feat', emoji: 'β¨', breaking: false},
build: {prefix: 'build', emoji: 'π§', breaking: false},
chore: {prefix: 'chore', emoji: 'β»οΈ', breaking: false},
ci: {prefix: 'ci', emoji: 'π·', breaking: false},
documentation: {prefix: 'docs', emoji: 'π', breaking: false},
style: {prefix: 'style', emoji: 'π', breaking: false},
refactor: {prefix: 'refactor', emoji: 'β»οΈ', breaking: false},
performance: {prefix: 'perf', emoji: 'β‘οΈ', breaking: false},
test: {prefix: 'test', emoji: 'β
', breaking: false},
'breaking-change': {prefix: 'feat', emoji: 'π₯', breaking: true},
'breaking change': {prefix: 'feat', emoji: 'π₯', breaking: true}
}
if (config && config.conventionalLabels) {
Object.keys(config.conventionalLabels).forEach(prefix => {
Object.keys(config.conventionalLabels[prefix]).forEach(label => {
const emoji = config.conventionalLabels[prefix][label]
const breaking = config.conventionalLabels[prefix].breaking === true
if (prefix === 'features') {
prefix = 'feat'
}
mapping[label] = {prefix: prefix, emoji: emoji, breaking: breaking}
})
})
}
return mapping
}
function isBreakingPr(labels: Array<string>, mapping: ConventionalLabelMapping) {
return labels.some(l => l in mapping && mapping[l].breaking)
}