<commit_before>package com.softwaremill.codebrag.activities.finders

import org.bson.types.ObjectId
import com.softwaremill.codebrag.dao.finders.views.{CommitState, CommitView}
import com.softwaremill.codebrag.domain.{CommitAuthorClassification, ReviewedCommit}
import com.softwaremill.codebrag.dao.user.UserDAO
import com.softwaremill.codebrag.cache.{BranchCommitCacheEntry, BranchCommitsCache, UserReviewedCommitsCache}
import org.joda.time.DateTime
import com.softwaremill.codebrag.service.config.ReviewProcessConfig

trait CommitReviewStateAppender {

  def reviewedCommitsCache: UserReviewedCommitsCache
  def userDao: UserDAO
  def config: ReviewProcessConfig

  def setCommitsReviewStates(commitsViews: List[CommitView], userId: ObjectId) = {
    commitsViews.map(setCommitReviewState(_, userId))
  }

  def setCommitReviewState(commitView: CommitView, userId: ObjectId) = {
    commitView.copy(state = calculateState(commitView, userId))
  }


  def calculateState(commitView: CommitView, userId: ObjectId): CommitState.Value = {
    if(commitTooOldForUser(commitView, userId)) {
      return CommitState.NotApplicable
    }
    if (reviewedByUser(commitView, userId)) {
      return CommitState.ReviewedByUser
    }
    if(fullyReviewed(commitView)) {
      return CommitState.ReviewedByOthers
    }
    if(isUserAnAuthor(commitView, userId)) {
      return CommitState.AwaitingOthersReview
    }
    CommitState.AwaitingUserReview
  }

  def reviewedByUser(commitView: CommitView, userId: ObjectId): Boolean = {
    reviewedCommitsCache.reviewedByUser(commitView.sha, userId)
  }

  def fullyReviewed(commit: CommitView): Boolean = {
    reviewedCommitsCache.usersWhoReviewed(commit.sha).size >= config.requiredReviewersCount
  }

  private def isUserAnAuthor(commit: CommitView, userId: ObjectId) = {
    userDao.findById(userId) match {
      case Some(user) => CommitAuthorClassification.commitAuthoredByUser(commit, user)<commit_msg>Minor - some privacy applied<commit_after>package com.softwaremill.codebrag.activities.finders

import org.bson.types.ObjectId
import com.softwaremill.codebrag.dao.finders.views.{CommitState, CommitView}
import com.softwaremill.codebrag.domain.{CommitAuthorClassification, ReviewedCommit}
import com.softwaremill.codebrag.dao.user.UserDAO
import com.softwaremill.codebrag.cache.{BranchCommitCacheEntry, BranchCommitsCache, UserReviewedCommitsCache}
import org.joda.time.DateTime
import com.softwaremill.codebrag.service.config.ReviewProcessConfig

trait CommitReviewStateAppender {

  def reviewedCommitsCache: UserReviewedCommitsCache
  def userDao: UserDAO
  def config: ReviewProcessConfig

  def setCommitsReviewStates(commitsViews: List[CommitView], userId: ObjectId) = {
    commitsViews.map(setCommitReviewState(_, userId))
  }

  def setCommitReviewState(commitView: CommitView, userId: ObjectId) = {
    commitView.copy(state = calculateReviewState(commitView, userId))
  }


  private def calculateReviewState(commitView: CommitView, userId: ObjectId): CommitState.Value = {
    if(commitTooOldForUser(commitView, userId)) {
      return CommitState.NotApplicable
    }
    if (reviewedByUser(commitView, userId)) {
      return CommitState.ReviewedByUser
    }
    if(fullyReviewed(commitView)) {
      return CommitState.ReviewedByOthers
    }
    if(isUserAnAuthor(commitView, userId)) {
      return CommitState.AwaitingOthersReview
    }
    CommitState.AwaitingUserReview
  }

  def reviewedByUser(commitView: CommitView, userId: ObjectId): Boolean = {
    reviewedCommitsCache.reviewedByUser(commitView.sha, userId)
  }

  def fullyReviewed(commit: CommitView): Boolean = {
    reviewedCommitsCache.usersWhoReviewed(commit.sha).size >= config.requiredReviewersCount
  }

  private def isUserAnAuthor(commit: CommitView, userId: ObjectId) = {
    userDao.findById(userId) match {
      case Some(user) => CommitAuthorClassification.commitAuthoredByUser(commit, user)
{'fraction_non_alphanumeric': 0.0878086026978875, 'fraction_numerical': 0.0, 'mean_word_length': 5.517412935323383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2567845', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1201, 'n_words': 193}