Skip to content

Commit 0e033d5

Browse files
Merge pull request #67 from Tinder/universe_query_generate_hashes
Support Universe Query in Bazel-Diff command
2 parents b1d406b + 5985c16 commit 0e033d5

File tree

6 files changed

+37
-51
lines changed

6 files changed

+37
-51
lines changed

integration/WORKSPACE

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,3 @@ maven_install(
1919
"https://jcenter.bintray.com/",
2020
]
2121
)
22-
23-
http_archive(
24-
name = "io_bazel_rules_docker",
25-
sha256 = "1698624e878b0607052ae6131aa216d45ebb63871ec497f26c67455b34119c80",
26-
strip_prefix = "rules_docker-0.15.0",
27-
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.15.0/rules_docker-v0.15.0.tar.gz"],
28-
)
29-
30-
load(
31-
"@io_bazel_rules_docker//repositories:repositories.bzl",
32-
container_repositories = "repositories",
33-
)
34-
container_repositories()
35-
36-
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
37-
38-
container_deps()
39-
40-
load(
41-
"@io_bazel_rules_docker//container:container.bzl",
42-
"container_pull",
43-
)
44-
45-
container_pull(
46-
name = "openjdk_11_slim",
47-
digest = "sha256:28b59dc9a129c75349418e3b75508fd8eef3b33dd7d796079d1d19445d907776",
48-
registry = "index.docker.io",
49-
repository = "library/openjdk",
50-
)

integration/integration_test.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ modified_filepaths_output="$workspace_path/modified_filepaths.txt"
1010
starting_hashes_json="$output_dir/starting_hashes.json"
1111
final_hashes_json="$output_dir/final_hashes.json"
1212
impacted_targets_path="$output_dir/impacted_targets.txt"
13-
shared_flags="--config=verbose"
14-
command_options="--incompatible_restrict_string_escapes=false"
13+
shared_flags=""
1514

1615
export USE_BAZEL_VERSION=last_downstream_green
1716

@@ -22,13 +21,13 @@ containsElement () {
2221
return 1
2322
}
2423

25-
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path $starting_hashes_json -co $command_options
24+
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path $starting_hashes_json
2625

27-
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path -m $modified_filepaths_output $final_hashes_json -co $command_options
26+
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path -m $modified_filepaths_output $final_hashes_json
2827

2928
awk '{gsub(/:StringGenerator.java": \"\w+\"/,"modifiedhash");print}' $final_hashes_json > /dev/null
3029

31-
$bazel_path run :bazel-diff $shared_flags -- -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_targets_path -aq "attr('tags', 'manual', //...)" -co $command_options
30+
$bazel_path run :bazel-diff $shared_flags -- -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_targets_path -aq "attr('tags', 'manual', //...)"
3231

3332
IFS=$'\n' read -d '' -r -a impacted_targets < $impacted_targets_path
3433
target1="//test/java/com/integration:bazel-diff-integration-test-lib"

src/main/java/com/bazel_diff/BazelClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
interface BazelClient {
2222
List<BazelTarget> queryAllTargets() throws IOException;
23-
Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery) throws IOException;
23+
Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery, String universeQuery) throws IOException;
2424
Set<BazelSourceFileTarget> convertFilepathsToSourceTargets(Set<Path> filepaths) throws IOException, NoSuchAlgorithmException;
2525
Set<BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException;
2626
}
@@ -47,12 +47,12 @@ public List<BazelTarget> queryAllTargets() throws IOException {
4747
}
4848

