<commit_before>package com.softwaremill.codebrag.rest

import org.scalatra._
import com.softwaremill.codebrag.service.user.Authenticator
import json.JacksonJsonSupport
import swagger.{Swagger, SwaggerSupport}

import com.softwaremill.codebrag.dao.reporting._
import org.bson.types.ObjectId
import com.softwaremill.codebrag.common.ObjectIdGenerator
import com.softwaremill.codebrag.service.diff.DiffService
import com.softwaremill.codebrag.dao.reporting.CommitListDTO
import scala.Some
import com.softwaremill.codebrag.dao.reporting.CommentListDTO
import com.softwaremill.codebrag.service.diff.FileWithDiff
import com.softwaremill.codebrag.service.comments.command.AddComment
import com.softwaremill.codebrag.service.github.GitHubCommitImportServiceFactory
import com.softwaremill.codebrag.activities.CommentActivity
import com.softwaremill.codebrag.dao.UserDAO

class CommitsServlet(val authenticator: Authenticator,
                     commitListFinder: CommitListFinder,
                     commentListFinder: CommentListFinder,
                     commentActivity: CommentActivity,
                     userDao: UserDAO, val swagger: Swagger,
                     diffService: DiffService, importerFactory: GitHubCommitImportServiceFactory)
  extends JsonServletWithAuthentication with CommitsServletSwaggerDefinition with JacksonJsonSupport {

  get("/") {
    // for all /commits/*
    halt(404)
  }

  post("/:id/comments", operation(addCommentOperation)) {
    haltIfNotAuthenticated
    val commitId = params("id")
    val messageBody = extractNotEmptyString("body")
    val command = AddComment(new ObjectId(commitId), user.id, messageBody)
    val newComment = commentActivity.commentOnCommit(command)
    userDao.findById(command.authorId) match {
      case Some(user) => CommentListItemDTO(newComment.id.toString, user.name, command.message, newComment.postingTime.toDate)
      case None => halt(400, s"Invalid user id $command.authorId")
    }
  }

  get("/:id/comments", operation(getCommentsOperation)) {
    haltIfNotAuthenticated
    val commitId = params("id")
    commentListFinder.findAllForCommit(new ObjectId(commitId))
  }

  get("/", operation(getCommitsOperation)) {
    // for /commits?type=* only
    haltIfNotAuthenticated
    params.get("type") match {
      case Some("pending") => fetchPendingCommits()
      case _ => pass()
    }
  }
<commit_msg>Bring back wrapping of newly added comment REST response - Fixes problems with displaying empty new comment<commit_after>package com.softwaremill.codebrag.rest

import org.scalatra._
import com.softwaremill.codebrag.service.user.Authenticator
import json.JacksonJsonSupport
import swagger.{Swagger, SwaggerSupport}

import com.softwaremill.codebrag.dao.reporting._
import org.bson.types.ObjectId
import com.softwaremill.codebrag.common.ObjectIdGenerator
import com.softwaremill.codebrag.service.diff.DiffService
import com.softwaremill.codebrag.dao.reporting.CommitListDTO
import scala.Some
import com.softwaremill.codebrag.dao.reporting.CommentListDTO
import com.softwaremill.codebrag.service.diff.FileWithDiff
import com.softwaremill.codebrag.service.comments.command.AddComment
import com.softwaremill.codebrag.service.github.GitHubCommitImportServiceFactory
import com.softwaremill.codebrag.activities.CommentActivity
import com.softwaremill.codebrag.dao.UserDAO

class CommitsServlet(val authenticator: Authenticator,
                     commitListFinder: CommitListFinder,
                     commentListFinder: CommentListFinder,
                     commentActivity: CommentActivity,
                     userDao: UserDAO, val swagger: Swagger,
                     diffService: DiffService, importerFactory: GitHubCommitImportServiceFactory)
  extends JsonServletWithAuthentication with CommitsServletSwaggerDefinition with JacksonJsonSupport {

  get("/") {
    // for all /commits/*
    halt(404)
  }

  post("/:id/comments", operation(addCommentOperation)) {
    haltIfNotAuthenticated
    val commitId = params("id")
    val messageBody = extractNotEmptyString("body")
    val command = AddComment(new ObjectId(commitId), user.id, messageBody)
    val newComment = commentActivity.commentOnCommit(command)
    userDao.findById(command.authorId) match {
      case Some(user) => AddCommentResponse(CommentListItemDTO(newComment.id.toString, user.name, command.message, newComment.postingTime.toDate))
      case None => halt(400, s"Invalid user id $command.authorId")
    }
  }

  get("/:id/comments", operation(getCommentsOperation)) {
    haltIfNotAuthenticated
    val commitId = params("id")
    commentListFinder.findAllForCommit(new ObjectId(commitId))
  }

  get("/", operation(getCommitsOperation)) {
    // for /commits?type=* only
    haltIfNotAuthenticated
    params.get("type") match {
      case Some("pending") => fetchPendingCommits()
      case _ => pass()
    }
  }

{'fraction_non_alphanumeric': 0.0919705038918476, 'fraction_numerical': 0.0024580090126997134, 'mean_word_length': 4.2960954446854664, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4370725', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1393, 'n_words': 253}