All files / src/api azureDevOpsIdentityHelpers.js

100% Statements 82/82
100% Branches 32/32
100% Functions 6/6
100% Lines 82/82

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 839x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 6x 6x 6x 6x 6x 6x 6x 8x 8x 6x 6x 6x 2x 5x 5x 5x 5x 2x 4x 4x 4x 4x 4x 6x 6x 6x 6x 6x 5x 5x 6x 4x 4x 4x 2x 5x 5x 5x 5x 5x 5x 5x 5x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 5x 4x 4x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
function buildAzureDevOpsClientConfig(pat = "") {
  return {
    auth: {
      username: "",
      password: pat,
    },
    headers: {
      "Content-Type": "application/json",
    },
    timeout: 30000,
  };
}
 
function splitIntoIdentityBatches(userIds = [], batchSize = 100) {
  const safeUserIds = Array.isArray(userIds) ? userIds : [];
  const safeBatchSize =
    Number.isInteger(batchSize) && batchSize > 0 ? batchSize : 100;
 
  const batches = [];
  for (let i = 0; i < safeUserIds.length; i += safeBatchSize) {
    batches.push(safeUserIds.slice(i, i + safeBatchSize));
  }
 
  return batches;
}
 
function buildIdentitiesLookupUrl(organization = "", userBatch = []) {
  const idsParam = (Array.isArray(userBatch) ? userBatch : []).join(",");
  return `https://vssps.dev.azure.com/${organization}/_apis/identities?identityIds=${idsParam}&api-version=7.0`;
}
 
function mapIdentityResultsToUserMap(identities = []) {
  const safeIdentities = Array.isArray(identities) ? identities : [];
  const userMap = {};
 
  for (const identity of safeIdentities) {
    const id = identity?.id?.toLowerCase();
    const displayName =
      identity?.displayName || identity?.providerDisplayName || "Usuario";
 
    if (id) {
      userMap[id] = displayName;
    }
  }
 
  return userMap;
}
 
function buildWorkItemStatesUrl({
  organization = "",
  project = "",
  workItemType = "",
} = {}) {
  const encodedType = encodeURIComponent(workItemType);
  return `https://dev.azure.com/${organization}/${project}/_apis/wit/workitemtypes/${encodedType}/states?api-version=7.0`;
}
 
function resolveStateColorHex(states = [], targetState = "") {
  const safeStates = Array.isArray(states) ? states : [];
  const safeTargetState = String(targetState || "").toLowerCase();
 
  const stateInfo = safeStates.find(
    (state) => String(state?.name || "").toLowerCase() === safeTargetState,
  );
 
  if (!stateInfo?.color) {
    return null;
  }
 
  return stateInfo.color.startsWith("#")
    ? stateInfo.color
    : `#${stateInfo.color}`;
}
 
module.exports = {
  buildAzureDevOpsClientConfig,
  splitIntoIdentityBatches,
  buildIdentitiesLookupUrl,
  mapIdentityResultsToUserMap,
  buildWorkItemStatesUrl,
  resolveStateColorHex,
};