 (ns puppeteer.domain.usecase.deploy
   (:import (io.fabric8.kubernetes.api.model IntOrString)
            (io.fabric8.kubernetes.api.model.extensions IngressRule HTTPIngressRuleValue HTTPIngressPath IngressBackend))
-  (:require [com.stuartsierra.component :as component]
+  (:require [clojure.string :as string]
+            [com.stuartsierra.component :as component]
             [flatland.ordered.map :refer [ordered-map]]
             [puppeteer.infra.repository.deploy :as deployrepo]))
+
+(defn- sanitize-name
+  [name]
+  (string/replace name #"[\\\/\_\.]" "-"))
+
+(defn- ->app
+  [{:keys [repo-name branch-name] :as job}]
+  (str (sanitize-name repo-name) "-" (sanitize-name branch-name)))
+
+(defn- ->host
+  [{:keys [domain repo-name branch-name]}]
+  (str (sanitize-name repo-name) "-" (sanitize-name branch-name) "." domain))
+
+(defn- ->service-name
+  [{:keys [repo-name branch-name] :as job}]
+  (str (sanitize-name repo-name) "-" (sanitize-name branch-name)))
 
 (defn- update-containers-image
   [{:keys [conf build] :as job} containers]

                   :repo repo-name
                   :ref branch-name
                   :path (-> conf :k8s :service)})
        app (str repo-name "-" branch-name)]
    (-> service
        (assoc-in [:metadata :name] app)
        (assoc-in [:spec :selector :app] app))))

(defn round-up
  [{:keys [deploy-repository] :as comp}
   job]
  (let [app (->app job)]
    (deployrepo/delete-service deploy-repository {:app app})
    (deployrepo/delete-deployment deploy-repository {:app app})
    (deployrepo/remove-subdomain deploy-repository job)))
