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

52.33% Statements 56/107
55.44% Branches 56/101
57.77% Functions 26/45
51.45% Lines 53/103

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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382                                          18x 18x 18x   18x 8x     18x       3x           3x 3x       10x 18x                                                                                                                                     1x       1x       1x     1x     1x       8x 8x   8x           1x             16x 16x   16x 16x                                                                                                                                   10x 6x                   4x                       196x     28x     1x                 1x                   1x       1x                                               28x   17x   17x 10x 10x   10x                         28x 17x     10x 10x   10x       10x 10x 9x     1x       8x 8x   8x       8x                                                     3x                            
import { sortBy, cloneDeep, find, inRange } from 'lodash';
import {
  TYPENAME_BOARD,
  TYPENAME_ITERATION,
  TYPENAME_MILESTONE,
  TYPENAME_USER,
} from '~/graphql_shared/constants';
import { isGid, convertToGraphQLId, getIdFromGraphQLId } from '~/graphql_shared/utils';
import {
  ListType,
  MilestoneIDs,
  AssigneeFilterType,
  MilestoneFilterType,
  boardQuery,
} from 'ee_else_ce/boards/constants';
 
export function getMilestone() {
  return null;
}
 
export function updateListPosition(listObj) {
  const { listType } = listObj;
  let { position } = listObj;
  Iif (listType === ListType.closed) {
    position = Infinity;
  } else if (listType === ListType.backlog) {
    position = -Infinity;
  }
 
  return { ...listObj, position };
}
 
export function calculateNewPosition(listPosition, initialPosition, targetPosition) {
  Iif (
    listPosition === null ||
    !(inRange(listPosition, initialPosition, targetPosition) || listPosition === targetPosition)
  ) {
    return listPosition;
  }
  const offset = initialPosition < targetPosition ? -1 : 1;
  return listPosition + offset;
}
 
export function formatBoardLists(lists) {
  return lists.nodes.reduce((map, list) => {
    return {
      ...map,
      [list.id]: updateListPosition(list),
    };
  }, {});
}
 
export function formatIssue(issue) {
  return {
    ...issue,
    labels: issue.labels?.nodes || [],
    assignees: issue.assignees?.nodes || [],
  };
}
 
export function formatListIssues(listIssues) {
  const boardItems = {};
 
  const listData = listIssues.nodes.reduce((map, list) => {
    let sortedIssues = list.issues.nodes;
    if (list.listType !== ListType.closed) {
      sortedIssues = sortBy(sortedIssues, 'relativePosition');
    }
 
    return {
      ...map,
      [list.id]: sortedIssues.map((i) => {
        const { id } = i;
 
        const listIssue = {
          ...i,
          labels: i.labels?.nodes || [],
          assignees: i.assignees?.nodes || [],
        };
 
        boardItems[id] = listIssue;
 
        return id;
      }),
    };
  }, {});
 
  return { listData, boardItems };
}
 
export function formatListsPageInfo(lists) {
  const listData = lists.nodes.reduce((map, list) => {
    return {
      ...map,
      [list.id]: list.issues.pageInfo,
    };
  }, {});
  return listData;
}
 
export function fullBoardId(boardId) {
  if (!boardId) {
    return null;
  }
  return convertToGraphQLId(TYPENAME_BOARD, boardId);
}
 
export function fullIterationId(id) {
  return convertToGraphQLId(TYPENAME_ITERATION, id);
}
 
export function fullUserId(id) {
  return convertToGraphQLId(TYPENAME_USER, id);
}
 
export function fullMilestoneId(id) {
  return convertToGraphQLId(TYPENAME_MILESTONE, id);
}
 
export function fullLabelId(label) {
  Iif (isGid(label.id)) {
    return label.id;
  }
  Iif (label.project_id && label.project_id !== null) {
    return `gid://gitlab/ProjectLabel/${label.id}`;
  }
  return `gid://gitlab/GroupLabel/${label.id}`;
}
 
