All files / publisher/pages/Releases releasesState.js

44.82% Statements 26/58
54.05% Branches 20/37
27.77% Functions 5/18
44.64% Lines 25/56

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      6x 6x 4x 4x     3x           3x       4x 4x   4x 4x       4x 4x     4x 2x     4x           1x           3x                                                                                                                                   1x   1x             1x 2x   2x       1x 1x         1x                                              
import { RISKS } from "./constants";
 
function getRevisionsMap(revisions) {
  const revisionsMap = {};
  revisions.forEach((rev) => {
    rev.channels = [];
    revisionsMap[rev.revision] = rev;
  });
 
  return revisionsMap;
}
 
// init channels data in revision history
function initReleasesData(revisionsMap, releases) {
  // go through releases from older to newest
  releases
    .slice()
    .reverse()
    .forEach((release) => {
      Eif (release.revision) {
        const rev = revisionsMap[release.revision];
 
        Eif (rev) {
          const channel = release.branch
            ? `${release.track}/${release.risk}/${release.branch}`
            : `${release.track}/${release.risk}`;
 
          Eif (rev.channels.indexOf(channel) === -1) {
            rev.channels.push(channel);
          }
 
          if (!rev.prog_channels) {
            rev.prog_channels = [];
          }
 
          if (
            rev.prog_channels.indexOf(channel) === -1 &&
            release.progressive &&
            (release.progressive.percentage ||
              release.progressive["current-percentage"])
          ) {
            rev.prog_channels.push(channel);
          }
        }
      }
    });
 
  return releases;
}
 
// Get specific revision based on snapName and a channelMap object
function fetchMissingRevision(snapName, info) {
  return fetch(`/${snapName}/releases/revision/${info.revision}`)
    .then((res) => res.json())
    .then((revision) => ({
      info,
      revision: revision.revision,
    }));
}
 
// transforming channel map list data into format used by this component
// https://dashboard.snapcraft.io/docs/v2/en/snaps.html#snap-channel-map
function getReleaseDataFromChannelMap(channelMap, revisionsMap, snapName) {
  return new Promise((resolve) => {
    const releasedChannels = {};
    const missingRevisions = [];
 
    channelMap.forEach((mapInfo) => {
      if (!releasedChannels[mapInfo.channel]) {
        releasedChannels[mapInfo.channel] = {};
      }
 
      if (!releasedChannels[mapInfo.channel][mapInfo.architecture]) {
        const revisionInfo = revisionsMap.find(
          (r) => r.revision === mapInfo.revision,
        );
        if (revisionInfo) {
          releasedChannels[mapInfo.channel][mapInfo.architecture] =
            revisionInfo;
          releasedChannels[mapInfo.channel][mapInfo.architecture].expiration =
            mapInfo["expiration-date"];
          releasedChannels[mapInfo.channel][mapInfo.architecture].progressive =
            mapInfo["progressive"];
        } else {
          missingRevisions.push(fetchMissingRevision(snapName, mapInfo));
        }
      }
    });
 
    if (missingRevisions.length > 0) {
      Promise.all(missingRevisions)
        .then((revs) => {
          revs.forEach((rev) => {
            const { info, revision } = rev;
            releasedChannels[info.channel][info.architecture] = revision;
            releasedChannels[info.channel][info.architecture].expiration =
              revision["expiration-date"];
          });
 
          resolve([releasedChannels, revs.map((r) => r.revision)]);
        })
        .catch(() => {
          // if a call doesn't work for whatever reason
          resolve([releasedChannels, []]);
        });
    } else {
      resolve([releasedChannels, []]);
    }
  });
}
 
// for channel without release get next (less risk) channel with a release
function getTrackingChannel(releasedChannels, track, risk, arch) {
  let tracking = null;
  // if there is no revision for this arch in given channel (track/risk)
  Eif (
    !(
      releasedChannels[`${track}/${risk}`] &&
      releasedChannels[`${track}/${risk}`][arch]
    )
  ) {
    // find the next channel that has any revision
    for (let i = RISKS.indexOf(risk); i >= 0; i--) {
      const trackingChannel = `${track}/${RISKS[i]}`;
 
      if (
        releasedChannels[trackingChannel] &&
        releasedChannels[trackingChannel][arch]
      ) {
        tracking = trackingChannel;
        break;
      }
    }
  }
 
  return tracking;
}
 
function getUnassignedRevisions(revisionsMap, arch) {
  let filteredRevisions = Object.values(revisionsMap).reverse();
  if (arch) {
    filteredRevisions = filteredRevisions.filter((revision) => {
      return (
        revision.architectures.includes(arch) &&
        (!revision.channels || revision.channels.length === 0)
      );
    });
  }
  return filteredRevisions;
}
 
export {
  getReleaseDataFromChannelMap,
  getRevisionsMap,
  getTrackingChannel,
  getUnassignedRevisions,
  initReleasesData,
};