Skip to content

Commit 3f830b8

Browse files
ssbrcopybara-github
authored andcommitted
Add the new Google TODO format to todo_replace.
This has been silently leaking googlers' usernames for years. :( GWSQ_IGNORE: [email protected] PiperOrigin-RevId: 848303274 Change-Id: I1440803927eddf4218d9ec7137e131b4067f7d19
1 parent 53eddaf commit 3f830b8

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

java/com/google/copybara/transform/TodoReplace.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ public TodoReplace(
9595
}
9696

9797
private Pattern createPattern(ImmutableList<String> todoTags) {
98-
return Pattern.compile("((?:"
99-
+ Joiner.on("|").join(todoTags.stream().map(Pattern::quote).collect(Collectors.toList()))
100-
+ ") ?)\\((.*?)\\)");
98+
return Pattern.compile(
99+
"((?:"
100+
+ Joiner.on("|")
101+
.join(todoTags.stream().map(Pattern::quote).collect(Collectors.toList()))
102+
+ ") ?)(?:\\((.*?)\\):|: (.*?) -)");
101103
}
102104

103105
@Override
@@ -125,16 +127,25 @@ private Set<FileState> run(Iterable<FileState> files, Console console)
125127
StringBuffer sb = new StringBuffer();
126128
boolean modified = false;
127129
while (matcher.find()) {
128-
if (matcher.group(2).trim().isEmpty()) {
129-
matcher.appendReplacement(sb, matcher.group(0));
130+
boolean newTodoFormat = matcher.group(3) != null;
131+
String matchedUsers = newTodoFormat ? matcher.group(3) : matcher.group(2);
132+
if (matchedUsers.trim().isEmpty()) {
133+
matcher.appendReplacement(sb, matcher.group(0) + ":");
130134
continue;
131135
}
132-
List<String> users = Splitter.on(",").splitToList(matcher.group(2));
136+
List<String> users = Splitter.on(",").splitToList(matchedUsers);
133137
List<String> mappedUsers = mapUsers(users, matcher.group(0), file.getPath(), console);
134138
modified |= !users.equals(mappedUsers);
135139
String result = matcher.group(1);
136140
if (!mappedUsers.isEmpty()) {
137-
result += "(" + Joiner.on(",").join(mappedUsers) + ")";
141+
String todoContent = Joiner.on(",").join(mappedUsers);
142+
if (newTodoFormat) {
143+
result += ": " + todoContent + " -";
144+
} else {
145+
result += "(" + todoContent + "):";
146+
}
147+
} else {
148+
result += ":";
138149
}
139150
matcher.appendReplacement(sb, Matcher.quoteReplacement(result));
140151
}

javatests/com/google/copybara/transform/TodoReplaceTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ public void testMapping() throws Exception {
116116
.containsNoMoreFiles();
117117
}
118118

119+
@Test
120+
public void testNewTodoMapping() throws Exception {
121+
TodoReplace replace = todoReplace("mapping = { 'aaa': 'foo', 'bbb' : 'bar'}");
122+
write("one", "" + "aaa\n" + "// TODO: aaa, bbb,other - Example\n");
123+
write("two", "// TODO: aaa - Other Example\n");
124+
run(replace);
125+
126+
assertThatPath(checkoutDir)
127+
.containsFile("one", "" + "aaa\n" + "// TODO: foo, bar,other - Example\n")
128+
.containsFile("two", "// TODO: foo - Other Example\n")
129+
.containsNoMoreFiles();
130+
131+
run(replace.reverse());
132+
133+
assertThatPath(checkoutDir)
134+
.containsFile("one", "" + "aaa\n" + "// TODO: aaa, bbb,other - Example\n")
135+
.containsFile("two", "// TODO: aaa - Other Example\n")
136+
.containsNoMoreFiles();
137+
}
138+
119139
@Test
120140
public void testMultipleNotFound() throws Exception {
121141
TodoReplace replace = todoReplace("mapping = { 'test': 'foo'}");
@@ -146,7 +166,7 @@ public void testMapOrFail() throws Exception {
146166
ValidationException notFound = assertThrows(ValidationException.class, () -> run(replace));
147167
assertThat(notFound)
148168
.hasMessageThat()
149-
.contains("Cannot find a mapping 'bbb' in 'TODO(bbb)' (/one.txt)");
169+
.contains("Cannot find a mapping 'bbb' in 'TODO(bbb):' (/one.txt)");
150170
// Does not conform the pattern for users
151171
write("one.txt", "// TODO(aaa foo/1234): Example\n");
152172

@@ -240,6 +260,11 @@ public void testScrub() throws Exception {
240260
.containsFile("one.txt", "// TODO: Example\n")
241261
.containsNoMoreFiles();
242262

263+
// and the new format
264+
write("one.txt", "// TODO: aaa, bbb - Example\n");
265+
run(replace);
266+
assertThatPath(checkoutDir).containsFile("one.txt", "// TODO: Example\n").containsNoMoreFiles();
267+
243268
// Does not conform the pattern, will be scrubbed anyway
244269
write("one.txt", "// TODO(aaa, bbb foo/1234): Example\n");
245270
run(replace);

0 commit comments

Comments
 (0)