blob: 573555724337ab3e442f0ebfa2abe96019d45699 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// TODO(crbug.com/monorail/4549): Remove this hardcoded data once backend custom
6// field grouping is implemented.
7export const HARDCODED_FIELD_GROUPS = [
8 {
9 groupName: 'Feature Team',
10 fieldNames: ['PM', 'Tech Lead', 'Tech-Lead', 'TechLead', 'TL',
11 'Team', 'UX', 'TE'],
12 applicableType: 'FLT-Launch',
13 },
14 {
15 groupName: 'Docs',
16 fieldNames: ['PRD', 'DD', 'Design Doc', 'Design-Doc',
17 'DesignDoc', 'Mocks', 'Test Plan', 'Test-Plan', 'TestPlan',
18 'Metrics'],
19 applicableType: 'FLT-Launch',
20 },
21];
22
23export const fieldGroupMap = (fieldGroupsArg, issueType) => {
24 const fieldGroups = groupsForType(fieldGroupsArg, issueType);
25 return fieldGroups.reduce((acc, group) => {
26 return group.fieldNames.reduce((acc, fieldName) => {
27 acc[fieldName] = group.groupName;
28 return acc;
29 }, acc);
30 }, {});
31};
32
33/**
34 * Get all values for a field, given an issue's fieldValueMap.
35 * @param {Map.<string, Array<string>>} fieldValueMap Map where keys are
36 * lowercase fieldNames and values are fieldValue strings.
37 * @param {string} fieldName The name of the field to look up.
38 * @param {string=} phaseName Name of the phase the field is attached to,
39 * if applicable.
40 * @return {Array<string>} The values of the field.
41 */
42export const valuesForField = (fieldValueMap, fieldName, phaseName) => {
43 if (!fieldValueMap) return [];
44 return fieldValueMap.get(
45 fieldValueMapKey(fieldName, phaseName)) || [];
46};
47
48/**
49 * Get just one value for a field. Convenient in some cases for
50 * fields that are not multi-valued.
51 * @param {Map.<string, Array<string>>} fieldValueMap Map where keys are
52 * lowercase fieldNames and values are fieldValue strings.
53 * @param {string} fieldName The name of the field to look up.
54 * @param {string=} phaseName Name of the phase the field is attached to,
55 * if applicable.
56 * @return {string} The value of the field.
57 */
58export function valueForField(fieldValueMap, fieldName, phaseName) {
59 const values = valuesForField(fieldValueMap, fieldName, phaseName);
60 return values.length ? values[0] : undefined;
61}
62
63/**
64 * Helper to generate Map keys for FieldValueMaps in a standard format.
65 * @param {string} fieldName Name of the field the value is tied to.
66 * @param {string=} phaseName Name of the phase the value is tied to.
67 * @return {string}
68 */
69export const fieldValueMapKey = (fieldName, phaseName) => {
70 const key = [fieldName];
71 if (phaseName) {
72 key.push(phaseName);
73 }
74 return key.join(' ').toLowerCase();
75};
76
77export const groupsForType = (fieldGroups, issueType) => {
78 return fieldGroups.filter((group) => {
79 if (!group.applicableType) return true;
80 return issueType && group.applicableType.toLowerCase() ===
81 issueType.toLowerCase();
82 });
83};
84
85export const fieldDefsWithGroup = (fieldDefs, fieldGroupsArg, issueType) => {
86 const fieldGroups = groupsForType(fieldGroupsArg, issueType);
87 if (!fieldDefs) return [];
88 const groups = [];
89 fieldGroups.forEach((group) => {
90 const groupFields = [];
91 group.fieldNames.forEach((name) => {
92 const fd = fieldDefs.find(
93 (fd) => (fd.fieldRef.fieldName == name));
94 if (fd) {
95 groupFields.push(fd);
96 }
97 });
98 if (groupFields.length > 0) {
99 groups.push({
100 groupName: group.groupName,
101 fieldDefs: groupFields,
102 });
103 }
104 });
105 return groups;
106};
107
108export const fieldDefsWithoutGroup = (fieldDefs, fieldGroups, issueType) => {
109 if (!fieldDefs) return [];
110 const map = fieldGroupMap(fieldGroups, issueType);
111 return fieldDefs.filter((fd) => {
112 return !(fd.fieldRef.fieldName in map);
113 });
114};