-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathallowed_issue_fields.cjs
More file actions
62 lines (57 loc) · 1.79 KB
/
Copy pathallowed_issue_fields.cjs
File metadata and controls
62 lines (57 loc) · 1.79 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
// @ts-check
/// <reference types="@actions/github-script" />
const { ERR_VALIDATION } = require("./error_codes.cjs");
/**
* Parse allowed issue field names from config.
* @param {string[]|string|undefined} value
* @returns {string[]}
*/
function parseAllowedIssueFields(value) {
if (value == null || value === "") {
return [];
}
const raw = Array.isArray(value) ? value : String(value).split(",");
const uniqueFields = new Set();
for (const item of raw) {
const normalized = String(item).trim();
if (normalized) {
uniqueFields.add(normalized);
}
}
return [...uniqueFields];
}
/**
* Validate one issue field name against configured allowed-fields.
* @param {string} fieldName
* @param {string[]} allowedFields
* @returns {void}
*/
function validateAllowedIssueFieldName(fieldName, allowedFields) {
if (!fieldName) {
return;
}
if (!Array.isArray(allowedFields) || allowedFields.length === 0 || allowedFields.includes("*")) {
return;
}
const allowedFieldSet = new Set(allowedFields.map(field => field.toLowerCase()));
if (!allowedFieldSet.has(fieldName.toLowerCase())) {
throw new Error(`${ERR_VALIDATION}: issue field "${fieldName}" is not in the allowed-fields list: ${allowedFields.join(", ")}`);
}
}
/**
* Validate requested issue fields against configured allowed-fields.
* @param {Array<{name: string, value: string|number}>} issueFields
* @param {string[]} allowedFields
* @returns {void}
*/
function validateAllowedIssueFields(issueFields, allowedFields) {
if (!Array.isArray(issueFields) || issueFields.length === 0) return;
for (const field of issueFields) {
validateAllowedIssueFieldName(field.name, allowedFields);
}
}
module.exports = {
parseAllowedIssueFields,
validateAllowedIssueFieldName,
validateAllowedIssueFields,
};