All files / publisher/release/actions releases.ts

98.71% Statements 77/78
86.84% Branches 33/38
100% Functions 27/27
98.71% Lines 77/78

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                                        4x     5x                   5x 2x 2x 2x 2x         3x 2x 2x   2x   2x     2x       1x       1x 1x                 8x   8x 1x         8x 2x   2x 2x 4x 4x         8x                 7x     4x 4x 4x 4x 4x 4x 4x   4x     4x   2x 2x 1x   1x             4x 4x               3x             5x         5x   5x       3x     5x               5x                 5x 5x               5x 5x 5x 4x 6x   6x     4x   2x   1x   2x 1x   1x           5x   5x       5x     5x           1x     5x 5x   5x   5x 5x   3x               5x 5x         4x          
import {
  RISKS_WITH_AVAILABLE as RISKS,
  DEFAULT_ERROR_MESSAGE as ERROR_MESSAGE,
} from "../constants";
 
import { updateArchitectures } from "./architectures";
import { hideNotification, showNotification } from "./globalNotification";
import { cancelPendingReleases } from "./pendingReleases";
import { releaseRevisionSuccess, closeChannelSuccess } from "./channelMap";
import { updateRevisions } from "./revisions";
import { closeHistory } from "./history";
 
import {
  fetchReleasesHistory,
  fetchReleases,
  fetchCloses,
} from "../api/releases";
 
import { getRevisionsMap, initReleasesData } from "../releasesState";
 
export const UPDATE_RELEASES = "UPDATE_RELEASES";
 
function updateReleasesData(releasesData: { revisions: any; releases: any }) {
  return (
    dispatch: (arg0: {
      type: string;
      payload:
        | { releases: any }
        | { revisions: any }
        | { architectures: any[] };
    }) => void,
  ) => {
    // init channel data in revisions list
    const revisionsMap = getRevisionsMap(releasesData.revisions);
    initReleasesData(revisionsMap, releasesData.releases);
    dispatch(updateRevisions(revisionsMap));
    dispatch(updateReleases(releasesData.releases));
    dispatch(updateArchitectures(releasesData.revisions));
  };
}
 
export function handleCloseResponse(dispatch: any, json: any, channels: any) {
  if (json.success) {
    Eif (json.closed_channels && json.closed_channels.length > 0) {
      json.closed_channels.forEach((channel: string) => {
        // make sure channels without track name get prefixed with 'latest'
        Eif (RISKS.indexOf(channel.split("/")[0]) !== -1) {
          // TODO: This should be the default track, not latest
          channel = `latest/${channel}`;
        }
 
        dispatch(closeChannelSuccess(channel));
      });
    }
  } else {
    const error = new Error(
      `Error while closing channels: ${channels.join(", ")}.`,
    );
    // @ts-ignore
    error.json = json;
    throw error;
  }
}
 
export function getErrorMessage(error: {
  message?: any;
  json?: any;
  errors?: any;
}) {
  let message = error.message || ERROR_MESSAGE;
 
  if (error.errors && error.errors.length > 0) {
    message = error.errors[0].message;
  }
 
  // try to find error messages in response json
  // which may be an array or errors or object with errors propery
  if (error.json) {
    const errors = error.json.length ? error.json : error.json.errors;
 
    Eif (errors.length) {
      message = `${message} ${errors
        .map((e: { message: any }) => e.message)
        .filter((m: any) => m)
        .join(" ")}`;
    }
  }
 
  return message;
}
 