export function formatIssueInput(issueInput, boardConfig) {
  const { labelIds = [], assigneeIds = [] } = issueInput;
  const { labels, assigneeId, milestoneId, weight } = boardConfig;
 
  return {
    ...issueInput,
    milestoneId:
      milestoneId && milestoneId !== MilestoneIDs.ANY
        ? fullMilestoneId(milestoneId)
        : issueInput?.milestoneId,
    labelIds: [...labelIds, ...(labels?.map((l) => fullLabelId(l)) || [])],
    assigneeIds: [...assigneeIds, ...(assigneeId ? [fullUserId(assigneeId)] : [])],
    weight: weight > -1 ? weight : undefined,
  };
}
 
export function shouldCloneCard(fromListType, toListType) {
  const involvesClosed = fromListType === ListType.closed || toListType === ListType.closed;
  const involvesBacklog = fromListType === ListType.backlog || toListType === ListType.backlog;
 
  Eif (involvesClosed || involvesBacklog) {
    return false;
  }
 
  if (fromListType !== toListType) {
    return true;
  }
 
  return false;
}
 
export function getMoveData(state, params) {
  const { boardItems, boardItemsByListId, boardLists } = state;
  const { itemId, fromListId, toListId } = params;
  const fromListType = boardLists[fromListId].listType;
  const toListType = boardLists[toListId].listType;
 
  return {
    reordering: fromListId === toListId,
    shouldClone: shouldCloneCard(fromListType, toListType),
    itemNotInToList: !boardItemsByListId[toListId].includes(itemId),
    originalIssue: cloneDeep(boardItems[itemId]),
    originalIndex: boardItemsByListId[fromListId].indexOf(itemId),
    ...params,
  };
}
 
export function moveItemListHelper(item, fromList, toList) {
  const updatedItem = cloneDeep(item);
 
  if (
    toList.listType === ListType.label &&
    !updatedItem.labels.find((label) => label.id === toList.label.id)
  ) {
    updatedItem.labels.push(toList.label);
  }
  if (fromList?.label && fromList.listType === ListType.label) {
    updatedItem.labels = updatedItem.labels.filter((label) => fromList.label.id !== label.id);
  }
 
  if (
    toList.listType === ListType.assignee &&
    !updatedItem.assignees.find((assignee) => assignee.id === toList.assignee.id)
  ) {
    updatedItem.assignees.push(toList.assignee);
  }
  if (fromList?.assignee && fromList.listType === ListType.assignee) {
    updatedItem.assignees = updatedItem.assignees.filter(
      (assignee) => assignee.id !== fromList.assignee.id,
    );
  }
 
  return updatedItem;
}
 
