Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling dismiss of system messages #164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

@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 @@
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,79 +28,95 @@
private LocalDateTime expireDate;
private String uid;

private Boolean dismissible = true;

@DataBoundConstructor
public SystemMessage(String message, SystemMessageColor level, String uid) {
this.message = Util.fixEmptyAndTrim(message);
this.level = level;
if (Util.fixEmptyAndTrim(uid) == null) {
uid = UUID.randomUUID().toString();
}
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) {
this.expireDate = LocalDateTime.parse(expireDate, DATE_INPUT_FORMATTER);
}
}

public String getUid() {
return uid;
}

public String getExpireDate() {
if (expireDate != null) {
return expireDate.format(DATE_OUTPUT_FORMATTER);
}
return null;
}

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);
}
}
return false;
}

public boolean isExpired() {
if (expireDate != null) {
LocalDateTime now = LocalDateTime.now();
return expireDate.isBefore(now);
}
return false;
}

/**
* Sets the color for the system message.
* Only for backwards compatibility with CasC
*
* @param color
* @deprecated instead set the level in the constructor
*/
@DataBoundSetter
@Deprecated
public void setColor(String color) {
if (color != null) {
if (color.equals("lightyellow")) {
level = SystemMessageColor.info;
}
if (color.equals("red")) {
level = SystemMessageColor.danger;
}
if (color.equals("orange")) {
level = SystemMessageColor.warning;
}
}
}

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
Loading