Skip to content

Commit

Permalink
Notify the administrators when one of the installed licensed applicat…
Browse files Browse the repository at this point in the history
…ions has a new version available xwikisas#110

* send event message as json, since doing a split is error-prone
* small refactorings
  • Loading branch information
oanalavinia committed Jul 19, 2022
1 parent 065a1bc commit 2f3831d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,40 @@ public void checkLicensedExtensionsAvailableVersions()
InstalledExtension installedExtension = installedRepository.getInstalledExtension(extensionId);
Collection<String> namespaces = installedExtension.getNamespaces();
if (namespaces == null) {
notifyExtensionAvailableVersion(installedExtension.getId(), null);
notifyExtensionVersionAvailable(installedExtension.getId(), null);
} else {
for (String namespace : installedExtension.getNamespaces()) {
notifyExtensionAvailableVersion(installedExtension.getId(), namespace);
notifyExtensionVersionAvailable(installedExtension.getId(), namespace);
}
}
}

}

private void notifyExtensionAvailableVersion(ExtensionId extensionId, String namespace)
private void notifyExtensionVersionAvailable(ExtensionId extensionId, String namespace)
{
InstalledExtension installedExtension =
installedRepository.getInstalledExtension(extensionId.getId(), namespace);
// Get the list of versions that can be installed, with the first one being the most recent.
List<Version> installableVersions = upgradeExtensionHandler.getInstallableVersions(installedExtension.getId());
if (installableVersions.size() <= 0) {
if (installableVersions.isEmpty()) {
return;
}

List<String> newVersionNotifiedExtensions = new ArrayList<>(licensingConfig.getNewVersionNotifiedExtensions());
String namespaceName = namespace != null ? namespace : "root";
// Create an identified for the extension, by consider also the version and namespace.
String verifiedExtensionId =
// Create an identifier for the extension, by consider also the version and namespace.
String currentExtensionId =
String.format("%s-%s-%s", extensionId.getId(), namespaceName, installableVersions.get(0));

if (!newVersionNotifiedExtensions.contains(verifiedExtensionId)) {
newVersionNotifiedExtensions.add(verifiedExtensionId);
if (!newVersionNotifiedExtensions.contains(currentExtensionId)) {
newVersionNotifiedExtensions.add(currentExtensionId);
licensingConfig.setNewVersionNotifiedExtensions(newVersionNotifiedExtensions);

String message =
String.format("%s - %s - %s", installedExtension.getName(), namespaceName, installableVersions.get(0));
this.observationManager
.notify(
new NewExtensionVersionAvailableEvent(
new ExtensionId(extensionId.getId(), installableVersions.get(0)), namespace),
extensionId.getId(), message);
String message = String.format("{\"extensionName\":\"%s\", \"namespace\": \"%s\", \"version\": \"%s\"}",
installedExtension.getName(), namespaceName, installableVersions.get(0));
this.observationManager.notify(
new NewExtensionVersionAvailableEvent(new ExtensionId(extensionId.getId(), installableVersions.get(0)),
namespace), extensionId.getId(), message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#set ($smallIcon = "arrow_up")
#set ($compositeEventDate = $escapetool.xml($datetool.whenIs($event.dates.get(0))))

#macro (getExtensionUpgradeURL $event)
## The event body contains information about the extension id, version and targeted namespace.
#set ($extensionInfo = $event.getBody().split(' - '))
#set ($extensionNamespace = "#if ($extensionInfo[1] == 'root')$xcontext.mainWikiName#{else}$extensionInfo[1]#end")
#macro (getExtensionUpgradeURL $extensionInfo)
#set ($extensionNamespace =
"#if ($extensionInfo.namespace == 'root')$xcontext.mainWikiName#{else}$extensionInfo.namespace#end")
#set ($queryParams = $escapetool.url({
'section': 'XWiki.Extensions',
'extensionId': $event.getApplication(),
'extensionVersion': $extensionInfo[2],
'extensionVersion': $extensionInfo.version,
'extensionNamespace': $extensionNamespace
}))
$xwiki.getURL($services.model.createDocumentReference($xcontext.mainWikiName, 'XWiki', 'XWikiPreferences'),
Expand All @@ -23,9 +22,12 @@
<td>
<span class="notification-event-user">$escapetool.xml($event.user.name)</span>
</td>
## The notification message is stored directly on the event body.
## The event body contains information about the extension name, version and targeted namespace.
#set ($extensionInfo = $jsontool.fromString($event.getBody()))
<td class="description">
<a href="#getExtensionUpgradeURL($event)">$escapetool.xml($event.getBody())</a>
<a href="#getExtensionUpgradeURL($extensionInfo)">
$extensionInfo.extensionName - $extensionInfo.namespace - $extensionInfo.version
</a>
</td>
<td class="text-right text-muted">$escapetool.xml($datetool.whenIs($event.date))</td>
</tr>
Expand Down

0 comments on commit 2f3831d

Please sign in to comment.