All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.hubot.HubotNotifier Maven / Gradle / Ivy

There is a newer version: 3.0.12
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.fabric8.hubot; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.jaxrs.cfg.Annotations; import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; import io.fabric8.annotations.Protocol; import io.fabric8.annotations.ServiceName; import io.fabric8.kubernetes.api.ExceptionResponseMapper; import io.fabric8.utils.Strings; import org.apache.cxf.jaxrs.client.JAXRSClientFactory; import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.deltaspike.core.api.config.ConfigProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; import java.util.ArrayList; import java.util.List; import static io.fabric8.kubernetes.api.KubernetesFactory.createObjectMapper; import static io.fabric8.utils.cxf.WebClients.configureUserAndPassword; import static io.fabric8.utils.cxf.WebClients.createProviders; import static io.fabric8.utils.cxf.WebClients.disableSslChecks; /** * A service for notifying a message to the Hubot chat bot */ public class HubotNotifier { public static final String HUBOT_WEB_HOOK_SERVICE_NAME = "hubot-web-hook"; public static final String DEFAULT_ROOM_EXPRESSION = "#fabric8_${namespace}"; private static final transient Logger LOG = LoggerFactory.getLogger(HubotNotifier.class); private final String hubotUrl; private final String username; private final String password; private final String roomExpression; private HubotRestApi api; @Inject public HubotNotifier(@Protocol("http") @ServiceName(HUBOT_WEB_HOOK_SERVICE_NAME) String hubotUrl, @ConfigProperty(name = "HUBOT_USERNAME", defaultValue = "") String username, @ConfigProperty(name = "HUBOT_PASSWORD", defaultValue = "") String password, @ConfigProperty(name = "HUBOT_BUILD_ROOM", defaultValue = DEFAULT_ROOM_EXPRESSION) String roomExpression) { this.hubotUrl = hubotUrl; this.username = username; this.password = password; this.roomExpression = roomExpression; LOG.info("Starting HubotNotifier using address: " + hubotUrl); } /** * Sends a message to a given room in the chat bot * * @param room the name of the room usually using IRC style starting with # * @param message the notification message to send which can include links or refer to people via @somenick */ public void notifyRoom(String room, String message) { LOG.info("About to notify room: " + room + " message: " + message); try { getHubotRestApi().notify(room, message); } catch (Exception e) { LOG.error("Failed to notify hubot room: " + room + " with message: " + message + ". Reason: " + e, e); } } /** * Notifies the room for the given namespace and build config name with the given message. * * By default this maps to a single logical room; which could be global or typically parameterised with * the namespace and/or buildConfig. * * @param namespace the kubernetes namespace for the build * @param buildConfig the name of the build configuration * @param message the notification message */ public void notifyBuild(String namespace, String buildConfig, String message) { String room = roomExpression.replace("${namespace}", namespace).replace("${buildConfig}", buildConfig); notifyRoom(room, message); } protected HubotRestApi getHubotRestApi() { if (api == null) { api = createWebClient(HubotRestApi.class); } return api; } /** * Creates a JAXRS web client for the given JAXRS client */ protected T createWebClient(Class clientType) { List providers = createProviders(); WebClient webClient = WebClient.create(hubotUrl, providers); disableSslChecks(webClient); configureUserAndPassword(webClient, username, password); return JAXRSClientFactory.fromClient(webClient, clientType); } }