From c77032cc74b2d766cb8eb69b65a205dd0d8a5887 Mon Sep 17 00:00:00 2001 From: Thomas Draebing Date: Mon, 27 Jan 2020 11:20:28 +0100 Subject: [PATCH 01/14] Add MessageOfTheDay-entries to ServerInfo The message of the day functionality was lost in the new PolyGerrit-UI, since the original implementation relied on the server side addition of the messages to the host page. This change adds the messages registered to the MessageOfTheDay- extension point to the ServerInfo, which can be fetched via Rest API. This allows accessing this information from the new UI. The UI elements that consume this information will be added in a followup change. Change-Id: I1deff5afbd812a1efaf6ad11e871544b36a5d88d --- Documentation/rest-api-config.txt | 19 +++++++++++++ .../common/MessageOfTheDayInfo.java | 27 +++++++++++++++++++ .../gerrit/extensions/common/ServerInfo.java | 2 ++ .../server/restapi/config/GetServerInfo.java | 24 ++++++++++++++++- 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 java/com/google/gerrit/extensions/common/MessageOfTheDayInfo.java diff --git a/Documentation/rest-api-config.txt b/Documentation/rest-api-config.txt index 7be8c324b4..0e4cac28f1 100644 --- a/Documentation/rest-api-config.txt +++ b/Documentation/rest-api-config.txt @@ -1905,6 +1905,21 @@ The maximal memory size. The value is returned with a unit abbreviation The number of open files. |============================ +[[message-of-the-day-info]] +=== MessageOfTheDayInfo +The `MessageOfTheDayInfo` entity contains information about a message +that was registered with the `MessageOfTheDay`-extension by plugins. + +[options="header",cols="1,^1,5"] +|=========================== +|Field Name ||Description +|`id` ||ID of the message. +|`redisplay` || +Date and Time, when the message should be displayed again after it was dismissed +by the user. +|`html` ||Message in HTML-format. +|=========================== + [[plugin-config-info]] === PluginConfigInfo The `PluginConfigInfo` entity contains information about Gerrit @@ -1958,6 +1973,10 @@ information about Gerrit Information about the configuration from the link:config-gerrit.html#gerrit[gerrit] section as link:#gerrit-info[ GerritInfo] entity. +|`messages` || +List of messages registered with the `MessageOfTheDay`- +extension containing link:#message-of-the-day-info[ +MessageOfTheDayInfo] entities. |`note_db_enabled` |not set if `false`| Whether the NoteDb storage backend is fully enabled. |`plugin` || diff --git a/java/com/google/gerrit/extensions/common/MessageOfTheDayInfo.java b/java/com/google/gerrit/extensions/common/MessageOfTheDayInfo.java new file mode 100644 index 0000000000..f752f86a64 --- /dev/null +++ b/java/com/google/gerrit/extensions/common/MessageOfTheDayInfo.java @@ -0,0 +1,27 @@ +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.extensions.common; + +import java.util.Date; + +/** REST API representation of a "message of the day". */ +public class MessageOfTheDayInfo { + /** The ID of the message. */ + public String id; + /** The date and time the message will be displayed again after being dismissed by the user. */ + public Date redisplay; + /** The message in HTML-format. */ + public String html; +} diff --git a/java/com/google/gerrit/extensions/common/ServerInfo.java b/java/com/google/gerrit/extensions/common/ServerInfo.java index 8904f0a03b..27cf529a24 100644 --- a/java/com/google/gerrit/extensions/common/ServerInfo.java +++ b/java/com/google/gerrit/extensions/common/ServerInfo.java @@ -14,6 +14,7 @@ package com.google.gerrit.extensions.common; +import java.util.List; import java.util.Map; public class ServerInfo { @@ -22,6 +23,7 @@ public class ServerInfo { public ChangeConfigInfo change; public DownloadInfo download; public GerritInfo gerrit; + public List messages; public Boolean noteDbEnabled; public PluginConfigInfo plugin; public SshdInfo sshd; diff --git a/java/com/google/gerrit/server/restapi/config/GetServerInfo.java b/java/com/google/gerrit/server/restapi/config/GetServerInfo.java index eca26c34a5..04f4f8a824 100644 --- a/java/com/google/gerrit/server/restapi/config/GetServerInfo.java +++ b/java/com/google/gerrit/server/restapi/config/GetServerInfo.java @@ -27,6 +27,7 @@ import com.google.gerrit.extensions.common.ChangeConfigInfo; import com.google.gerrit.extensions.common.DownloadInfo; import com.google.gerrit.extensions.common.DownloadSchemeInfo; import com.google.gerrit.extensions.common.GerritInfo; +import com.google.gerrit.extensions.common.MessageOfTheDayInfo; import com.google.gerrit.extensions.common.PluginConfigInfo; import com.google.gerrit.extensions.common.ReceiveInfo; import com.google.gerrit.extensions.common.ServerInfo; @@ -36,7 +37,9 @@ import com.google.gerrit.extensions.common.UserConfigInfo; import com.google.gerrit.extensions.config.CloneCommand; import com.google.gerrit.extensions.config.DownloadCommand; import com.google.gerrit.extensions.config.DownloadScheme; +import com.google.gerrit.extensions.registration.DynamicSet; import com.google.gerrit.extensions.restapi.RestReadView; +import com.google.gerrit.extensions.systemstatus.MessageOfTheDay; import com.google.gerrit.extensions.webui.WebUiPlugin; import com.google.gerrit.server.EnableSignedPush; import com.google.gerrit.server.account.AccountVisibilityProvider; @@ -69,6 +72,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -100,6 +104,7 @@ public class GetServerInfo implements RestReadView { private final GerritOptions gerritOptions; private final ChangeIndexCollection indexes; private final SitePaths sitePaths; + private final DynamicSet messages; @Inject public GetServerInfo( @@ -123,7 +128,8 @@ public class GetServerInfo implements RestReadView { AgreementJson agreementJson, GerritOptions gerritOptions, ChangeIndexCollection indexes, - SitePaths sitePaths) { + SitePaths sitePaths, + DynamicSet motd) { this.config = config; this.accountVisibilityProvider = accountVisibilityProvider; this.authConfig = authConfig; @@ -145,6 +151,7 @@ public class GetServerInfo implements RestReadView { this.gerritOptions = gerritOptions; this.indexes = indexes; this.sitePaths = sitePaths; + this.messages = motd; } @Override @@ -155,6 +162,7 @@ public class GetServerInfo implements RestReadView { info.change = getChangeInfo(); info.download = getDownloadInfo(); info.gerrit = getGerritInfo(); + info.messages = getMessages(); info.noteDbEnabled = toBoolean(isNoteDbEnabled()); info.plugin = getPluginInfo(); info.defaultTheme = getDefaultTheme(); @@ -325,6 +333,20 @@ public class GetServerInfo implements RestReadView { return CharMatcher.is('/').trimTrailingFrom(docUrl) + '/'; } + private List getMessages() { + return this.messages.stream() + .filter(motd -> !Strings.isNullOrEmpty(motd.getHtmlMessage())) + .map( + motd -> { + MessageOfTheDayInfo m = new MessageOfTheDayInfo(); + m.id = motd.getMessageId(); + m.redisplay = motd.getRedisplay(); + m.html = motd.getHtmlMessage(); + return m; + }) + .collect(toList()); + } + private boolean isNoteDbEnabled() { return migration.readChanges(); } From 1fbf53df6049cdadc8b2b2dd5bb61a19eefd90d4 Mon Sep 17 00:00:00 2001 From: Thomas Draebing Date: Mon, 27 Jan 2020 17:55:55 +0100 Subject: [PATCH 02/14] Add UI element to display messages of the day This change adds an UI element that displays messages registered with the MessageOfTheDay extension point. The messages will be displayed below the main navigation header. This functionality was already part of the GWT-UI, but was never migrated to the new UI. The messages can be dismissed by a button click, that will create a cookie that will prevent the message from being displayed again until the redisplay time configured with the message. The content of the message can be HTML. Depending on where the message- content is coming from, this might be harmful. In case of the messageoftheday plugin, the message is added as a config from the Gerrit server site, which can probably be considered save. However, in a future change rendering HTML should be made configurable and switched off by default. Text should then be rendered using `gr-formatted-text`. Change-Id: I2cbbcbcd49734b645701b9fac62a016a269d37f2 --- .../gr-message-header/gr-message-header.html | 41 +++++++++++++ .../gr-message-header/gr-message-header.js | 52 ++++++++++++++++ .../gr-message-header_test.html | 60 +++++++++++++++++++ polygerrit-ui/app/elements/gr-app.html | 7 +++ polygerrit-ui/app/elements/gr-app.js | 4 ++ 5 files changed, 164 insertions(+) create mode 100644 polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.html create mode 100644 polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.js create mode 100644 polygerrit-ui/app/elements/core/gr-message-header/gr-message-header_test.html diff --git a/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.html b/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.html new file mode 100644 index 0000000000..b2b382ca50 --- /dev/null +++ b/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.html @@ -0,0 +1,41 @@ + + + + + + + + + diff --git a/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.js b/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.js new file mode 100644 index 0000000000..fac4a6a399 --- /dev/null +++ b/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header.js @@ -0,0 +1,52 @@ +/** + * @license + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + 'use strict'; + + Polymer({ + is: 'gr-message-header', + + properties: { + message: { + type: Object, + reflectToAttribute: true, + }, + _hidden: { + type: Boolean, + value: true, + }, + }, + + attached() { + if (!this.message || !this.message.html) { + return; + } + this._isHidden(); + this.$.message.innerHTML = this.message.html; + }, + + _handleDismissMessage() { + document.cookie = + `msg-${this.message.id}=1; expires=${this.message.redisplay}`; + this._hidden = true; + }, + + _isHidden() { + this._hidden = window.util.getCookie(`msg-${this.message.id}`) === '1'; + }, + }); +})(); diff --git a/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header_test.html b/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header_test.html new file mode 100644 index 0000000000..c275e55345 --- /dev/null +++ b/polygerrit-ui/app/elements/core/gr-message-header/gr-message-header_test.html @@ -0,0 +1,60 @@ + + + + +gr-message-header + + + + + + + + + + + + + diff --git a/polygerrit-ui/app/elements/gr-app.html b/polygerrit-ui/app/elements/gr-app.html index 3a9719d5b9..4fff9e6221 100644 --- a/polygerrit-ui/app/elements/gr-app.html +++ b/polygerrit-ui/app/elements/gr-app.html @@ -55,6 +55,7 @@ limitations under the License. + @@ -158,6 +159,12 @@ limitations under the License. class$="[[_computeShadowClass(_isShadowDom)]]"> +