Skip to content

Commit c434e6c

Browse files
authored
Merge pull request #293 from hossain-khan/feature/292-use-botuser
[UPDATE] Use bot user to ignore PR author
2 parents b431ce1 + e8556aa commit c434e6c

File tree

3 files changed

+489
-3
lines changed

3 files changed

+489
-3
lines changed

src/main/kotlin/dev/hossain/githubstats/repository/PullRequestStatsRepoImpl.kt

+16-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class PullRequestStatsRepoImpl(
5151
return StatsResult.Failure(IllegalStateException("PR has not been merged, no reason to analyze PR stats."))
5252
}
5353

54+
if (pullRequest.user.login in botUserIds) {
55+
// Skips PR stats generation if PR is created by bot user.
56+
Log.i("The PR#${pullRequest.number} is created by bot user '${pullRequest.user.login}'. Skipping PR stat analysis.")
57+
return StatsResult.Failure(
58+
IllegalStateException("PR has been created by bot user '${pullRequest.user.login}', no reason to analyze PR stats."),
59+
)
60+
}
61+
5462
// API request to get all timeline events for the PR
5563
val prTimelineEvents = timelinesPager.getAllTimelineEvents(repoOwner, repoId, prNumber)
5664
// API request to get all PR source code review comments associated with diffs
@@ -61,14 +69,20 @@ class PullRequestStatsRepoImpl(
6169
val prAvailableForReviewOn: Instant = prAvailableForReviewTime(pullRequest.prCreatedOn, prTimelineEvents)
6270

6371
// List of users who has been requested as reviewer or reviewed the PR
72+
val nonFilteredPrReviewerUsers = prReviewers(pullRequest.user, prTimelineEvents)
6473
val prReviewers: Set<User> =
65-
prReviewers(pullRequest.user, prTimelineEvents)
74+
nonFilteredPrReviewerUsers
6675
// Filters out the bot users from the reviewers
6776
.filter { it.login !in botUserIds }.toSet()
6877

6978
if (prReviewers.isEmpty()) {
7079
Log.w("No human reviewers found for PR#${pullRequest.number}. Skipping PR stat analysis.")
71-
return StatsResult.Failure(IllegalStateException("No human reviewers found for PR#${pullRequest.number}."))
80+
return StatsResult.Failure(
81+
IllegalStateException(
82+
"No human reviewers found for PR#${pullRequest.number}. " +
83+
"Original reviewers: ${nonFilteredPrReviewerUsers.map { it.login }}.",
84+
),
85+
)
7286
}
7387

7488
// Builds a map of [Reviewer User -> Initial response time by either commenting, reviewing or approving PR]

src/test/kotlin/dev/hossain/githubstats/PullRequestStatsRepoTest.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ internal class PullRequestStatsRepoTest {
158158
}
159159

160160
@Test
161-
fun `stats - given merged with bot user as reviewer - provides no related metrics`() =
161+
fun `stats - given merged with bot user as reviewer - provides failure status`() =
162162
runTest {
163163
// Uses data from https://github.com/hossain-khan/github-stats/pull/27
164164
mockWebServer.enqueue(MockResponse().setBody(respond("pulls-githubstats-27.json")))
@@ -170,6 +170,19 @@ internal class PullRequestStatsRepoTest {
170170
assertThat(calculateStats).isInstanceOf(StatsResult.Failure::class.java)
171171
}
172172

173+
@Test
174+
fun `stats - given pr created by ignored bot user - provides failure status`() =
175+
runTest {
176+
// Uses data from https://github.com/jquery/jquery/pull/5046
177+
mockWebServer.enqueue(MockResponse().setBody(respond("pulls-jquery-5046-bot-user.json")))
178+
mockWebServer.enqueue(MockResponse().setBody(respond("timeline-jquery-5046.json")))
179+
mockWebServer.enqueue(MockResponse().setBody("[]")) // PR Review comments
180+
181+
val calculateStats = pullRequestStatsRepo.stats(REPO_OWNER, REPO_ID, 123, listOf("BotUser"))
182+
183+
assertThat(calculateStats).isInstanceOf(StatsResult.Failure::class.java)
184+
}
185+
173186
@Test
174187
fun `stats - given pr was draft - provides time taken after pr was ready for review`() =
175188
runTest {

0 commit comments

Comments
 (0)