4949
@Override
50-
public Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery) throws IOException {
50+
public Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery, String universeQuery) throws IOException {
5151
Set<String> impactedTargetNames = new HashSet<>();
5252
String targetQuery = impactedTargets.stream()
5353
.map(target -> String.format("'%s'", target))
5454
.collect(Collectors.joining(" + "));
55-
String query = query = String.format("rdeps(//... except '//external:all-targets', %s)", targetQuery);
55+
String query = query = String.format("rdeps(%s except '//external:all-targets', %s)", universeQuery, targetQuery);
5656
if (avoidQuery != null) {
5757
query = String.format("(%s) except (%s)", query, avoidQuery);
5858
}

src/main/java/com/bazel_diff/TargetHashingClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
interface TargetHashingClient {
1313
Map<String, String> hashAllBazelTargets(Set<Path> modifiedFilepaths, Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException;
1414
Map<String, String> hashAllBazelTargetsAndSourcefiles(Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException;
15-
Set<String> getImpactedTargets(Map<String, String> startHashes, Map<String, String> endHashes, String avoidQuery, Boolean hashAllTargets) throws IOException;
15+
Set<String> getImpactedTargets(Map<String, String> startHashes, Map<String, String> endHashes, String avoidQuery, String universeQuery, Boolean hashAllTargets) throws IOException;
1616
}
1717

1818
class TargetHashingClientImpl implements TargetHashingClient {
@@ -41,6 +41,7 @@ public Set<String> getImpactedTargets(
4141
Map<String, String> startHashes,
4242
Map<String, String> endHashes,
4343
String avoidQuery,
44+
String universeQuery,
4445
Boolean hashAllTargets)
4546
throws IOException {
4647
Set<String> impactedTargets = new HashSet<>();
@@ -53,7 +54,7 @@ public Set<String> getImpactedTargets(
5354
if (hashAllTargets != null && hashAllTargets && avoidQuery == null) {
5455
return impactedTargets;
5556
}
56-
return bazelClient.queryForImpactedTargets(impactedTargets, avoidQuery);
57+
return bazelClient.queryForImpactedTargets(impactedTargets, avoidQuery, universeQuery);
5758
}
5859

5960
private byte[] createDigestForTarget(

src/main/java/com/bazel_diff/main.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ public Integer call() {
153153
versionProvider = VersionProvider.class
154154
)
155155
class BazelDiff implements Callable<Integer> {
156+
@ArgGroup(exclusive = true)
157+
Exclusive exclusive;
158+
static class Exclusive {
159+
@Option(names = {"-a", "--all-sourcefiles"}, description = "Experimental: Hash all sourcefile targets (instead of relying on --modifiedFilepaths), Warning: Performance may degrade from reading all source files")
160+
Boolean hashAllSourcefiles;
161+
162+
@Option(names = {"-u", "--universeQuery"}, description = "The universe query to use when executing `rdeps()` queries, use this to limit the search scope of impacted targets i.e. ignore external targets and such")
163+
String universeRdepsQuery;
164+
}
156165

157166
@Option(names = {"-w", "--workspacePath"}, description = "Path to Bazel workspace directory.", scope = ScopeType.INHERIT, required = true)
158167
Path workspacePath;
@@ -169,9 +178,6 @@ class BazelDiff implements Callable<Integer> {
169178
@Option(names = {"-o", "--output"}, scope = ScopeType.LOCAL, description = "Filepath to write the impacted Bazel targets to, newline separated")
170179
File outputPath;
171180

172-
@Option(names = {"-a", "--all-sourcefiles"}, description = "Experimental: Hash all sourcefile targets (instead of relying on --modifiedFilepaths), Warning: Performance may degrade from reading all source files")
173-
Boolean hashAllSourcefiles;
174-
175181
@Option(names = {"-aq", "--avoid-query"}, scope = ScopeType.LOCAL, description = "A Bazel query string, any targets that pass this query will be removed from the returned set of targets")
176182
String avoidQuery;
177183

@@ -221,7 +227,16 @@ public Integer call() throws IOException {
221227
Map<String, String > gsonHash = new HashMap<>();
222228
Map<String, String> startingHashes = gson.fromJson(startingFileReader, gsonHash.getClass());
223229
Map<String, String> finalHashes = gson.fromJson(finalFileReader, gsonHash.getClass());
224-
Set<String> impactedTargets = hashingClient.getImpactedTargets(startingHashes, finalHashes, avoidQuery, hashAllSourcefiles);
230+
Boolean shouldHashAllSourceFiles = false;
231+
String universeQuery = "//...";
232+
if (exclusive != null) {
233+
if (exclusive.hashAllSourcefiles) {
234+
shouldHashAllSourceFiles = exclusive.hashAllSourcefiles;
235+
} else {
236+
universeQuery = exclusive.universeRdepsQuery;
237+
}
238+
}
239+
Set<String> impactedTargets = hashingClient.getImpactedTargets(startingHashes, finalHashes, avoidQuery, universeQuery, shouldHashAllSourceFiles);
225240
try {
226241
FileWriter myWriter = new FileWriter(outputPath);
227242
myWriter.write(impactedTargets.stream().collect(Collectors.joining(System.lineSeparator())));

test/java/com/bazel_diff/TargetHashingClientImplTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void hashAllBazelTargets_sourceTargets_modifiedSources_seedFilepaths() th
163163

164164
@Test
165165
public void getImpactedTargets() throws IOException {
166-
when(bazelClientMock.queryForImpactedTargets(anySet(), anyObject())).thenReturn(
166+
when(bazelClientMock.queryForImpactedTargets(anySet(), anyObject(), anyObject())).thenReturn(
167167
new HashSet<>(Arrays.asList("rule1", "rule3"))
168168
);
169169
TargetHashingClientImpl client = new TargetHashingClientImpl(bazelClientMock, filesClientMock);
@@ -174,7 +174,7 @@ public void getImpactedTargets() throws IOException {
174174
hash2.put("rule1", "differentrule1hash");
175175
hash2.put("rule2", "rule2hash");
176176
hash2.put("rule3", "rule3hash");
177-
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, null, false);
177+
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, null, null, false);
178178
Set<String> expectedSet = new HashSet<>();
179179
expectedSet.add("rule1");
180180
expectedSet.add("rule3");
@@ -183,7 +183,7 @@ public void getImpactedTargets() throws IOException {
183183

184184
@Test
185185
public void getImpactedTargets_withAvoidQuery() throws IOException {
186-
when(bazelClientMock.queryForImpactedTargets(anySet(), eq("some_query"))).thenReturn(
186+
when(bazelClientMock.queryForImpactedTargets(anySet(), eq("some_query"), eq("universe_query"))).thenReturn(
187187
new HashSet<>(Arrays.asList("rule1"))
188188
);
189189
TargetHashingClientImpl client = new TargetHashingClientImpl(bazelClientMock, filesClientMock);
@@ -194,15 +194,15 @@ public void getImpactedTargets_withAvoidQuery() throws IOException {
194194
hash2.put("rule1", "differentrule1hash");
195195
hash2.put("rule2", "rule2hash");
196196
hash2.put("rule3", "rule3hash");
197-
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, "some_query", false);
197+
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, "some_query", "universe_query", false);
198198
Set<String> expectedSet = new HashSet<>();
199199
expectedSet.add("rule1");
200200
assertEquals(expectedSet, impactedTargets);
201201
}
202202

203203
@Test
204204
public void getImpactedTargets_withHashAllTargets() throws IOException {
205-
when(bazelClientMock.queryForImpactedTargets(anySet(), anyObject())).thenReturn(
205+
when(bazelClientMock.queryForImpactedTargets(anySet(), anyObject(), anyObject())).thenReturn(
206206
new HashSet<>(Arrays.asList("rule1"))
207207
);
208208
TargetHashingClientImpl client = new TargetHashingClientImpl(bazelClientMock, filesClientMock);
@@ -213,7 +213,7 @@ public void getImpactedTargets_withHashAllTargets() throws IOException {
213213
hash2.put("rule1", "differentrule1hash");
214214
hash2.put("rule2", "rule2hash");
215215
hash2.put("rule3", "rule3hash");
216-
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, null, true);
216+
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, null, null, true);
217217
Set<String> expectedSet = new HashSet<>();
218218
expectedSet.add("rule1");
219219
expectedSet.add("rule3");
@@ -222,7 +222,7 @@ public void getImpactedTargets_withHashAllTargets() throws IOException {
222222

223223
@Test
224224
public void getImpactedTargets_withHashAllTargets_withAvoidQuery() throws IOException {
225-
when(bazelClientMock.queryForImpactedTargets(anySet(), eq("some_query"))).thenReturn(
225+
when(bazelClientMock.queryForImpactedTargets(anySet(), eq("some_query"), anyObject())).thenReturn(
226226
new HashSet<>(Arrays.asList("rule1"))
227227
);
228228
TargetHashingClientImpl client = new TargetHashingClientImpl(bazelClientMock, filesClientMock);
@@ -233,7 +233,7 @@ public void getImpactedTargets_withHashAllTargets_withAvoidQuery() throws IOExce
233233
hash2.put("rule1", "differentrule1hash");
234234
hash2.put("rule2", "rule2hash");
235235
hash2.put("rule3", "rule3hash");
236-
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, "some_query", true);
236+
Set<String> impactedTargets = client.getImpactedTargets(hash1, hash2, "some_query", null, true);
237237
Set<String> expectedSet = new HashSet<>();
238238
expectedSet.add("rule1");
239239
assertEquals(expectedSet, impactedTargets);

0 commit comments

Comments
 (0)