mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-15 17:21:13 +10:00
feat: update checker based gh releases
This commit is contained in:
143
server/tasks/check/update.ts
Normal file
143
server/tasks/check/update.ts
Normal file
@ -0,0 +1,143 @@
|
||||
import { type } from "arktype";
|
||||
import { systemConfig } from "../../internal/config/sys-conf";
|
||||
import * as semver from "semver";
|
||||
import type { TaskReturn } from "../../h3";
|
||||
import notificationSystem from "../../internal/notifications";
|
||||
|
||||
const latestRelease = type({
|
||||
url: "string", // api url for specific release
|
||||
html_url: "string", // user facing url
|
||||
id: "number", // release id
|
||||
tag_name: "string", // tag used for release
|
||||
name: "string", // release name
|
||||
draft: "boolean",
|
||||
prerelease: "boolean",
|
||||
created_at: "string",
|
||||
published_at: "string",
|
||||
});
|
||||
|
||||
export default defineTask<TaskReturn>({
|
||||
meta: {
|
||||
name: "check:update",
|
||||
},
|
||||
async run() {
|
||||
if (systemConfig.shouldCheckForUpdates()) {
|
||||
console.log("[Task check:update]: Checking for update");
|
||||
try {
|
||||
const response = await fetch(
|
||||
"https://api.github.com/repos/Drop-OSS/drop/releases/latest",
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
console.log("[Task check:update]: Failed to check for update", {
|
||||
status: response.status,
|
||||
body: response.body,
|
||||
});
|
||||
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: "" + response.status,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const resJson = await response.json();
|
||||
const body = latestRelease(resJson);
|
||||
if (body instanceof type.errors) {
|
||||
console.error(body.summary);
|
||||
console.log("GitHub Api response", resJson);
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: body.summary,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// const currVerStr = systemConfig.getDropVersion()
|
||||
const currVerStr = "v0.1";
|
||||
|
||||
const latestVer = semver.coerce(body.tag_name);
|
||||
const currVer = semver.coerce(currVerStr);
|
||||
if (latestVer === null) {
|
||||
const msg = "Github Api returned invalid semver tag";
|
||||
console.log("[Task check:update]:", msg);
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: msg,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (currVer === null) {
|
||||
const msg = "Drop provided a invalid semver tag";
|
||||
console.log("[Task check:update]:", msg);
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: msg,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (semver.gt(latestVer, currVer)) {
|
||||
console.log("[Task check:update]: Update available");
|
||||
notificationSystem.pushAllAdmins({
|
||||
nonce: `drop-update-available-${currVer}-to-${latestVer}`,
|
||||
title: `Update available to v${latestVer}`,
|
||||
description: `A new version of Drop is available v${latestVer}`,
|
||||
actions: [`View|${body.html_url}`],
|
||||
});
|
||||
} else {
|
||||
console.log("[Task check:update]: no update available");
|
||||
}
|
||||
|
||||
console.log("[Task check:update]: Done");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (typeof e === "string") {
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: e,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (e instanceof Error) {
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: e.message,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: "unknown cause, please check console",
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
result: {
|
||||
success: true,
|
||||
data: undefined,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user