|
| 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 | +} |
0 commit comments