Skip to content

Commit

Permalink
Updates notification (#3119)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bugsounet and bugsounet committed Jun 8, 2023
1 parent b737153 commit e985e99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 21 additions & 3 deletions modules/default/updatenotification/git_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const BASE_DIR = path.normalize(`${__dirname}/../../../`);
class GitHelper {
constructor() {
this.gitRepos = [];
this.gitResultList = [];
}

getRefRegex(branch) {
Expand Down Expand Up @@ -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;
}
}

Expand Down
5 changes: 5 additions & 0 deletions modules/default/updatenotification/node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

Expand Down
12 changes: 9 additions & 3 deletions modules/default/updatenotification/updatenotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
},

Expand Down

0 comments on commit e985e99

Please sign in to comment.