Skip to content

Commit

Permalink
ops: attempt to rename release assets
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiksta committed Jul 11, 2024
1 parent caa2945 commit c627aac
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions .github/util-scripts/rename-release-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const API_BASE = 'https://api.github.com';
const makeApiClient = (url) => async (input, init) => {
const method = (init && init.method) ? init.method : 'GET';
const fullUrl = `${API_BASE}${url}${input}`;
const body = (init && init.body) ? body : '{}';
const body = (init && init.body) ? init.body : '{}';
console.log(`[api] ${method}: ${fullUrl} ${body}`);
const resp = await fetch(
fullUrl,
Expand All @@ -22,12 +22,20 @@ const makeApiClient = (url) => async (input, init) => {
...init,
}
);
const json = await resp.json();
console.log(`[api] -> ${JSON.stringify(json)}`);
return json;
let ret;
try { ret = await resp.json(); } catch { ret = ''; }

if (!resp.status.toString().startsWith('2')) {
console.error(resp.status);
console.error(json);
throw resp;
}
console.log(`[api] -> ${JSON.stringify(ret)}`);
return ret;
};

const releasesApi = makeApiClient('/repos/dominiksta/wournal/releases');
const assetsApi = makeApiClient('/repos/dominiksta/wournal/releases/assets');

// ----------------------------------------------------------------------
// api methods
Expand All @@ -40,23 +48,22 @@ const getAssets = async (releaseId) =>
(await releasesApi(`/${releaseId}/assets`, {}))
.map(a => ({ id: a.id, name: a.name }));

const renameAsset = (releaseId, assetId, name) =>
releasesApi(`/${releaseId}/assets/${assetId}`, {
const renameAsset = (assetId, name) =>
assetsApi(`/${assetId}`, {
method: 'PATCH', body: JSON.stringify({ name })
});

const deleteAsset = (releaseId, id) =>
releasesApi(`/${releaseId}/assets/${id}`, { method: 'DELETE' });
const deleteAsset = (id) => assetsApi(`/${id}`, { method: 'DELETE' });

// ----------------------------------------------------------------------
// detect & rename
// ----------------------------------------------------------------------

const RENAME = {
'Wournal-<<VER>>-x64.AppImage' : 'wournal-linux-<<VER>>.AppImage',
'Wournal-linux-x64-<<VER>>.zip' : 'wournal-linux-<<VER>>.zip',
'Wournal-win32-x64-<<VER>>.zip' : 'wournal-windows-<<VER>>.zip',
'Wournal.Setup.<<VER>>.exe' : 'wournal-windows-setup-<<VER>>.exe',
'Wournal-<<VER>>-x64.AppImage' : 'Wournal-Linux-<<VER>>.AppImage',
'Wournal-linux-x64-<<VER>>.zip' : 'Wournal-Linux-<<VER>>.zip',
'Wournal-win32-x64-<<VER>>.zip' : 'Wournal-Windows-<<VER>>.zip',
'Wournal.Setup.<<VER>>.exe' : 'Wournal-Windows-Setup-<<VER>>.exe',
};

const DELETE = [ 'latest.yml' ];
Expand All @@ -76,15 +83,15 @@ async function main() {
if (name === toRename.replace('<<VER>>', version)) {
const newName = RENAME[toRename].replace('<<VER>>', version);
console.log(`Renaming ${name} -> ${newName}`);
await renameAsset(releaseId, id, newName);
await renameAsset(id, newName);
continue avail;
}
}

for (const toDelete of DELETE) {
if (name === toDelete.replace('<<VER>>', version)) {
console.log(`Deleting ${name}`);
await deleteAsset(releaseId, id);
await deleteAsset(id);
continue avail;
}
}
Expand Down

0 comments on commit c627aac

Please sign in to comment.