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 <bugsounet@bugsounet.fr>
This commit is contained in:
Bugsounet - Cédric 2023-06-08 22:41:48 +02:00 committed by GitHub
parent b7371538bc
commit e985e99036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 6 deletions

View File

@ -15,6 +15,7 @@ _This release is scheduled to be released on 2023-07-01._
- Added tests for serveronly - Added tests for serveronly
- Set Timezone `Europe/Berlin` in unit tests (needed for new formatTime tests) - Set Timezone `Europe/Berlin` in unit tests (needed for new formatTime tests)
- Added no-param-reassign eslint rule and fix warnings - Added no-param-reassign eslint rule and fix warnings
- updatenotification: Added `sendUpdatesNotifications` feature. Broadcast update with `UPDATES` notification to other modules
### Removed ### Removed

View File

@ -9,6 +9,7 @@ const BASE_DIR = path.normalize(`${__dirname}/../../../`);
class GitHelper { class GitHelper {
constructor() { constructor() {
this.gitRepos = []; this.gitRepos = [];
this.gitResultList = [];
} }
getRefRegex(branch) { getRefRegex(branch) {
@ -171,21 +172,38 @@ class GitHelper {
} }
async getRepos() { async getRepos() {
const gitResultList = []; this.gitResultList = [];
for (const repo of this.gitRepos) { for (const repo of this.gitRepos) {
try { try {
const gitInfo = await this.getRepoInfo(repo); const gitInfo = await this.getRepoInfo(repo);
if (gitInfo) { if (gitInfo) {
gitResultList.push(gitInfo); this.gitResultList.push(gitInfo);
} }
} catch (e) { } catch (e) {
Log.error(`Failed to retrieve repo info for ${repo.module}: ${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;
} }
} }

View File

@ -44,6 +44,11 @@ module.exports = NodeHelper.create({
this.sendSocketNotification("STATUS", repo); 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); this.scheduleNextFetch(this.config.updateInterval);
}, },

View File

@ -8,7 +8,8 @@ Module.register("updatenotification", {
defaults: { defaults: {
updateInterval: 10 * 60 * 1000, // every 10 minutes updateInterval: 10 * 60 * 1000, // every 10 minutes
refreshInterval: 24 * 60 * 60 * 1000, // one day refreshInterval: 24 * 60 * 60 * 1000, // one day
ignoreModules: [] ignoreModules: [],
sendUpdatesNotifications: false
}, },
suspended: false, suspended: false,
@ -40,8 +41,13 @@ Module.register("updatenotification", {
}, },
socketNotificationReceived(notification, payload) { socketNotificationReceived(notification, payload) {
if (notification === "STATUS") { switch (notification) {
this.updateUI(payload); case "STATUS":
this.updateUI(payload);
break;
case "UPDATES":
this.sendNotification("UPDATES", payload);
break;
} }
}, },