Skip to content

Instantly share code, notes, and snippets.

@vanrysss
Last active August 18, 2017 17:44
Show Gist options
  • Save vanrysss/3ea97e4a31fd9367d884 to your computer and use it in GitHub Desktop.
Save vanrysss/3ea97e4a31fd9367d884 to your computer and use it in GitHub Desktop.
(ns feedback-service.core
(:require [clojure.data.json :as json]
[clojure.edn :as edn]
[org.httpkit.server :refer [run-server]]
[org.httpkit.client :as http]
[ring.middleware.params :as params]
[ring.middleware.keyword-params :as keyword-params]
[ring.middleware.nested-params :as nested-params]
[base64-clj.core :as base64]
[compojure.api.sweet :refer :all]
[schema.core :as s]
[clojure.tools.logging :as log]
[clj-yaml.core :as yaml])
(:gen-class))
;;;UTIL
(defn- write-uuid [^java.util.UUID m ^java.io.PrintWriter writer]
(.print writer (str \" m \")))
(extend java.util.UUID json/JSONWriter {:-write write-uuid})
(defn load-config
[file-name]
(yaml/parse-string (slurp file-name)))
(defonce config (load-config "./config/config.yml"))
(defonce jira-base-url (:jiraURL config))
;;;BUSINESS LOGIC
(defn jira-basic-auth [user pwd]
(str "Basic " (base64/encode (str user ":" pwd))))
(defn feedback-summary [feedback]
(if (< (count feedback) 20)
feedback
(subs feedback 0 19)
))
(defn jira-improvement [req]
{:body (json/write-str {:fields {
:project {:key (:jira-project config)}
:summary (feedback-summary (:feedback req))
:description (str (:feedback req) "\n"
"email: " (:email req) "\n"
"partner uuid: " (:partnerBid req) "\n"
"partner id: " (:partnerId req) "\n"
"user roles: " (:userRoles req) "\n"
"user agent: " (:userAgent req) "\n"
"tags: " (:tags req))
:issuetype {:name (:feedbackType req)}
}})
:headers {"Content-Type" "application/json"
"Authorization" (jira-basic-auth (:jira-user config) (:jira-pwd config))}
:insecure? true})
;;;ROUTES LOGIC
(defn get-pong [request]
"pong")
(defn post-improvement [request]
(let [{:keys [status headers body error] :as response}
@(http/post (str jira-base-url "/rest/api/2/issue") (jira-improvement request))]
(if (not= status 201) (log/error error))
{:status status})
)
;;;APP DEFINITION
(s/defschema Bug
{:userFullName s/Str
:userName s/Str
:email s/Str
:partnerBid java.util.UUID
:partnerId s/Num
:userRoles [s/Str]
:tags [s/Str]
:feedback s/Str
:userAgent s/Str
:feedbackType s/Str
})
(defapi app-routes
(swagger-ui)
(swagger-docs
{:info {:title "Feedback Service API"
:description "Proxies feedback from our frontend into Jira tickets"}
:tags [{:name "api", :description "feedback service"}]})
(context* "/api" []
:tags ["api"]
(GET* "/ping" [] get-pong)
(POST* "/feedback" []
:body [bug Bug]
:summary "Creates a ticket in Jira"
(post-improvement bug))
))
(defn app [request]
((-> app-routes
keyword-params/wrap-keyword-params
nested-params/wrap-nested-params
params/wrap-params)
request))
(defn -main []
(let [port (config :port)]
(run-server app {:port port})
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment