From e985e99036321adc9b0095dbdffea44435ceccfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bugsounet=20-=20C=C3=A9dric?= Date: Thu, 8 Jun 2023 22:41:48 +0200 Subject: [PATCH] Updates notification (#3119) Hi, Like some default modules, I propose to send an `UPDATES` notification in an array with the git information of these modules This allows developers to create their own auto-update system (which I've been using in my case since 3 years, with automatic things) Of course, for security reasons `MagicMirror` is excluded --------- Co-authored-by: bugsounet --- CHANGELOG.md | 1 + .../default/updatenotification/git_helper.js | 24 ++++++++++++++++--- .../default/updatenotification/node_helper.js | 5 ++++ .../updatenotification/updatenotification.js | 12 +++++++--- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53caec0a..29acc643 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ _This release is scheduled to be released on 2023-07-01._ - Added tests for serveronly - Set Timezone `Europe/Berlin` in unit tests (needed for new formatTime tests) - Added no-param-reassign eslint rule and fix warnings +- updatenotification: Added `sendUpdatesNotifications` feature. Broadcast update with `UPDATES` notification to other modules ### Removed diff --git a/modules/default/updatenotification/git_helper.js b/modules/default/updatenotification/git_helper.js index b4e0299c..a4c603e0 100644 --- a/modules/default/updatenotification/git_helper.js +++ b/modules/default/updatenotification/git_helper.js @@ -9,6 +9,7 @@ const BASE_DIR = path.normalize(`${__dirname}/../../../`); class GitHelper { constructor() { this.gitRepos = []; + this.gitResultList = []; } getRefRegex(branch) { @@ -171,21 +172,38 @@ class GitHelper { } async getRepos() { - const gitResultList = []; + this.gitResultList = []; for (const repo of this.gitRepos) { try { const gitInfo = await this.getRepoInfo(repo); if (gitInfo) { - gitResultList.push(gitInfo); + this.gitResultList.push(gitInfo); } } catch (e) { Log.error(`Failed to retrieve repo info for ${repo.module}: ${e}`); } } - return gitResultList; + return this.gitResultList; + } + + async checkUpdates() { + var updates = []; + + const allRepos = await this.gitResultList.map((module) => { + return new Promise((resolve) => { + if (module.behind > 0 && module.module !== "MagicMirror") { + Log.info(`Update found for module: ${module.module}`); + updates.push(module); + } + resolve(module); + }); + }); + await Promise.all(allRepos); + + return updates; } } diff --git a/modules/default/updatenotification/node_helper.js b/modules/default/updatenotification/node_helper.js index ae3e0375..2f11cc7c 100644 --- a/modules/default/updatenotification/node_helper.js +++ b/modules/default/updatenotification/node_helper.js @@ -44,6 +44,11 @@ module.exports = NodeHelper.create({ this.sendSocketNotification("STATUS", repo); } + if (this.config.sendUpdatesNotifications) { + const updates = await this.gitHelper.checkUpdates(); + if (updates.length) this.sendSocketNotification("UPDATES", updates); + } + this.scheduleNextFetch(this.config.updateInterval); }, diff --git a/modules/default/updatenotification/updatenotification.js b/modules/default/updatenotification/updatenotification.js index 602e76f4..00d9e650 100644 --- a/modules/default/updatenotification/updatenotification.js +++ b/modules/default/updatenotification/updatenotification.js @@ -8,7 +8,8 @@ Module.register("updatenotification", { defaults: { updateInterval: 10 * 60 * 1000, // every 10 minutes refreshInterval: 24 * 60 * 60 * 1000, // one day - ignoreModules: [] + ignoreModules: [], + sendUpdatesNotifications: false }, suspended: false, @@ -40,8 +41,13 @@ Module.register("updatenotification", { }, socketNotificationReceived(notification, payload) { - if (notification === "STATUS") { - this.updateUI(payload); + switch (notification) { + case "STATUS": + this.updateUI(payload); + break; + case "UPDATES": + this.sendNotification("UPDATES", payload); + break; } },