Skip to content
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,17 @@ To validate all `diff`, `cmp`, etc. actions by default, set `common --@diff.bzl/
### Support automatic patch workflows for CI

This ruleset outputs patches into a distinct `diff_bzl__patch` output group making it easier for patches to be collected and then applied using automation. See the [build & patch](./examples/build-and-patch.sh) example script.

Patches can be placed in separately identifiable output groups via the `source_patch_extra_output_groups` attr. For example, you may want all patches in the "autopatch" group to be automatically applied on a CI build failure, while patches from snapshot tests ought to be reviewed by a human first.

```starlark
diff(
name = "foo",
srcs = [
"foo.pb.go",
":foo_generated",
],
validate = 1,
source_patch_extra_output_groups = ["autopatch"],
)
```
15 changes: 14 additions & 1 deletion diff/private/diff.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,31 @@ def _diff_rule_impl(ctx):
ERROR: diff command exited with non-zero status.
{}""".format(patch_msg)))

source_patches_depset = depset(source_patch_outputs)
source_patch_extra_output_groups = {group: source_patches_depset for group in ctx.attr.source_patch_extra_output_groups}

return [
DefaultInfo(files = depset(outputs)),
OutputGroupInfo(
_validation = depset(validation_outputs),
# By reading the Build Events, a Bazel wrapper can identify this diff output group and apply the patch.
diff_bzl__patch = depset(source_patch_outputs),
diff_bzl__patch = source_patches_depset,
**source_patch_extra_output_groups
),
]

diff_rule = rule(
implementation = _diff_rule_impl,
attrs = {
"source_patch_extra_output_groups": attr.string_list(
doc = """\
Additional output groups to add non-empty source file patches to, in addition to the default
diff_bzl__patch group. Groups can be used to identify sets of patches, for example, patches
that should be automatically applied after a failing CI build might be placed in an "autopatch"
group.
""",
default = [],
),
"args": attr.string_list(
doc = """\
Additional arguments to pass to the diff command.
Expand Down
10 changes: 10 additions & 0 deletions diff/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ diff(
validate = 1,
)

diff(
name = "case_source_patch_extra_output_groups",
srcs = [
"case_extra_output_group.a",
"case_extra_output_group.b",
],
source_patch_extra_output_groups = ["autopatch"],
)

genrule(
name = "case_diff_genrule",
srcs = [],
Expand Down Expand Up @@ -536,6 +545,7 @@ build_test(
":case_to_file_directory_assert_patch",
":case_to_file_directory_newfile",
":case_to_file_directory_newfile_assert_patch",
":case_source_patch_extra_output_groups",
":case_diff_genrule",
":case_cmp_genrule",
":case_cmp_same_binary",
Expand Down
1 change: 1 addition & 0 deletions diff/tests/case_extra_output_group.a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
moo
1 change: 1 addition & 0 deletions diff/tests/case_extra_output_group.b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cow
Loading