-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
51 lines (40 loc) · 1.59 KB
/
helpers.js
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
function isValidTicketNumberFormat(ticketNumber) {
const upperCaseTicketNumber = ticketNumber.toUpperCase();
const ticketNumberRegex = /^[A-Z]+-\d+(\,[A-Z]+-\d+)*$/;
return ticketNumberRegex.test(upperCaseTicketNumber);
}
function validateType(prName) {
const typeSegment = prName.split(':')[0];
const typePart = typeSegment.includes('(') ? typeSegment.split('(')[0].trim() : typeSegment.trim();
return typePart.toLowerCase();
}
function validateTickets(prName) {
const ticketNumberMatch = prName.match(/\(([^\)]*)\)/);
if (!ticketNumberMatch) return '';
const ticketNumberSegment = ticketNumberMatch[1].trim().toUpperCase();
if (!isValidTicketNumberFormat(ticketNumberSegment)) {
return ''; // Return empty string if format is invalid
}
return `(${ticketNumberSegment})`; // Keep the valid format
}
function validateTask(prName) {
// Split the PR name by colon and take all parts after the first colon
const taskSegments = prName.split(':').slice(1);
// Join the segments back together to form the full task description
const taskDescription = taskSegments.join(':').trim();
return taskDescription;
}
function validatePrTitle(title) {
const type = validateType(title);
const tickets = validateTickets(title);
const task = validateTask(title);
if (!type || !task) {
return { isValid: false, validTitle: null, error: 'Invalid PR name format' };
}
const correctedPrTitle = `${type}${tickets}: ${task}`;
const isPrTitleValid = title === correctedPrTitle;
return { isValid: isPrTitleValid, validTitle: correctedPrTitle };
}
export default {
validatePrTitle
};