export function handleReleaseResponse(
  dispatch: any,
  json: any,
  release: any,
  revisions: any,
) {
  if (json.success) {
    // Update channel map based on the response
    // We need to use channel_map_tree to get branches
    Object.keys(json.channel_map_tree).forEach((trackKey) => {
      const track = json.channel_map_tree[trackKey];
      Object.keys(track).forEach((seriesKey) => {
        const series = track[seriesKey];
        Object.keys(series).forEach((archKey) => {
          const arch = series[archKey];
          arch.forEach(
            (map: { revision: number; version: any; channel: any }) => {
              Eif (map.revision) {
                let revision;
 
                if (map.revision === +release.id) {
                  // release.id is a string so turn it into a number for comparison
                  revision = release.revision;
                } else if (revisions[map.revision]) {
                  revision = revisions[map.revision];
                } else {
                  revision = {
                    revision: map.revision,
                    version: map.version,
                    architectures: release.revision.architectures,
                  };
                }
 
                const channel = `${trackKey}/${map.channel}`;
                dispatch(releaseRevisionSuccess(revision, channel));
              }
            },
          );
        });
      });
    });
  } else {
    Iif (json.errors) {
      throw new Error(json.errors[0]);
    }
  }
}
 
export function releaseRevisions() {
  const mapToRelease = (pendingRelease: {
    progressive: { percentage: number };
    revision: { revision: any };
    channel: any;
  }) => {
    let progressive = null;
 
    if (
      pendingRelease.progressive &&
      pendingRelease.progressive.percentage < 100
    ) {
      progressive = pendingRelease.progressive;
    }
 
    return {
      id: pendingRelease.revision.revision,
      revision: pendingRelease.revision,
      channels: [pendingRelease.channel],
      progressive: progressive,
    };
  };
 
  return (
    dispatch: any,
    getState: () => {
      pendingReleases: any;
      pendingCloses: any;
      revisions: any;
      options: any;
    },
  ) => {
    const { pendingReleases, pendingCloses, revisions, options } = getState();
    const { csrfToken, snapName } = options;
 
    // To dedupe releases
    const progressiveReleases: {
      id: any;
      revision: { revision: any };
      channels: any[];
      progressive: { percentage: number } | null;
    }[] = [];
    const regularReleases: Array<any> = [];
    Object.keys(pendingReleases).forEach((revId) => {
      Object.keys(pendingReleases[revId]).forEach((channel) => {
        const pendingRelease = pendingReleases[revId][channel];
 
        if (pendingRelease.progressive) {
          // first move progressive releases out
 
          progressiveReleases.push(mapToRelease(pendingRelease));
        } else {
          const releaseIndex = regularReleases.findIndex(
            (release: { revision: { revision: number } }) =>
              release.revision.revision === parseInt(revId),
          );
          if (releaseIndex === -1) {
            regularReleases.push(mapToRelease(pendingRelease));
          } else {
            regularReleases[releaseIndex].channels.push(pendingRelease.channel);
          }
        }
      });
    });
 
    const releases = progressiveReleases.concat(regularReleases);
 
    const _handleReleaseResponse = (
      json: { success: any; channel_map_tree: any; errors?: any },
      release: { id: number; revision: number; channels: string[] }[],
    ) => {
      return handleReleaseResponse(dispatch, json, release, revisions);
    };
 
    const _handleCloseResponse = (json: {
      success?: any;
      closed_channels?: any;
      error?: boolean | undefined;
      json?: string | undefined;
    }) => {
      return handleCloseResponse(dispatch, json, pendingCloses);
    };
 
    dispatch(hideNotification());
    return fetchReleases(_handleReleaseResponse, releases, csrfToken, snapName)
      .then(() =>
        fetchCloses(_handleCloseResponse, csrfToken, snapName, pendingCloses),
      )
      .then(() => fetchReleasesHistory(csrfToken, snapName))
      .then((json) => dispatch(updateReleasesData(json)))
      .catch((error) =>
        dispatch(
          showNotification({
            status: "error",
            appearance: "negative",
            content: getErrorMessage(error),
          }),
        ),
      )
      .then(() => dispatch(cancelPendingReleases()))
      .then(() => dispatch(closeHistory()));
  };
}
 
export function updateReleases(releases: { revision: number }[]) {
  return {
    type: UPDATE_RELEASES,
    payload: { releases },
  };
}