Skip to content

Commit

Permalink
Compare securityLevel to null for checking if issue is private (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
violine1101 authored Jul 19, 2023
1 parent ffd02af commit c180b4f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 118 deletions.
3 changes: 0 additions & 3 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,6 @@ arisa:
keepPrivate:
tag: MEQS_KEEP_PRIVATE
message: panel-unmark-private-issue
privateLevels:
- '10318' # most projects
- '10502' # just MCL

privateDuplicate:
resolutions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ object Arisa : ConfigSpec() {
description = "The key of the message that is posted when this module succeeds."
)
val tag by optional<String?>(null)
val privateLevels by required<Set<String>>(
description = "All levels used to denote private security level"
)
}

object PrivateDuplicate : ModuleConfigSpec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import java.time.Instant

class KeepPrivateModule(
private val keepPrivateTag: String?,
private val message: String,
private val privateLevels: Set<String>
private val message: String
) : Module {
override fun invoke(issue: Issue, lastRun: Instant): Either<ModuleError, ModuleResponse> = with(issue) {
Either.fx {
assertNotNull(keepPrivateTag).bind()
assertContainsKeepPrivateTag(comments).bind()
assertIsPublic(securityLevel).bind()
assertNull(securityLevel).bind()

setPrivate()

Expand Down Expand Up @@ -48,17 +47,10 @@ class KeepPrivateModule(
(comment.body?.contains(keepPrivateTag!!) ?: false)

private fun isSecurityChangeToPublic(item: ChangeLogItem) =
item.field == "security" && privateLevels.contains(item.changedFromString)
item.field == "security" && item.changedFrom != null && item.changedTo == null

private fun assertContainsKeepPrivateTag(comments: List<Comment>) = when {
comments.any(::isKeepPrivateTag) -> Unit.right()
else -> OperationNotNeededModuleResponse.left()
}

private fun assertIsPublic(securityLevel: String?) =
if (privateLevels.contains(securityLevel)) {
OperationNotNeededModuleResponse.left()
} else {
Unit.right()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LanguageModule(
override fun invoke(issue: Issue, lastRun: Instant): Either<ModuleError, ModuleResponse> = with(issue) {
Either.fx {
assertAfter(created, lastRun).bind()
assertIsPublic(securityLevel, project.privateSecurity).bind()
assertNull(securityLevel).bind()

val combinedText = combineSummaryAndDescription(summary ?: "", description ?: "")
assertExceedLengthThreshold(combinedText).bind()
Expand Down Expand Up @@ -73,13 +73,6 @@ class LanguageModule(
else -> Unit.right()
}

private fun assertIsPublic(securityLevel: String?, privateLevel: String) =
if (securityLevel == privateLevel) {
OperationNotNeededModuleResponse.left()
} else {
Unit.right()
}

private fun assertLanguageIsNotAllowed(allowedLanguages: List<String>, language: String) = when {
allowedLanguages.any { language == it } -> OperationNotNeededModuleResponse.left()
else -> Unit.right()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package io.github.mojira.arisa.modules

import arrow.core.Either
import arrow.core.extensions.fx
import arrow.core.left
import arrow.core.right
import io.github.mojira.arisa.domain.Comment
import io.github.mojira.arisa.domain.Issue
import io.github.mojira.arisa.domain.Link
Expand All @@ -15,13 +13,14 @@ class PrivateDuplicateModule(
override fun invoke(issue: Issue, lastRun: Instant): Either<ModuleError, ModuleResponse> = with(issue) {
Either.fx {
assertNotNull(keepPrivateTag).bind()
assertIsPublic(securityLevel, project.privateSecurity).bind()
assertNull(securityLevel).bind()

val duplicatedReports = links
.filter(::isDuplicatesLink)
.map { it.issue.getFullIssue().toFailedModuleEither().bind() }
assertGreaterThan(duplicatedReports.size, 0).bind()
duplicatedReports.forEach {
assertParentPrivate(it.securityLevel, it.project.privateSecurity).bind()
assertNotNull(it.securityLevel).bind()
setPrivate()
if (parentHasKeepPrivateTag(it)) {
addRawRestrictedComment(keepPrivateTag!!, "staff")
Expand All @@ -37,18 +36,4 @@ class PrivateDuplicateModule(
private fun parentHasKeepPrivateTag(parent: Issue): Boolean = parent.comments.any(::isKeepPrivateTag)

private fun isDuplicatesLink(link: Link): Boolean = link.type == "Duplicate" && link.outwards

private fun assertIsPublic(securityLevel: String?, privateLevel: String) =
if (securityLevel == privateLevel) {
OperationNotNeededModuleResponse.left()
} else {
Unit.right()
}

private fun assertParentPrivate(securityLevel: String?, privateLevel: String) =
if (securityLevel == privateLevel) {
Unit.right()
} else {
OperationNotNeededModuleResponse.left()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ class InstantModuleRegistry(config: Config) : ModuleRegistry(config) {
Arisa.Modules.KeepPrivate,
KeepPrivateModule(
config[Arisa.Modules.KeepPrivate.tag],
config[Arisa.Modules.KeepPrivate.message],
config[Arisa.Modules.KeepPrivate.privateLevels]
config[Arisa.Modules.KeepPrivate.message]
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,22 @@ import io.kotest.matchers.shouldBe
private val REMOVE_SECURITY = mockChangeLogItem(
created = RIGHT_NOW.minusSeconds(10),
field = "security",
changedFromString = PRIVATE_SECURITY_LEVEL,
getAuthorGroups = { listOf("user") }
)

private const val PUBLIC_SECURITY_LEVEL = "public"

private val USE_PUBLIC_SECURITY = mockChangeLogItem(
created = RIGHT_NOW.minusSeconds(10),
field = "security",
changedFromString = PRIVATE_SECURITY_LEVEL,
changedToString = PUBLIC_SECURITY_LEVEL,
changedFrom = PRIVATE_SECURITY_LEVEL,
changedTo = null,
getAuthorGroups = { listOf("user") }
)

private val REMOVE_SECURITY_STAFF = mockChangeLogItem(
created = RIGHT_NOW.minusSeconds(10),
field = "security",
changedFromString = PRIVATE_SECURITY_LEVEL,
changedFrom = PRIVATE_SECURITY_LEVEL,
changedTo = null,
getAuthorGroups = { listOf("staff") }
)

class KeepPrivateModuleTest : StringSpec({
"should return OperationNotNeededModuleResponse when keep private tag is null" {
val module = KeepPrivateModule(null, "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule(null, "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
visibilityType = "group",
Expand All @@ -54,7 +46,7 @@ class KeepPrivateModuleTest : StringSpec({
}

"should return OperationNotNeededModuleResponse when comments are empty" {
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val issue = mockIssue(
changeLog = listOf(REMOVE_SECURITY)
)
Expand All @@ -65,7 +57,7 @@ class KeepPrivateModuleTest : StringSpec({
}

"should return OperationNotNeededModuleResponse when no comment contains private tag" {
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "Hello world!",
visibilityType = "group",
Expand All @@ -82,7 +74,7 @@ class KeepPrivateModuleTest : StringSpec({
}

"should return OperationNotNeededModuleResponse when the comment isn't restricted to staff group" {
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE"
)
Expand All @@ -97,7 +89,7 @@ class KeepPrivateModuleTest : StringSpec({
}

"should return OperationNotNeededModuleResponse when security level is set to private" {
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
visibilityType = "group",
Expand All @@ -117,14 +109,15 @@ class KeepPrivateModuleTest : StringSpec({
var didSetToPrivate = false
var didComment = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
created = RIGHT_NOW.minusSeconds(20),
visibilityType = "group",
visibilityValue = "staff"
)
val issue = mockIssue(
securityLevel = null,
comments = listOf(comment),
changeLog = listOf(REMOVE_SECURITY_STAFF),
setPrivate = { didSetToPrivate = true; Unit.right() },
Expand All @@ -142,14 +135,15 @@ class KeepPrivateModuleTest : StringSpec({
var didSetToPrivate = false
var didComment = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
created = RIGHT_NOW.minusSeconds(20),
visibilityType = "group",
visibilityValue = "staff"
)
val issue = mockIssue(
securityLevel = null,
comments = listOf(comment),
changeLog = listOf(REMOVE_SECURITY),
setPrivate = { didSetToPrivate = true; Unit.right() },
Expand All @@ -166,7 +160,7 @@ class KeepPrivateModuleTest : StringSpec({
"should not set to private if bug report is already private but with different security level" {
var didSetToPrivate = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL, "special-private"))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
created = RIGHT_NOW.minusSeconds(20),
Expand All @@ -189,54 +183,17 @@ class KeepPrivateModuleTest : StringSpec({
var didSetToPrivate = false
var didComment = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
created = RIGHT_NOW.minusSeconds(20),
visibilityType = "group",
visibilityValue = "staff"
)
val issue = mockIssue(
securityLevel = PUBLIC_SECURITY_LEVEL,
comments = listOf(comment),
changeLog = listOf(USE_PUBLIC_SECURITY),
setPrivate = { didSetToPrivate = true; Unit.right() },
addComment = { didComment = true; Unit.right() }
)

val result = module(issue, RIGHT_NOW)

result.shouldBeRight(ModuleResponse)
didSetToPrivate shouldBe true
didComment shouldBe true
}

"should both set to private and comment when security level is transitively not private" {
var didSetToPrivate = false
var didComment = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
created = RIGHT_NOW.minusSeconds(20),
visibilityType = "group",
visibilityValue = "staff"
)

val otherPublicLevel = "other-$PUBLIC_SECURITY_LEVEL"
val otherSecurityLevelChange = mockChangeLogItem(
created = RIGHT_NOW.minusSeconds(5),
field = "security",
changedFromString = PUBLIC_SECURITY_LEVEL,
changedToString = otherPublicLevel,
getAuthorGroups = { listOf("user") }
)

val issue = mockIssue(
securityLevel = otherPublicLevel,
securityLevel = null,
comments = listOf(comment),
// private -> public -> other-public
changeLog = listOf(USE_PUBLIC_SECURITY, otherSecurityLevelChange),
changeLog = listOf(REMOVE_SECURITY),
setPrivate = { didSetToPrivate = true; Unit.right() },
addComment = { didComment = true; Unit.right() }
)
Expand All @@ -252,7 +209,7 @@ class KeepPrivateModuleTest : StringSpec({
var didSetToPrivate = false
var didComment = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
visibilityType = "group",
Expand All @@ -275,7 +232,7 @@ class KeepPrivateModuleTest : StringSpec({
var didSetToPrivate = false
var didComment = false

val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message", setOf(PRIVATE_SECURITY_LEVEL))
val module = KeepPrivateModule("MEQS_KEEP_PRIVATE", "message")
val comment = mockComment(
body = "MEQS_KEEP_PRIVATE",
created = RIGHT_NOW.minusSeconds(2),
Expand Down
Loading

0 comments on commit c180b4f

Please sign in to comment.