<commit_before>package controllers

import models.MainTemplate
import core.{ServiceDescriptionBuilder, ServiceDescriptionValidator}
import lib.{UrlKey, Util}
import com.gilt.apidoc.models.{Organization, Version, Visibility}
import play.api._
import play.api.mvc._
import play.api.libs.json.Json
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import java.io.File

object Versions extends Controller {

  private val DefaultVersion = "0.0.1-dev"

  implicit val context = scala.concurrent.ExecutionContext.Implicits.global

  def redirectToLatest(orgKey: String, serviceKey: String) = Action {
    Redirect(routes.Versions.show(orgKey, serviceKey, "latest"))
  }

  def show(orgKey: String, serviceKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
    for {
      serviceResponse <- request.api.Services.getByOrgKey(orgKey = orgKey, key = Some(serviceKey))
      versionsResponse <- request.api.Versions.getByOrgKeyAndServiceKey(orgKey, serviceKey)
      versionOption <- request.api.Versions.getByOrgKeyAndServiceKeyAndVersion(orgKey, serviceKey, versionName)
      generators <- request.api.Generators.get()
    } yield {
      versionOption match {

        case None => {
          if ("latest" == versionName) {
            Redirect(routes.Organizations.show(orgKey)).flashing("warning" -> s"Service not found: ${serviceKey}")
          } else {
            Redirect(routes.Versions.show(orgKey, serviceKey, "latest")).flashing("warning" -> s"Version not found: $versionName")
          }
        }

        case Some(v: Version) => {
          val service = serviceResponse.headOption.getOrElse {
            sys.error(s"Could not find service for orgKey[$orgKey] and key[$serviceKey]")
          }

          val sd = ServiceDescriptionBuilder(v.json)
          val tpl = request.mainTemplate(Some(service.name + " " + v.version)).copy(
            service = Some(service),
            version = Some(v.version),
            allServiceVersions = versionsResponse.map(_.version),
            serviceDescription = Some(sd),
            generators = generators.filter(_.enabled)
          )
          Ok(views.html.versions.show(tpl, sd))
        }
      }
    }
  }

  def apiJson(orgKey: String, serviceKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
    request.api.Versions.getByOrgKeyAndServiceKeyAndVersion(orgKey, serviceKey, versionName).map {
      case None => {
        if ("latest" == versionName) {
          Redirect(routes.Organizations.show(orgKey)).flashing("warning" -> s"Service not found: ${serviceKey}")
        } else {
          Redirect(routes.Versions.show(orgKey, serviceKey, "latest"))
            .flashing("warning" -> s"Version not found: ${versionName}")
        }
      }<commit_msg>Move 'latest' keyword to a variable<commit_after>package controllers

import models.MainTemplate
import core.{ServiceDescriptionBuilder, ServiceDescriptionValidator}
import lib.{UrlKey, Util}
import com.gilt.apidoc.models.{Organization, Version, Visibility}
import play.api._
import play.api.mvc._
import play.api.libs.json.Json
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import java.io.File

object Versions extends Controller {

  private val DefaultVersion = "0.0.1-dev"
  private val LatestVersion = "latest"

  implicit val context = scala.concurrent.ExecutionContext.Implicits.global

  def redirectToLatest(orgKey: String, serviceKey: String) = Action {
    Redirect(routes.Versions.show(orgKey, serviceKey, LatestVersion))
  }

  def show(orgKey: String, serviceKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
    for {
      serviceResponse <- request.api.Services.getByOrgKey(orgKey = orgKey, key = Some(serviceKey))
      versionsResponse <- request.api.Versions.getByOrgKeyAndServiceKey(orgKey, serviceKey)
      versionOption <- request.api.Versions.getByOrgKeyAndServiceKeyAndVersion(orgKey, serviceKey, versionName)
      generators <- request.api.Generators.get()
    } yield {
      versionOption match {

        case None => {
          if (LatestVersion == versionName) {
            Redirect(routes.Organizations.show(orgKey)).flashing("warning" -> s"Service not found: ${serviceKey}")
          } else {
            Redirect(routes.Versions.show(orgKey, serviceKey, LatestVersion)).flashing("warning" -> s"Version not found: $versionName")
          }
        }

        case Some(v: Version) => {
          val service = serviceResponse.headOption.getOrElse {
            sys.error(s"Could not find service for orgKey[$orgKey] and key[$serviceKey]")
          }

          val sd = ServiceDescriptionBuilder(v.json)
          val tpl = request.mainTemplate(Some(service.name + " " + v.version)).copy(
            service = Some(service),
            version = Some(v.version),
            allServiceVersions = versionsResponse.map(_.version),
            serviceDescription = Some(sd),
            generators = generators.filter(_.enabled)
          )
          Ok(views.html.versions.show(tpl, sd))
        }
      }
    }
  }

  def apiJson(orgKey: String, serviceKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
    request.api.Versions.getByOrgKeyAndServiceKeyAndVersion(orgKey, serviceKey, versionName).map {
      case None => {
        if (LatestVersion == versionName) {
          Redirect(routes.Organizations.show(orgKey)).flashing("warning" -> s"Service not found: ${serviceKey}")
        } else {
          Redirect(routes.Versions.show(orgKey, serviceKey, LatestVersion))
            .flashing("warning" -> s"Version not found: ${versionName}")
        }
      }
{'fraction_non_alphanumeric': 0.11515883977900553, 'fraction_numerical': 0.0010359116022099447, 'mean_word_length': 3.188720173535792, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1346911', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1633, 'n_words': 335}