Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Created July 5, 2021 20:40
Show Gist options
  • Save andyczerwonka/f15d1a2192034da45965a475a0692e0e to your computer and use it in GitHub Desktop.
Save andyczerwonka/f15d1a2192034da45965a475a0692e0e to your computer and use it in GitHub Desktop.

Revisions

  1. andyczerwonka created this gist Jul 5, 2021.
    34 changes: 34 additions & 0 deletions JIRADiscordWebhookHandler.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import java.io.{InputStream, OutputStream}
    import com.amazonaws.services.lambda.runtime.{Context, RequestStreamHandler}
    import com.softwaremill.sttp._
    import io.circe.Json
    import io.circe.generic.auto._
    import io.circe.parser._
    import io.circe.syntax._
    class JiraDiscord extends RequestStreamHandler with Helpers {
    override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = {
    val logger = context.getLogger
    val rawJson = scala.io.Source.fromInputStream(input).getLines().mkString("\n")
    val jsonDoc = parse(rawJson).getOrElse(Json.Null)
    extractJiraBody(jsonDoc) map { json =>
    logger.log(json.spaces2)
    JiraParser.parse(json) map { event =>
    implicit val backend: SttpBackend[Id, Nothing] = HttpURLConnectionBackend()
    try {
    val title = s"${event.key}: ${event.summary}"
    val desc = s"**${event.eventTypeLabel}**\n${event.description()}"
    val msg = DiscordWebhook(title, event.url, desc, event.author()).asJson.noSpaces
    val request = sttp
    .contentType("application/json")
    .header("User-Agent", userAgent)
    .body(msg)
    .post(discordUri(jsonDoc))
    request.send()
    output.write(ok)
    } finally {
    backend.close()
    }
    }
    }
    }
    }