All files / publisher/release/reducers pendingReleases.ts

100% Statements 71/71
97.95% Branches 48/49
100% Functions 22/22
100% Lines 71/71

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                                        10x 10x 9x 9x     9x 6x       10x                   11x       11x 19x 23x   23x         5x                 11x 9x     11x 10x           11x 1x     11x 2x     11x             3x 4x 2x               3x             4x   4x 3x   3x     3x         1x         4x             3x   3x 3x 3x 2x         3x       3x   3x 2x 2x 1x         3x       3x   3x 2x 2x 1x         3x                     4x   4x 3x 3x 4x   4x   3x 3x         4x                                                 56x   8x               3x           2x   3x   4x   3x   3x   3x   4x   23x      
import {
  RELEASE_REVISION,
  UNDO_RELEASE,
  CANCEL_PENDING_RELEASES,
  SET_PROGRESSIVE_RELEASE_PERCENTAGE,
  UPDATE_PROGRESSIVE_RELEASE_PERCENTAGE,
  PAUSE_PROGRESSIVE_RELEASE,
  RESUME_PROGRESSIVE_RELEASE,
  CANCEL_PROGRESSIVE_RELEASE,
} from "../actions/pendingReleases";
 
import { CLOSE_CHANNEL } from "../actions/pendingCloses";
 
import { jsonClone } from "../helpers";
 
function removePendingRelease(
  state: any,
  revision: { revision: string | number },
  channel: string | number,
) {
  const newState = jsonClone(state);
  if (newState[revision.revision]) {
    Eif (newState[revision.revision][channel]) {
      delete newState[revision.revision][channel];
    }
 
    if (Object.keys(newState[revision.revision]).length === 0) {
      delete newState[revision.revision];
    }
  }
 
  return newState;
}
 
function releaseRevision(
  state: any,
  revision: { architectures: any[]; revision: number },
  channel: string,
  progressive: null,
  previousRevisions: undefined,
) {
  state = { ...state };
 
  // cancel any other pending release for the same channel in same architectures
  // if it's a different revision
  revision.architectures.forEach((arch) => {
    Object.keys(state).forEach((revId) => {
      const pendingRelease = state[revId];
 
      if (
        parseInt(revId) !== revision.revision &&
        pendingRelease[channel] &&
        pendingRelease[channel].revision.architectures.includes(arch)
      ) {
        state = removePendingRelease(
          state,
          pendingRelease[channel].revision,
          channel,
        );
      }
    });
  });
 
  if (!state[revision.revision]) {
    state[revision.revision] = {};
  }
 
  if (!state[revision.revision][channel]) {
    state[revision.revision][channel] = {
      revision,
      channel,
    };
  }
 
  if (previousRevisions) {
    state[revision.revision][channel].previousRevisions = previousRevisions;
  }
 
  if (progressive && !state[revision.revision][channel].progressive) {
    state[revision.revision][channel].progressive = progressive;
  }
 
  return state;
}
 
function closeChannel(
  state: { [s: string]: unknown } | ArrayLike<unknown>,
  channel: string | number,
) {
  Object.values(state).forEach((pendingRelease: any) => {
    if (pendingRelease[channel]) {
      state = removePendingRelease(
        state,
        pendingRelease[channel].revision,
        channel,
      );
    }
  });
 
  return state;
}
 
function setProgressiveRelease(
  state: any,
  progressive: { percentage: number },
) {
  const nextState = jsonClone(state);
 
  Object.values(nextState).forEach((pendingRelease: any) => {
    Object.values(pendingRelease).forEach((channel: any) => {
      const hasPreviousRevisions =
        channel.previousRevisions &&
        Object.keys(channel.previousRevisions).length > 0;
 
      if (
        hasPreviousRevisions &&
        !channel.progressive &&
        progressive.percentage < 100
      ) {
        channel.progressive = { paused: false, ...progressive };
      }
    });
  });
 
  return nextState;
}
 
function updateProgressiveRelease(
  state: any,
  progressive: { percentage: any },
) {
  const nextState = jsonClone(state);
 
  Object.values(nextState).forEach((pendingRelease: any) => {
    Object.values(pendingRelease).forEach((channel: any) => {
      if (channel.progressive) {
        channel.progressive.percentage = progressive.percentage;
      }
    });
  });
 
  return nextState;
}
 
function pauseProgressiveRelease(state: any) {
  const nextState = jsonClone(state);
 
  Object.values(nextState).forEach((pendingRelease: any) => {
    Object.values(pendingRelease).forEach((channel: any) => {
      if (channel.progressive) {
        channel.progressive.paused = true;
      }
    });
  });
 
  return nextState;
}
 
function resumeProgressiveRelease(state: any) {
  const nextState = jsonClone(state);
 
  Object.values(nextState).forEach((pendingRelease: any) => {
    Object.values(pendingRelease).forEach((channel: any) => {
      if (channel.progressive) {
        channel.progressive.paused = false;
      }
    });
  });
 
  return nextState;
}
 
// This only works on the channel/arch the cancel button is pressed on
// because we're using the previousRevision from that specific combo.
// That means the progressive.key is ignored and other releases with the
// same key are not affected.
function cancelProgressiveRelease(
  state: any,
  previousRevision: { revision: any; architectures?: any[] },
) {
  let nextState = jsonClone(state);
 
  Object.keys(nextState).forEach((revision) => {
    const pendingReleaseChannels = nextState[revision];
    Object.keys(pendingReleaseChannels).forEach((channel) => {
      const pendingRelease = pendingReleaseChannels[channel];
 
      if (pendingRelease.progressive) {
        // @ts-ignore
        nextState = releaseRevision(state, previousRevision, channel, null);
        nextState[previousRevision.revision][channel].replaces = pendingRelease;
      }
    });
  });
 
  return nextState;
}
 
// revisions to be released:
// key is the id of revision to release
// value is object containing release object and channels to release to
// {
//   <revisionId>: {
//     <channel>: {
//       revision: { revision: <revisionId>, version, ... },
//       channel: <channel>,
//       progressive: { key, percentage, paused },
//       previousRevisions: {
//         <arch>: { revision: <revisionId>, version, ... }
//       },
//       replaces: <revision>
//     }
//   }
// }
// TODO: remove `revision` from here, use only data from `revisions` state
// to prevent duplication of revison data
export default function pendingReleases(
  state = {},
  action: { type?: any; payload?: any },
) {
  switch (action.type) {
    case RELEASE_REVISION:
      return releaseRevision(
        state,
        action.payload.revision,
        action.payload.channel,
        action.payload.progressive,
        action.payload.previousRevisions,
      );
    case UNDO_RELEASE:
      return removePendingRelease(
        state,
        action.payload.revision,
        action.payload.channel,
      );
    case CANCEL_PENDING_RELEASES:
      return {};
    case CLOSE_CHANNEL:
      return closeChannel(state, action.payload.channel);
    case SET_PROGRESSIVE_RELEASE_PERCENTAGE:
      return setProgressiveRelease(state, action.payload);
    case UPDATE_PROGRESSIVE_RELEASE_PERCENTAGE:
      return updateProgressiveRelease(state, action.payload);
    case PAUSE_PROGRESSIVE_RELEASE:
      return pauseProgressiveRelease(state);
    case RESUME_PROGRESSIVE_RELEASE:
      return resumeProgressiveRelease(state);
    case CANCEL_PROGRESSIVE_RELEASE:
      return cancelProgressiveRelease(state, action.payload.previousRevision);
    default:
      return state;
  }
}