import { CreateSolutionInput } from './types'; export const getBranches = async (): Promise => { const branches: string[] = []; let page = 1; let morePages = true; while (morePages) { const result = await fetch( `https://api.github.com/repos/Axinom/mosaic-media-template/branches?per_page=100&page=${page}`, ); if (result.status !== 200) { console.warn('Failed to fetch the branches'); } page++; const data = await result.json(); branches.push(...data.map((branch: { name: string }) => branch.name)); if (data.length < 100) { morePages = false; } } const sortedBranches = branches.sort((a, b) => { // first branches starting with "releases/", then "main", then "dev", then the rest if (a.startsWith('releases/')) { return -1; } if (a === 'main') { return -1; } if (a === 'dev') { return -1; } if (b.startsWith('releases/')) { return 1; } if (b === 'main') { return 1; } if (b === 'dev') { return 1; } return 0; }); return sortedBranches; }; export const getCommitId = async ({ gitReference, gitReferenceType, }: Pick< CreateSolutionInput, 'gitReference' | 'gitReferenceType' >): Promise => { let url: string; if (gitReferenceType === 'tags') { url = `https://api.github.com/repos/Axinom/mosaic-media-template/git/refs/tags/${gitReference}`; } else { url = `https://api.github.com/repos/Axinom/mosaic-media-template/branches/${gitReference}`; } const result = await fetch(url); if (result.status !== 200) { console.log( 'Failed to fetch the head of the branch', url, result.statusText, ); return new Date().toISOString(); } const data = await result.json(); return gitReferenceType === 'tags' ? data.object.sha : data.commit.sha; };