All files / publisher/pages/Releases helpers.ts

36.25% Statements 29/80
27.65% Branches 13/47
50% Functions 9/18
36.36% Lines 28/77

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                        7x       3x 1x     2x                   68x           51x 51x       1x     1x 4x       7x   1x                                     2x 2x   2x   2x 8x 8x 8x 3x   8x     2x     2x                                                                                                                                                                                                                                                                   2x 2x             2x     2x 2x    
import { AVAILABLE, REVISION_STATUS } from "./constants";
import { getChannelString } from "../../../libs/channels";
import { useEffect } from "react";
import {
  ArchitectureRevisionsMap,
  CPUArchitecture,
  LaunchpadBuildRevision,
  Release,
  Revision,
} from "../../types/releaseTypes";
 
export function isInDevmode(revision: Revision) {
  return revision.confinement === "devmode" || revision.grade === "devel";
}
 
export function getChannelName(track: string, risk: string, branch?: string | null) {
  if (risk === AVAILABLE) {
    return AVAILABLE;
  }
 
  return getChannelString({
    track,
    risk,
    branch,
  });
}
 
export function getBuildId<T extends Revision<boolean>>(
  revision: T
): T["attributes"]["build-request-id"] {
  return (
    revision && revision.attributes && revision.attributes["build-request-id"]
  );
}
 
export function isRevisionBuiltOnLauchpad(revision: Revision) {
  const buildId = getBuildId(revision);
  return !!(buildId && buildId.indexOf("lp-") === 0);
}
 
export function getRevisionsArchitectures(revisions: Revision[]) {
  let archs: CPUArchitecture[] = [];
 
  // get all architectures from all revisions
  revisions.forEach((revision) => {
    archs = archs.concat(revision.architectures);
  });
 
  // make archs unique and sorted
  archs = archs.filter((item, i, ar) => ar.indexOf(item) === i).sort();
 
  return archs;
}
 
export function getLatestRelease(
  revision: { releases?: Release[] },
  channel: string
) {
  const releases = revision.releases;
  if (!releases) {
    return null;
  }
  const filteredReleases = releases.filter((r) => r.channel === channel);
  if (filteredReleases.length === 0) {
    return null;
  }
  return filteredReleases[0];
}
 
export function isSameVersion(revisions?: ArchitectureRevisionsMap) {
  let hasSameVersion = false;
  const versionsMap: { [version: string]: CPUArchitecture[] } = {};
 
  Eif (revisions) {
    // calculate map of architectures for each version
    for (const arch in revisions) {
      const revision = revisions[arch];
      const version = revision.version;
      if (!versionsMap[version]) {
        versionsMap[version] = [];
      }
      versionsMap[version].push(arch);
    }
 
    hasSameVersion = Object.keys(versionsMap).length === 1;
  }
 
  return hasSameVersion;
}
 
export function canBeReleased(revision: { status: string }) {
  const allowed = [REVISION_STATUS.PUBLISHED, REVISION_STATUS.UNPUBLISHED];
 
  return revision && allowed.includes(revision.status);
}
 
export function validatePhasingPercentage(value: string) {
  if (value.trim()) {
    if (isNaN(parseInt(value))) {
      return "Phasing percentage must be a number";
    } else {
      const percentage = parseFloat(value);
      if (percentage < 0 || percentage > 100) {
        return "Phasing percentage must be between 0 and 100";
      }
    }
  }
  return "";
}
 
export function resizeAsidePanel(panelType: string) {
  useEffect(() => {
    const adjustAsidePanelHeight = () => {
      const targetComponent = document.querySelector("#main-content");
      let asidePanel;
 
      if (panelType === "add") {
        asidePanel = document.querySelector(
          "#add-track-aside-panel"
        ) as HTMLElement;
      } else {
        asidePanel = document.querySelector(
          "#request-track-aside-panel"
        ) as HTMLElement;
      }
 
      if (targetComponent && asidePanel) {
        const targetRect = targetComponent.getBoundingClientRect();
        const targetTop = targetRect.top;
        const targetBottom = targetRect.bottom;
        const viewportHeight = window.innerHeight;
 
        if (targetBottom > viewportHeight) {
          asidePanel.style.position = "fixed";
          asidePanel.style.top = `${targetTop}px`;
          asidePanel.style.bottom = "0";
        } else {
          asidePanel.style.position = "sticky";
          asidePanel.style.top = `${targetTop}px`;
        }
      }
    };
 
    adjustAsidePanelHeight();
 
    window.addEventListener("resize", adjustAsidePanelHeight);
    window.addEventListener("scroll", adjustAsidePanelHeight);
 
    return () => {
      window.removeEventListener("resize", adjustAsidePanelHeight);
      window.removeEventListener("scroll", adjustAsidePanelHeight);
    };
  });
}
 
export function numericalSort(a: string, b: string) {
  const regex = /\d+/g;
  const numSeqA = (a.match(regex) || []).map(Number);
  const numSeqB = (b.match(regex) || []).map(Number);
 
  for (let i = 0; i < Math.max(numSeqA.length, numSeqB.length); i++) {
    const numA = numSeqA[i] || 0;
    const numB = numSeqB[i] || 0;
 
    if (numA !== numB) {
      return numA - numB;
    }
  }
 
  return a.localeCompare(b);
}
 
interface PackageMetadata {
  id: string;
  private: boolean;
  publisher: {
    id: string;
    username: string | null;
    "display-name": string | null;
    email?: string;
    validation?: string;
  };
  status: string;
  store: string;
  type: string;
  authority: string | null;
  contact: string | null;
  description: string | null;
  name: string | null;
  summary: string | null;
  title: string | null;
  website: string | null;
  "default-track": string | null;
  links: Record<string, string[]> | null; // e.g. { "homepage": ["https://example.com"] }
  media: {
    type: "icon"; // technically it's an enum
    url: string;
  }[];
  "track-guardrails":
    | {
        pattern: string;
        "created-at": string;
      }[]
    | null;
  tracks:
    | {
        name: string;
        "version-pattern": string | null;
        "created-at": string;
        "automatic-phasing-percentage": number | null;
      }[]
    | null;
}
 
export async function getPackageMetadata(
  snap: string
): Promise<PackageMetadata> {
  const url = `/api/packages/${snap}`;
  const response = await fetch(url, {
    method: "GET",
    headers: {
      "Content-Type": "application/json",
    },
  });
 
  Iif (!response.ok) {
    throw new Error("There was a problem fetching the snap's metadata");
  }
  const data = await response.json();
  return data.data;
}