Skip to content

Commit 0267e41

Browse files
authored
Add Example Logging Http Notifier (#439)
2 parents fd9b648 + 2bfa382 commit 0267e41

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.jenkinsci.plugins.stashNotifier.example;
2+
3+
import java.io.PrintStream;
4+
import java.net.URI;
5+
6+
import org.jenkinsci.plugins.stashNotifier.HttpNotifier;
7+
import org.jenkinsci.plugins.stashNotifier.NotificationContext;
8+
import org.jenkinsci.plugins.stashNotifier.NotificationResult;
9+
import org.jenkinsci.plugins.stashNotifier.NotificationSettings;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import com.cloudbees.plugins.credentials.Credentials;
14+
15+
import edu.umd.cs.findbugs.annotations.NonNull;
16+
import net.sf.json.JSONObject;
17+
18+
/**
19+
* Included as an example to demonstrate how alternative notifiers work.
20+
* Set the system property <code>-Dorg.jenkinsci.plugins.stashNotifier.HttpNotifierSelector.className=org.jenkinsci.plugins.stashNotifier.example.LoggingHttpNotifierSelector<code>
21+
* to tell the system to choose this implementation.
22+
*/
23+
class LoggingHttpNotifier implements HttpNotifier {
24+
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingHttpNotifier.class);
25+
26+
@Override
27+
public @NonNull NotificationResult send(@NonNull URI uri, @NonNull JSONObject payload, @NonNull NotificationSettings settings, @NonNull NotificationContext context) {
28+
Credentials creds = settings.getCredentials();
29+
String runId = context.getRunId();
30+
PrintStream buildLog = context.getLogger();
31+
String target = uri == null ? "(undefined)" : uri.toASCIIString();
32+
buildLog.println("[stash-notifier] would have notified " + target + " with " + payload);
33+
if (creds != null) {
34+
Class<?> credsClass = creds.getClass();
35+
LOGGER.info("{} would have used {} to notify {} with payload {}", runId, credsClass.getSimpleName(), target, payload);
36+
} else {
37+
LOGGER.info("{} would have notified {} with payload {}", runId, target, payload);
38+
}
39+
return NotificationResult.newSuccess();
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jenkinsci.plugins.stashNotifier.example;
2+
3+
import org.jenkinsci.plugins.stashNotifier.HttpNotifier;
4+
import org.jenkinsci.plugins.stashNotifier.HttpNotifierSelector;
5+
import org.jenkinsci.plugins.stashNotifier.SelectionContext;
6+
7+
import edu.umd.cs.findbugs.annotations.NonNull;
8+
9+
/**
10+
* This is an example alternative way of selecting a {@link HttpNotifier}.
11+
*/
12+
class LoggingHttpNotifierSelector implements HttpNotifierSelector {
13+
14+
/**
15+
* @param context unused
16+
* @return {@link LoggingHttpNotifier}
17+
*/
18+
@Override
19+
public @NonNull HttpNotifier select(@NonNull SelectionContext context) {
20+
return new LoggingHttpNotifier();
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.jenkinsci.plugins.stashNotifier.example;
2+
3+
import com.google.inject.AbstractModule;
4+
import com.google.inject.Provides;
5+
import hudson.Extension;
6+
import jakarta.inject.Singleton;
7+
8+
@Extension
9+
public class LoggingNotifierModule extends AbstractModule {
10+
@Provides
11+
@Singleton
12+
LoggingHttpNotifierSelector providesLoggingNotifierSelector() {
13+
return new LoggingHttpNotifierSelector();
14+
}
15+
}

0 commit comments

Comments
 (0)