Skip to content

Commit

Permalink
Merge pull request #123 from jenkinsci/message-via-rest
Browse files Browse the repository at this point in the history
delete system message via rest api
  • Loading branch information
mawinter69 authored Jun 15, 2024
2 parents 9dcedd8 + 88d209b commit ff1b760
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,24 @@ 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.

### Create System Message via REST api
![System Message](/docs/pics/system-message.png)<br/>

### Create/Delete System Messages via REST api
You can create system messages by doing a post request to `<jenkins_url>/customizable-header/addSystemMessage`.
To be able to later delete a system message, pass an id parameter in the call. In case you omit the id, an id will be generated and returned in the response body.

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` |
| 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 |

![System Message](/docs/pics/system-message.png)<br/>
To delete a system message do a post request to `<jenkins_url>/customizable-header/deleteSystemMessage?id=<id>`

## The weather symbols
To demonstrate the custom weather symbols download the svgs from `docs/svgs` to `userContent/svgs` in your
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ public void addSystemMessage(SystemMessage message) {
save();
}

public void deleteSystemMessage(String id) {
synchronized (systemMessages) {
systemMessages.removeIf(sm -> sm.getUid().equals(id));
}
save();
}

public List<AppNavLink> getLinks() {
return links;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ public boolean hasLinks() {

@POST
public HttpResponse doAddSystemMessage(@QueryParameter(fixEmpty = true) String message, @QueryParameter(fixEmpty = true) String level,
@QueryParameter String expireDate) throws IOException {
@QueryParameter String expireDate, @QueryParameter(fixEmpty = true) String id) 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");
}
try {
SystemMessage.SystemMessageColor lvl = SystemMessage.SystemMessageColor.valueOf(level);
SystemMessage msg = new SystemMessage(message, lvl, null);
SystemMessage msg = new SystemMessage(message, lvl, id);
msg.setExpireDate(expireDate);
CustomHeaderConfiguration config = CustomHeaderConfiguration.get();
config.addSystemMessage(msg);
Expand All @@ -83,6 +83,17 @@ public HttpResponse doAddSystemMessage(@QueryParameter(fixEmpty = true) String m
}
}

@POST
public void doDeleteSystemMessage(@QueryParameter(fixEmpty = true) String id) throws IOException {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (id == null) {
throw HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, "Missing parameters: id is mandatory");
}
CustomHeaderConfiguration config = CustomHeaderConfiguration.get();
config.deleteSystemMessage(id);
}


@POST
public void doDismissMessage(@QueryParameter String uid) throws IOException {
User user = User.current();
Expand Down

0 comments on commit ff1b760

Please sign in to comment.