All files / ee/app/assets/javascripts/boards boards_util.js

79.16% Statements 76/96
59.42% Branches 41/69
55.88% Functions 19/34
78.94% Lines 75/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313                                                                                        4x                       6x 3x     3x 2x     1x       1x       6x 4x     2x                               1x                       1x     1x 1x 1x   1x     1x   1x           1x   1x         1x       1x 1x         1x       1x 1x   1x         6x 6x   6x           6x   6x               5x 5x 5x 18x   12x 12x       5x 5x     5x 2x     5x 5x   5x 1x 1x 4x 1x 1x 3x 2x     5x 5x 5x       5x     5x   5x 5x   5x 5x 5x   5x 3x       5x 5x     20x                                                                                                               82x 2x   80x       14x 14x 14x 14x     14x                              
import { sortBy } from 'lodash';
import {
  FiltersInfo as FiltersInfoCE,
  formatIssueInput as formatIssueInputCe,
} from '~/boards/boards_util';
import { boardQuery } from '~/boards/constants';
import {
  TYPENAME_EPIC_BOARD,
  TYPENAME_ITERATION,
  TYPENAME_ITERATIONS_CADENCE,
  TYPENAME_EPIC,
  TYPENAME_MILESTONE,
  TYPENAME_USER,
} from '~/graphql_shared/constants';
import { getIdFromGraphQLId, convertToGraphQLId } from '~/graphql_shared/utils';
import { objectToQuery, queryToObject } from '~/lib/utils/url_utility';
import {
  EPIC_LANE_BASE_HEIGHT,
  IterationFilterType,
  IterationIDs,
  HealthStatusFilterType,
  ListType,
  MilestoneFilterType,
  MilestoneIDs,
  WeightFilterType,
  WeightIDs,
  EpicFilterType,
} from './constants';
import epicBoardQuery from './graphql/epic_board.query.graphql';
 
export {
  formatBoardLists,
  formatListIssues,
  formatListsPageInfo,
  formatIssue,
  updateListPosition,
  moveItemListHelper,
  getMoveData,
  filterVariables,
  fullBoardId,
  calculateNewPosition,
} from '~/boards/boards_util';
 
export function getMilestone({ milestone }) {
  return milestone || null;
}
 
export function fullEpicId(epicId) {
  return convertToGraphQLId(TYPENAME_EPIC, epicId);
}
 
export function fullMilestoneId(milestoneId) {
  return convertToGraphQLId(TYPENAME_MILESTONE, milestoneId);
}
 
function fullIterationId(id) {
  if (!id) {
    return null;
  }
 
  if (id === IterationIDs.CURRENT) {
    return 'CURRENT';
  }
 
  Iif (id === IterationIDs.UPCOMING) {
    return 'UPCOMING';
  }
 
  return convertToGraphQLId(TYPENAME_ITERATION, id);
}
 
function fullIterationCadenceId(id) {
  if (!id) {
    return null;
  }
 
  return convertToGraphQLId(TYPENAME_ITERATIONS_CADENCE, getIdFromGraphQLId(id));
}
 
export function fullUserId(userId) {
  return convertToGraphQLId(TYPENAME_USER, userId);
}
 
export function fullEpicBoardId(epicBoardId) {
  return convertToGraphQLId(TYPENAME_EPIC_BOARD, epicBoardId);
}
 
export function calculateSwimlanesBufferSize(listTopCoordinate) {
  return Math.ceil((window.innerHeight - listTopCoordinate) / EPIC_LANE_BASE_HEIGHT);
}
 
export function formatEpic(epic) {
  return {
    ...epic,
    labels: epic.labels?.nodes || [],
    // Epics don't support assignees as of now
    // but `<board-card-inner>` expects it.
    // So until https://gitlab.com/gitlab-org/gitlab/-/issues/238444
    // is addressed, we need to pass empty array.
    assignees: [],
  };
}
 
export function formatListEpics(listEpics) {
  const boardItems = {};
  let listItemsCount;
 
  const listData = listEpics.nodes.reduce((map, list) => {
    listItemsCount = list.epicsCount;
    const sortedEpics = list.epics.nodes;
 
    return {
      ...map,
      [list.id]: sortedEpics.map((i) => {
        const { id } = i;
 
        const listEpic = {
          ...i,
          labels: i.labels?.nodes || [],
          assignees: i.assignees?.nodes || [],
        };
 
        boardItems[id] = listEpic;
 
        return id;
      }),
    };
  }, {});
 
  return { listData, boardItems, listItemsCount };
}
 
export function formatEpicListsPageInfo(lists) {
  const listData = lists.nodes.reduce((map, list) => {
    return {
      ...map,
      [list.id]: list.epics.pageInfo,
    };
  }, {});
  return listData;
}
 
export function formatEpicInput(epicInput, boardConfig) {
  const { labelIds = [], ...restEpicInput } = epicInput;
  return {
    ...restEpicInput,
    addLabelIds: [...labelIds, ...boardConfig.labelIds.map((id) => getIdFromGraphQLId(id))],
  };
}
 
function iterationObj(iterationId) {
  const isWildcard = Object.values(IterationIDs).includes(iterationId);
  const key = isWildcard ? 'iterationWildcardId' : 'iterationId';
 
  return {
    [key]: fullIterationId(iterationId),
  };
}
 
