Skip to content

Commit 88d209b

Browse files
committed
delete system message via rest api
do a post to `<jenkins_url>/customizable-header/deleteSystemMessage?id=<id>`
1 parent bd4b3ed commit 88d209b

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ One or more system messages can be shown between the header and the breadcrumb b
5151
things related to the instance, e.g. a planned update of Jenkins, a downtime due to hardware replacement or an ongoing
5252
incident. You can include html (sanitized with owasp) in the message to apply some simple styling or include a link
5353
with more details.<br/>
54-
System messages can have an expiration time to automatically remove them. They can be dismissed on a per-user basis.
54+
System messages can have an expiration time to automatically remove them. They can be dismissed on a per-user basis.
5555

56-
### Create System Message via REST api
56+
![System Message](/docs/pics/system-message.png)<br/>
57+
58+
### Create/Delete System Messages via REST api
5759
You can create system messages by doing a post request to `<jenkins_url>/customizable-header/addSystemMessage`.
60+
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.
61+
5862
Parameters:
5963

60-
|Parameter|required| description |
61-
|---------|--------|--------------------------------------------------------------|
62-
| message | true | The message, can contain html |
63-
| level | true | Message level, one of `info`, `success`, `warning`, `danger` |
64-
| expireDate | false | Expiration date for the message, format: `yyyy-M-d H:m` |
64+
| Parameter |required| description |
65+
|------------|--------|--------------------------------------------------------------|
66+
| message | true | The message, can contain html |
67+
| level | true | Message level, one of `info`, `success`, `warning`, `danger` |
68+
| expireDate | false | Expiration date for the message, format: `yyyy-M-d H:m` |
69+
| uid | false | An optional unique id |
6570

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

6873
## The weather symbols
6974
To demonstrate the custom weather symbols download the svgs from `docs/svgs` to `userContent/svgs` in your

src/main/java/io/jenkins/plugins/customizable_header/CustomHeaderConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ public void addSystemMessage(SystemMessage message) {
172172
save();
173173
}
174174

175+
public void deleteSystemMessage(String id) {
176+
synchronized (systemMessages) {
177+
systemMessages.removeIf(sm -> sm.getUid().equals(id));
178+
}
179+
save();
180+
}
181+
175182
public List<AppNavLink> getLinks() {
176183
return links;
177184
}

src/main/java/io/jenkins/plugins/customizable_header/HeaderRootAction.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ public boolean hasLinks() {
6464

6565
@POST
6666
public HttpResponse doAddSystemMessage(@QueryParameter(fixEmpty = true) String message, @QueryParameter(fixEmpty = true) String level,
67-
@QueryParameter String expireDate) throws IOException {
67+
@QueryParameter String expireDate, @QueryParameter(fixEmpty = true) String id) throws IOException {
6868
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
6969
if (message == null || level == null) {
7070
throw HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, "Missing parameters: message and level are mandatory");
7171
}
7272
try {
7373
SystemMessage.SystemMessageColor lvl = SystemMessage.SystemMessageColor.valueOf(level);
74-
SystemMessage msg = new SystemMessage(message, lvl, null);
74+
SystemMessage msg = new SystemMessage(message, lvl, id);
7575
msg.setExpireDate(expireDate);
7676
CustomHeaderConfiguration config = CustomHeaderConfiguration.get();
7777
config.addSystemMessage(msg);
@@ -83,6 +83,17 @@ public HttpResponse doAddSystemMessage(@QueryParameter(fixEmpty = true) String m
8383
}
8484
}
8585

86+
@POST
87+
public void doDeleteSystemMessage(@QueryParameter(fixEmpty = true) String id) throws IOException {
88+
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
89+
if (id == null) {
90+
throw HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, "Missing parameters: id is mandatory");
91+
}
92+
CustomHeaderConfiguration config = CustomHeaderConfiguration.get();
93+
config.deleteSystemMessage(id);
94+
}
95+
96+
8697
@POST
8798
public void doDismissMessage(@QueryParameter String uid) throws IOException {
8899
User user = User.current();

0 commit comments

Comments
 (0)