Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WX-1137 Replace 4-byte unicode chars in PAPI event descriptions #7166

Merged
merged 9 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ trait PipelinesUtilityConversions {
Option(event.getDescription) collect { case d if d.startsWith(s"$k pulling") => "Pulling" + d.substring(s"$k pulling".length)}
} headOption

// See WX-1137. ContainerStoppedEvent descriptions contain the stderr, which may include 4-byte unicode
// characters (typically emoji). Some databases have trouble storing these; replace them with the standard
// "unknown character" unicode symbol.
val name = Option(event.getContainerStopped) match {
case Some(_) => cleanUtf8mb4(event.getDescription)
case _ => event.getDescription
}

ExecutionEvent(
name = event.getDescription,
name = name,
offsetDateTime = OffsetDateTime.parse(event.getTimestamp),
grouping = groupingFromAction.orElse(groupingFromPull)
)
Expand Down Expand Up @@ -91,4 +99,10 @@ object PipelinesUtilityConversions {
}
}
}

lazy val utf8mb4Regex = "[\\x{10000}-\\x{FFFFF}]"
lazy val utf8mb3Replacement = "\uFFFD" // This is the standard char for replacing invalid/unknown unicode chars
def cleanUtf8mb4(in: String): String = {
in.replaceAll(utf8mb4Regex, utf8mb3Replacement)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cromwell.backend.google.pipelines.v2beta

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class PipelinesUtilityConversionsSpec extends AnyFlatSpec with Matchers {
behavior of "PipelinesUtilityConversions"

it should "not modify strings that contain only ascii characters" in {
val input = "hi there!?"
PipelinesUtilityConversions.cleanUtf8mb4(input) shouldBe input
}

it should "not modify strings with 3-byte unicode characters" in {
val input = "Here is my non-ascii character: \u1234 Do you like it?"
PipelinesUtilityConversions.cleanUtf8mb4(input) shouldBe input
}

it should "replace 4-byte unicode characters" in {
val cry = new String(Character.toChars(Integer.parseInt("1F62D", 16)))
val barf = new String(Character.toChars(Integer.parseInt("1F92E", 16)))
val input = s"When I try to put an emoji in the database it $barf and then I $cry"
val cleaned = "When I try to put an emoji in the database it \uFFFD and then I \uFFFD"
PipelinesUtilityConversions.cleanUtf8mb4(input) shouldBe cleaned
}
}