export function moveItemVariables({
  iid,
  itemId,
  epicId = null,
  fromListId,
  toListId,
  moveBeforeId,
  moveAfterId,
  isIssue,
  boardId,
  itemToMove,
}) {
  if (isIssue) {
    return {
      iid,
      boardId,
      projectPath: itemToMove.referencePath.split(/[#]/)[0],
      moveBeforeId: moveBeforeId ? getIdFromGraphQLId(moveBeforeId) : undefined,
      moveAfterId: moveAfterId ? getIdFromGraphQLId(moveAfterId) : undefined,
      fromListId: getIdFromGraphQLId(fromListId),
      toListId: getIdFromGraphQLId(toListId),
    };
  }
  return {
    itemId,
    epicId,
    boardId,
    moveBeforeId,
    moveAfterId,
    fromListId,
    toListId,
  };
}
 
export function isListDraggable(list) {
  return list.listType !== ListType.backlog && list.listType !== ListType.closed;
}
 
export const FiltersInfo = {
  assigneeUsername: {
    negatedSupport: true,
    remap: (k, v) => (v === AssigneeFilterType.any ? 'assigneeWildcardId' : k),
  },
  assigneeId: {
    // assigneeId should be renamed to assigneeWildcardId.
    // Classic boards used 'assigneeId'
    remap: () => 'assigneeWildcardId',
  },
  assigneeWildcardId: {
    negatedSupport: false,
    transform: (val) => val.toUpperCase(),
  },
  authorUsername: {
    negatedSupport: true,
  },
  labelName: {
    negatedSupport: true,
  },
  milestoneTitle: {
    negatedSupport: true,
    remap: (k, v) => (Object.values(MilestoneFilterType).includes(v) ? 'milestoneWildcardId' : k),
  },
  milestoneWildcardId: {
    negatedSupport: true,
    transform: (val) => val.toUpperCase(),
  },
  myReactionEmoji: {
    negatedSupport: true,
  },
  releaseTag: {
    negatedSupport: true,
  },
  types: {
    negatedSupport: true,
  },
  confidential: {
    negatedSupport: false,
    transform: (val) => val === 'yes',
  },
  search: {
    negatedSupport: false,
  },
};
 
/**
 * @param {Object} filters - ex. { search: "foobar", "not[authorUsername]": "root", }
 * @returns {Object} - ex. [ ["search", "foobar", false], ["authorUsername", "root", true], ]
 */
const parseFilters = (filters) => {
  /* eslint-disable-next-line @gitlab/require-i18n-strings */
  const isNegated = (x) => x.startsWith('not[') && x.endsWith(']');
 
  return Object.entries(filters).map(([k, v]) => {
    const isNot = isNegated(k);
    const filterKey = isNot ? k.slice(4, -1) : k;
 
    return [filterKey, v, isNot];
  });
};
 
/**
 * Returns an object of filter key/value pairs used as variables in GraphQL requests.
 * (warning: filter values are not validated)
 *
 * @param {Object} objParam.filters - filters extracted from url params. ex. { search: "foobar", "not[authorUsername]": "root", }
 * @param {string} objParam.issuableType - issuable type e.g., issue.
 * @param {Object} objParam.filterInfo - data on filters such as how to transform filter value, if filter can be negated, etc.
 * @param {Object} objParam.filterFields - data on what filters are available for given issuableType (based on GraphQL schema)
 */
export const filterVariables = ({ filters, issuableType, filterInfo, filterFields }) =>
  parseFilters(filters)
    .map(([k, v, negated]) => {
      // for legacy reasons, some filters need to be renamed to correct GraphQL fields.
      const remapAvailable = filterInfo[k]?.remap;
      const remappedKey = remapAvailable ? filterInfo[k].remap(k, v) : k;
 
      return [remappedKey, v, negated];
    })
    .filter(([k, , negated]) => {
      // remove unsupported filters (+ check if the filters support negation)
      const supported = filterFields[issuableType].includes(k);
      if (supported) {
        return negated ? filterInfo[k].negatedSupport : true;
      }
 
      return false;
    })
    .map(([k, v, negated]) => {
      // if the filter value needs a special transformation, apply it (e.g., capitalization)
      const transform = filterInfo[k]?.transform;
      const newVal = transform ? transform(v) : v;
 
      return [k, newVal, negated];
    })
    .reduce(
      (acc, [k, v, negated]) => {
        return negated
          ? {
              ...acc,
              not: {
                ...acc.not,
                [k]: v,
              },
            }
          : {
              ...acc,
              [k]: v,
            };
      },
      { not: {} },
    );
 
// EE-specific feature. Find the implementation in the `ee/`-folder
export function transformBoardConfig() {
  return '';
}
 
export function getBoardQuery(boardType) {
  return boardQuery[boardType].query;
}
 
export function getListByTypeId(lists, type, id) {
  // type can be assignee/label/milestone/iteration
  Eif (type && id) return find(lists, (l) => l.listType === ListType[type] && l[type]?.id === id);
 
  return null;
}
 
export default {
  getMilestone,
  formatIssue,
  formatListIssues,
  fullBoardId,
  fullLabelId,
  fullIterationId,
  isListDraggable,
};