<commit_before>package controllers

import java.io.File
import javax.inject.Inject

import io.apibuilder.api.v0.models._
import io.apibuilder.spec.v0.models.json._
import lib.{Util, VersionTag}
import play.api.data.Forms._
import play.api.data._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json._
import play.api.mvc.{Action, Controller}

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

class Versions @Inject() (val messagesApi: MessagesApi) extends Controller with I18nSupport {

  private[this] val DefaultVersion = "0.0.1-dev"
  private[this] val LatestVersion = "latest"

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

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

  def show(orgKey: String, applicationKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
    for {
      applicationResponse <- request.api.applications.get(orgKey = orgKey, key = Some(applicationKey))
      versionsResponse <- request.api.versions.getByApplicationKey(orgKey, applicationKey)
      versionOption <- lib.ApiClient.callWith404(
        request.api.versions.getByApplicationKeyAndVersion(orgKey, applicationKey, versionName)
      )
      generators <- request.api.generatorWithServices.get()
      watches <- isWatching(request.api, request.user, orgKey, applicationKey)
    } yield {
      applicationResponse.headOption match {
        case None => {
          Redirect(routes.Organizations.show(orgKey)).flashing("warning" -> s"Application not found: ${applicationKey}")
        }
        case Some(application) => {
          versionOption match {

            case None => {
              if (LatestVersion == versionName) {
                if (request.isMember) {
                  Redirect(routes.Versions.create(orgKey, application = Some(applicationKey))).flashing("success" -> s"Application does not yet have any versions")
                } else {
                  Redirect(routes.Organizations.show(orgKey)).flashing("success" -> s"Application does not have any versions")
                }
              } else {
                Redirect(routes.Versions.show(orgKey, applicationKey, LatestVersion)).flashing("warning" -> s"Version not found: $versionName")
              }
            }

            case Some(v: Version) => {
              // TODO: For updates, include application in the template
              val tpl = request.mainTemplate(Some(v.service.name + " " + v.version)).copy(
                version = Some(v.version),
                allServiceVersions = versionsResponse.map(_.version),
                service = Some(v.service),
                versionObject = Some(v),
                generators = generators
              )
              Ok(views.html.versions.show(tpl, application, v.service, watches))
            }
          }
        }
      }
    }
  }

  def original(orgKey: String, applicationKey: String, versionName: String) = AnonymousOrg.async { implicit request =><commit_msg>set Cache-Control response header in Versions controller (#650)<commit_after>package controllers

import java.io.File
import javax.inject.Inject

import io.apibuilder.api.v0.models._
import io.apibuilder.spec.v0.models.json._
import lib.{Util, VersionTag}
import play.api.data.Forms._
import play.api.data._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json._
import play.api.mvc.{Action, Controller}

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

class Versions @Inject() (val messagesApi: MessagesApi) extends Controller with I18nSupport {

  private[this] val DefaultVersion = "0.0.1-dev"
  private[this] val LatestVersion = "latest"

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

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

  def show(orgKey: String, applicationKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
    for {
      applicationResponse <- request.api.applications.get(orgKey = orgKey, key = Some(applicationKey))
      versionsResponse <- request.api.versions.getByApplicationKey(orgKey, applicationKey)
      versionOption <- lib.ApiClient.callWith404(
        request.api.versions.getByApplicationKeyAndVersion(orgKey, applicationKey, versionName)
      )
      generators <- request.api.generatorWithServices.get()
      watches <- isWatching(request.api, request.user, orgKey, applicationKey)
    } yield {
      applicationResponse.headOption match {
        case None => {
          Redirect(routes.Organizations.show(orgKey)).flashing("warning" -> s"Application not found: ${applicationKey}")
        }
        case Some(application) => {
          versionOption match {

            case None => {
              if (LatestVersion == versionName) {
                if (request.isMember) {
                  Redirect(routes.Versions.create(orgKey, application = Some(applicationKey))).flashing("success" -> s"Application does not yet have any versions")
                } else {
                  Redirect(routes.Organizations.show(orgKey)).flashing("success" -> s"Application does not have any versions")
                }
              } else {
                Redirect(routes.Versions.show(orgKey, applicationKey, LatestVersion)).flashing("warning" -> s"Version not found: $versionName")
              }
            }

            case Some(v: Version) => {
              // TODO: For updates, include application in the template
              val tpl = request.mainTemplate(Some(v.service.name + " " + v.version)).copy(
                version = Some(v.version),
                allServiceVersions = versionsResponse.map(_.version),
                service = Some(v.service),
                versionObject = Some(v),
                generators = generators
              )
              Ok(views.html.versions.show(tpl, application, v.service, watches)).withHeaders(
                CACHE_CONTROL -> "max-age=0, s-maxage=0"
              )
            }
          }
        }
      }
    }
  }

  def original(orgKey: String, applicationKey: String, versionName: String) = AnonymousOrg.async { implicit request =>
{'fraction_non_alphanumeric': 0.1080827067669173, 'fraction_numerical': 0.005169172932330827, 'mean_word_length': 2.716530849825378, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 24, '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': '5197653', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1717, 'n_words': 366}