export function formatIssueInput(issueInput, boardConfig) {
  const { iterationId, iterationCadenceId } = boardConfig;
 
  return {
    ...formatIssueInputCe(issueInput, boardConfig),
    iterationCadenceId: fullIterationCadenceId(iterationCadenceId),
    ...iterationObj(iterationId),
  };
}
 
export function transformBoardConfig(boardConfig) {
  const updatedBoardConfig = {};
  const passedFilterParams = queryToObject(window.location.search, { gatherArrays: true });
  const updateScopeObject = (key, value = '') => {
    if (value === null || value === '') return;
    // Comparing with value string because weight can be a number
    Eif (!passedFilterParams[key] || passedFilterParams[key] !== value.toString()) {
      updatedBoardConfig[key] = value;
    }
  };
 
  let { milestoneTitle } = boardConfig;
  Iif (boardConfig.milestoneId === MilestoneIDs.NONE) {
    milestoneTitle = MilestoneFilterType.none;
  }
  if (milestoneTitle) {
    updateScopeObject('milestone_title', milestoneTitle);
  }
 
  const { iterationId, iterationCadenceId } = boardConfig;
  Iif (iterationId === IterationIDs.NONE) {
    updateScopeObject('iteration_id', IterationFilterType.none);
  } else if (iterationId === IterationIDs.ANY) {
    updateScopeObject('iteration_id', IterationFilterType.any);
    updateScopeObject('iteration_cadence_id', getIdFromGraphQLId(iterationCadenceId));
  } else if (iterationId === IterationIDs.CURRENT) {
    updateScopeObject('iteration_id', IterationFilterType.current);
    updateScopeObject('iteration_cadence_id', getIdFromGraphQLId(iterationCadenceId));
  } else if (iterationId) {
    updateScopeObject('iteration_id', getIdFromGraphQLId(iterationId));
  }
 
  let { weight } = boardConfig;
  Eif (weight !== WeightIDs.ANY) {
    Iif (weight === WeightIDs.NONE) {
      weight = WeightFilterType.none;
    }
 
    updateScopeObject('weight', weight);
  }
 
  updateScopeObject('assignee_username', boardConfig.assigneeUsername);
 
  let updatedFilterPath = objectToQuery(updatedBoardConfig);
  const filterPath = updatedFilterPath ? updatedFilterPath.split('&') : [];
 
  boardConfig.labels?.forEach((label) => {
    const labelTitle = encodeURIComponent(label.title);
    const param = `label_name[]=${labelTitle}`;
 
    if (!passedFilterParams.label_name?.includes(label.title)) {
      filterPath.push(param);
    }
  });
 
  updatedFilterPath = filterPath.join('&');
  return updatedFilterPath;
}
 
export const FiltersInfo = {
  ...FiltersInfoCE,
  epicId: {
    negatedSupport: true,
    transform: (epicId) => fullEpicId(epicId),
    // epic_id should be renamed to epicWildcardId when ANY or NONE is the value
    remap: (k, v) => (v === EpicFilterType.any || v === EpicFilterType.none ? 'epicWildcardId' : k),
  },
  epicWildcardId: {
    negatedSupport: false,
    transform: (val) => val.toUpperCase(),
  },
  iterationId: {
    negatedSupport: true,
    transform: (iterationId) => fullIterationId(iterationId),
    remap: (k, v) => {
      return v.endsWith(IterationFilterType.any) ||
        v.endsWith(IterationFilterType.none) ||
        v.endsWith(IterationFilterType.current)
        ? 'iterationWildcardId'
        : k;
    },
  },
  iterationTitle: {
    negatedSupport: true,
  },
  iterationWildcardId: {
    negatedSupport: true,
    transform: (val) => {
      // Gets the wildcard value out of the gid.
      const valList = val.split('/');
      return valList[valList.length - 1].toUpperCase();
    },
  },
  iterationCadenceId: {
    negatedSupport: false,
    transform: (iterationCadenceId) => fullIterationCadenceId(iterationCadenceId),
  },
  weight: {
    negatedSupport: true,
  },
  // healthStatus is deprecated in favour of healthStatusFilter
  healthStatus: {
    negatedSupport: true,
    remap: () => 'healthStatusFilter',
    transform: (v) =>
      v === HealthStatusFilterType.any || v === HealthStatusFilterType.none ? v.toUpperCase() : v,
  },
  healthStatusFilter: {
    negatedSupport: true,
    transform: (v) =>
      v === HealthStatusFilterType.any || v === HealthStatusFilterType.none ? v.toUpperCase() : v,
  },
};
 
export function getBoardQuery(boardType, isEpicBoard) {
  if (isEpicBoard) {
    return epicBoardQuery;
  }
  return boardQuery[boardType].query;
}
 
export function formatListIssuesForLanes(listIssues) {
  return listIssues.reduce((map, list) => {
    let sortedIssues = list.issues.nodes;
    Eif (list.listType !== ListType.closed) {
      sortedIssues = sortBy(sortedIssues, 'relativePosition');
    }
 
    return {
      ...map,
      [list.id]: sortedIssues,
    };
  }, {});
}
 
export default {
  getMilestone,
  fullEpicId,
  fullMilestoneId,
  fullUserId,
  fullEpicBoardId,
  transformBoardConfig,
};