Skip to content

Commit

Permalink
Allow disabling dismiss of system messages
Browse files Browse the repository at this point in the history
each system message can configured to be dismissible or not.
fix #160
  • Loading branch information
mawinter69 committed Dec 21, 2024
1 parent 6cbd4e8 commit ab3999e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ One or more system messages can be shown between the header and the breadcrumb b
things related to the instance, e.g. a planned update of Jenkins, a downtime due to hardware replacement or an ongoing
incident. You can include html (sanitized with owasp) in the message to apply some simple styling or include a link
with more details.<br/>
System messages can have an expiration time to automatically remove them. They can be dismissed on a per-user basis.
System messages can have an expiration time to automatically remove them. They can be dismissed on a per-user basis (can be disabled).

![System Message](/docs/pics/system-message.png)<br/>

Expand All @@ -61,12 +61,13 @@ To be able to later delete a system message, pass an id parameter in the call. I

Parameters:

| Parameter |required| description |
|------------|--------|--------------------------------------------------------------|
| message | true | The message, can contain html |
| level | true | Message level, one of `info`, `success`, `warning`, `danger` |
| expireDate | false | Expiration date for the message, format: `yyyy-M-d H:m` |
| uid | false | An optional unique id |
| Parameter | required | description |
|--------------|----------|--------------------------------------------------------------|
| message | true | The message, can contain html |
| level | true | Message level, one of `info`, `success`, `warning`, `danger` |
| expireDate | false | Expiration date for the message, format: `yyyy-M-d H:m` |
| uid | false | An optional unique id |
| dismissible | false | Message can be dismissed by users, defaults to `true` |

To delete a system message do a post request to `<jenkins_url>/customizable-header/deleteSystemMessage?id=<id>`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean hasLinks() {

@POST
public HttpResponse doAddSystemMessage(@QueryParameter(fixEmpty = true) String message, @QueryParameter(fixEmpty = true) String level,
@QueryParameter String expireDate, @QueryParameter(fixEmpty = true) String id) throws IOException {
@QueryParameter String expireDate, @QueryParameter(fixEmpty = true) String id, @QueryParameter(fixEmpty = true) Boolean dismissible) throws IOException {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (message == null || level == null) {
throw HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, "Missing parameters: message and level are mandatory");
Expand All @@ -74,6 +74,7 @@ public HttpResponse doAddSystemMessage(@QueryParameter(fixEmpty = true) String m
SystemMessage.SystemMessageColor lvl = SystemMessage.SystemMessageColor.valueOf(level);
SystemMessage msg = new SystemMessage(message, lvl, id);
msg.setExpireDate(expireDate);
msg.setDismissible(dismissible);

Check warning on line 77 in src/main/java/io/jenkins/plugins/customizable_header/HeaderRootAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 77 is not covered by tests
CustomHeaderConfiguration config = CustomHeaderConfiguration.get();
config.addSystemMessage(msg);
return HttpResponses.text(msg.getUid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class SystemMessage extends AbstractDescribableImpl<SystemMessage> {
private LocalDateTime expireDate;
private String uid;

private Boolean dismissible = true;

@DataBoundConstructor
public SystemMessage(String message, SystemMessageColor level, String uid) {
this.message = Util.fixEmptyAndTrim(message);
Expand All @@ -38,6 +40,17 @@ public SystemMessage(String message, SystemMessageColor level, String uid) {
this.uid = uid;
}

public Boolean getDismissible() {
return dismissible;
}

@DataBoundSetter
public void setDismissible(Boolean dismissible) {
if (dismissible != null) {
this.dismissible = dismissible;
}
}

@DataBoundSetter
public void setExpireDate(String expireDate) {
if (Util.fixEmptyAndTrim(expireDate) != null) {
Expand All @@ -58,7 +71,7 @@ public String getExpireDate() {

public boolean isDismissed() {
User user = User.current();
if (user != null) {
if (user != null && dismissible) {
UserHeader userHeader = user.getProperty(UserHeader.class);
if (userHeader != null) {
return userHeader.getDismissedMessages().contains(uid);
Expand Down Expand Up @@ -101,6 +114,9 @@ public void setColor(String color) {
public Object readResolve() {
setColor(color);
color = null;
if (dismissible == null) {
dismissible = true;

Check warning on line 118 in src/main/java/io/jenkins/plugins/customizable_header/SystemMessage.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 31-118 are not covered by tests
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<f:entry>
<f:textarea field="message"/>
</f:entry>
<f:entry field="dismissible" title="${%Dismissible}">
<f:checkbox default="true"/>
</f:entry>
<f:entry field="level" title="${%Background of System Message}">
<f:enum>${it.toString()}</f:enum>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
When not checked, users are not able to dismiss the message.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<j:if test="${!it.dismissed}">
<div class="jenkins-alert jenkins-alert-${it.level.name()} custom-header__system-message-header">
<div><j:out value="${it.escapedMessage}"/></div>
<j:if test="${!h.isAnonymous()}">
<j:if test="${!h.isAnonymous() and it.dismissible}">
<a href="#" class="ch-dismiss-link" tooltip="${%Dismiss message}" data-uid="${it.uid}">
<l:icon src="symbol-close-outline plugin-ionicons-api" class="icon-sm"/>
</a>
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ fs.realpath@^1.0.0:

fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

gensync@^1.0.0-beta.2:
Expand Down

0 comments on commit ab3999e

Please sign in to comment.