This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from softwaremill/hit-them-all
Hit them all
- Loading branch information
Showing
23 changed files
with
305 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...n/src/main/scala/com/softwaremill/codebrag/domain/reactions/AllCommitsReviewedEvent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.softwaremill.codebrag.domain.reactions | ||
|
||
import com.softwaremill.codebrag.common._ | ||
import org.bson.types.ObjectId | ||
import org.joda.time.DateTime | ||
|
||
/** | ||
* Describes event when someone reviewed all commits for given branch at once (hit Mark All Reviewed) | ||
*/ | ||
case class AllCommitsReviewedEvent( | ||
repoName: String, | ||
branchName: String, | ||
userIdArg: ObjectId | ||
)(implicit clock: Clock) | ||
extends Event | ||
with StatisticEvent | ||
with Hookable { | ||
|
||
val hookName = "all-commits-reviewed-hook" | ||
|
||
def eventType = AllCommitsReviewedEvent.EventType | ||
|
||
def timestamp: DateTime = clock.nowUtc | ||
|
||
def userId = userIdArg | ||
|
||
def toEventStream: String = s"User $userIdArg reviewed all commits on branch $branchName" | ||
|
||
} | ||
|
||
object AllCommitsReviewedEvent { | ||
val EventType = "AllCommitsReviewed" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...src/main/scala/com/softwaremill/codebrag/usecases/reactions/ReviewAllCommitsUseCase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.softwaremill.codebrag.usecases.reactions | ||
|
||
import com.softwaremill.codebrag.cache.{RepositoriesCache, UserReviewedCommitsCache} | ||
import com.softwaremill.codebrag.common.{EventBus, Clock} | ||
import com.softwaremill.codebrag.dao.commitinfo.CommitInfoDAO | ||
import com.softwaremill.codebrag.dao.user.UserDAO | ||
import com.softwaremill.codebrag.domain.ReviewedCommit | ||
import com.softwaremill.codebrag.domain.reactions.AllCommitsReviewedEvent | ||
import com.softwaremill.codebrag.finders.commits.toreview.ToReviewBranchCommitsFilter | ||
import com.typesafe.scalalogging.slf4j.Logging | ||
import org.bson.types.ObjectId | ||
|
||
/** | ||
* Handles user activity when user wants to mark all commits as reviewed | ||
*/ | ||
class ReviewAllCommitsUseCase( | ||
userDao: UserDAO, | ||
eventBus: EventBus, | ||
commitInfoDAO: CommitInfoDAO, | ||
repoCache: RepositoriesCache, | ||
toReviewCommitsFilter: ToReviewBranchCommitsFilter, | ||
reviewedCommitsCache: UserReviewedCommitsCache | ||
) | ||
(implicit clock: Clock) extends Logging { | ||
|
||
type ReviewAllCommitsResult = Either[String, Unit] | ||
|
||
def execute(repoName: String, branchName: String, userId: ObjectId): ReviewAllCommitsResult = { | ||
|
||
val result = for { | ||
user <- userDao.findById(userId) | ||
} yield { | ||
logger.debug(s"Using $user to find all commits to review") | ||
|
||
val allBranchCommits = repoCache.getBranchCommits(repoName, branchName) | ||
val toReviewBranchCommits = toReviewCommitsFilter.filterCommitsToReview(allBranchCommits, user, repoName) | ||
|
||
logger.debug(s"Found ${toReviewBranchCommits.size} commits to mark as reviewed") | ||
|
||
commitInfoDAO.findByShaList(repoName, toReviewBranchCommits).foreach { commit => | ||
val reviewedCommit = ReviewedCommit(commit.sha, user.id, repoName, clock.nowUtc) | ||
|
||
logger.info(s"Marking commit ${reviewedCommit.sha} as reviewed by user $user") | ||
reviewedCommitsCache.markCommitAsReviewed(reviewedCommit) | ||
} | ||
|
||
eventBus.publish(AllCommitsReviewedEvent(repoName, branchName, userId)) | ||
} | ||
|
||
result.toRight { | ||
s"Cannot mark all commits in branch $branchName as reviewed by user $userId" | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...test/scala/com/softwaremill/codebrag/usecases/reactions/ReviewAllCommitsUseCaseSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.softwaremill.codebrag.usecases.reactions | ||
|
||
import com.softwaremill.codebrag.cache.{BranchCommitCacheEntry, RepositoriesCache, UserReviewedCommitsCache} | ||
import com.softwaremill.codebrag.common.{ClockSpec, EventBus} | ||
import com.softwaremill.codebrag.dao.commitinfo.CommitInfoDAO | ||
import com.softwaremill.codebrag.dao.user.UserDAO | ||
import com.softwaremill.codebrag.domain.{User, PartialCommitInfo, ReviewedCommit} | ||
import com.softwaremill.codebrag.domain.builder.CommitInfoAssembler | ||
import com.softwaremill.codebrag.domain.reactions.AllCommitsReviewedEvent | ||
import com.softwaremill.codebrag.finders.commits.toreview.ToReviewBranchCommitsFilter | ||
import org.bson.types.ObjectId | ||
import org.mockito.Mockito._ | ||
import org.scalatest.matchers.ShouldMatchers | ||
import org.scalatest.mock.MockitoSugar | ||
import org.scalatest.{BeforeAndAfterEach, FlatSpec} | ||
|
||
class ReviewAllCommitsUseCaseSpec | ||
extends FlatSpec with MockitoSugar with ShouldMatchers with BeforeAndAfterEach with ClockSpec { | ||
|
||
var eventBus: EventBus = _ | ||
var userDAO: UserDAO = _ | ||
var commitInfoDao: CommitInfoDAO = _ | ||
var repositoriesCache: RepositoriesCache = _ | ||
var toReviewBranchCommitsFilter: ToReviewBranchCommitsFilter = _ | ||
var reviewedCommitsCache: UserReviewedCommitsCache = _ | ||
|
||
val userId = ObjectId.get | ||
var user: User = _ | ||
|
||
var useCase: ReviewAllCommitsUseCase = _ | ||
|
||
val RepoName = "codebrag" | ||
val BranchName = "master" | ||
|
||
val commit = CommitInfoAssembler.randomCommit.withRepo(RepoName).get | ||
val commitCache = BranchCommitCacheEntry(commit.sha, commit.authorName, commit.authorEmail, commit.authorDate) | ||
|
||
override def beforeEach() { | ||
eventBus = mock[EventBus] | ||
userDAO = mock[UserDAO] | ||
commitInfoDao = mock[CommitInfoDAO] | ||
repositoriesCache = mock[RepositoriesCache] | ||
toReviewBranchCommitsFilter = mock[ToReviewBranchCommitsFilter] | ||
reviewedCommitsCache = mock[UserReviewedCommitsCache] | ||
|
||
user = mock[User] | ||
when(user.id).thenReturn(userId) | ||
|
||
useCase = new ReviewAllCommitsUseCase(userDAO, eventBus, commitInfoDao, repositoriesCache, toReviewBranchCommitsFilter, reviewedCommitsCache) | ||
|
||
// given | ||
when(userDAO.findById(userId)).thenReturn(Option(user)) | ||
when(repositoriesCache.getBranchCommits(RepoName, BranchName)).thenReturn(List(commitCache)) | ||
when(toReviewBranchCommitsFilter.filterCommitsToReview(List(commitCache), user, RepoName)).thenReturn(List(commit.sha)) | ||
when(commitInfoDao.findByShaList(RepoName, List(commit.sha))).thenReturn(List(PartialCommitInfo(commit))) | ||
} | ||
|
||
it should "generate all commits reviewed event" in { | ||
// when | ||
useCase.execute(RepoName, BranchName, userId) | ||
|
||
// then | ||
verify(eventBus).publish(AllCommitsReviewedEvent(RepoName, BranchName, user.id)) | ||
} | ||
|
||
it should "mark all commits as reviewed if commits were found" in { | ||
// when | ||
useCase.execute(RepoName, BranchName, userId) | ||
|
||
// then | ||
val expectedCommitReviewed = ReviewedCommit(commit.sha, user.id, RepoName, clock.nowUtc) | ||
verify(reviewedCommitsCache).markCommitAsReviewed(expectedCommitReviewed) | ||
} | ||
} |
Oops, something went wrong.