-
Notifications
You must be signed in to change notification settings - Fork 0
/
copybara.bzl
329 lines (293 loc) · 10.2 KB
/
copybara.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
CopybaraInfo = provider(fields = ["skys", "additional", "script"])
def _copybara_impl(ctx):
# pull in .sky file with the copybara workflow
workflow_defs_path = ctx.attr.workflow_defs.files.to_list()[0]
# get the copybara provider
copybara = ctx.attr._copybara[DefaultInfo]
script = str("")
workflow_defs = []
additional_files = []
if len(ctx.attr.deps) > 0:
for dep in ctx.attr.deps:
if ctx.attr.run_deps:
script = script + dep[CopybaraInfo].script
additional_files.extend(dep[CopybaraInfo].additional)
workflow_defs.extend(dep[CopybaraInfo].skys)
for tgt in ctx.attr.additional_files:
additional_files.extend(tgt.files.to_list())
for workflow in ctx.attr.workflows:
command = []
# the copybara jar
command.append(str(copybara.files_to_run.executable.short_path))
command.append("migrate")
# the wrapper will copy the workflow file to this constant string (idk, it's not happy w/ a symlink or a nested file)
command.append("copy.bara.sky")
# drop the workflow name into the command
command.append(workflow)
# if the copybara callsite specifies a folder to do things in
if ctx.attr.destination_dir != "":
command.append("--folder-dir")
command.append("${BUILD_WORKSPACE_DIRECTORY}/" + str(ctx.attr.destination_dir))
# append additional command line args
if ctx.attr.cli_args != "":
command.append(str(ctx.attr.cli_args))
# form the actual cli command
command = " ".join(command)
# make the templates
script = script + """
cp {workflow_defs} copy.bara.sky
{command}
""".format(
workflow_defs = str(workflow_defs_path.path),
command = command,
)
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = "set -euo pipefail" + "\n" + "set -x" + "\n" + script,
)
copybara_info = CopybaraInfo(
script = script,
skys = workflow_defs + [workflow_defs_path],
additional = additional_files,
)
return [
DefaultInfo(
executable = ctx.outputs.executable,
runfiles = ctx.runfiles(
files = copybara.files.to_list() +
copybara_info.skys +
copybara_info.additional +
copybara.default_runfiles.files.to_list(),
),
),
copybara_info,
]
copybara = rule(
implementation = _copybara_impl,
attrs = {
"workflow_defs": attr.label(
allow_single_file = True,
),
"workflows": attr.string_list(),
"destination_dir": attr.string(
mandatory = False,
default = "",
),
"additional_files": attr.label_list(
allow_files = True,
),
"cli_args": attr.string(
mandatory = False,
default = "",
),
"deps": attr.label_list(
mandatory = False,
providers = [CopybaraInfo],
),
"run_deps": attr.bool(
default = True,
),
"_copybara": attr.label(
allow_files = True,
default = "@com_github_google_copybara//java/com/google/copybara",
),
},
executable = True,
)
def _str(instr):
return str(instr).replace('\\"', "🛐").replace('"', "").replace("🛐", '"')
def _copybara_move_commits_impl(ctx):
push = ctx.actions.declare_file(ctx.attr.name + "-push.bara.sky")
ctx.actions.expand_template(
template = ctx.file._tmpl,
output = push,
substitutions = {
"{sotRepo}": ctx.attr.sot_repo,
"{sotBranch}": ctx.attr.sot_branch,
"{destinationRepo}": ctx.attr.dest_repo,
"{destinationBranch}": ctx.attr.dest_branch,
"{committer}": ctx.attr.committer,
"{push_files}": ctx.attr.push_files,
"{destination_files}": ctx.attr.destination_files,
"{mode}": ctx.attr.mode,
"{push_transformations}": _str(ctx.attr.push_transformations),
"{pr_transformations}": _str(ctx.attr.pr_transformations),
},
)
copybara = ctx.attr._copybara[DefaultInfo]
command = []
command.append(copybara.files_to_run.executable.short_path)
# append additional command line args
if ctx.attr.cli_args != "":
command.append(str(ctx.attr.cli_args))
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = """
#!/bin/sh
rm -f copy.bara.sky
cp {tmpl} copy.bara.sky
{command} migrate copy.bara.sky push
""".format(
tmpl = str(push.short_path),
command = " ".join(command),
),
)
return DefaultInfo(
executable = ctx.outputs.executable,
runfiles = ctx.runfiles(files = [push] + copybara.files.to_list() + copybara.default_runfiles.files.to_list()),
)
default_push_transformations = [
'metadata.restore_author("ORIGINAL_AUTHOR", search_all_changes = True)',
'metadata.expose_label("COPYBARA_INTEGRATE_REVIEW")',
]
copybara_move_commits = rule(
implementation = _copybara_move_commits_impl,
attrs = {
"sot_repo": attr.string(
mandatory = True,
),
"sot_branch": attr.string(
mandatory = True,
),
"dest_repo": attr.string(
mandatory = True,
),
"dest_branch": attr.string(
mandatory = True,
),
"committer": attr.string(),
"push_files": attr.string(), # this is a string since the workflow can run on foreign repos
"destination_files": attr.string(),
"mode": attr.string(
default = "ITERATIVE",
),
"push_transformations": attr.string_list(
default = default_push_transformations,
),
"pr_transformations": attr.string_list(
default = [],
),
"cli_args": attr.string(
mandatory = False,
),
"_copybara": attr.label(
allow_files = True,
default = "@com_github_google_copybara//java/com/google/copybara",
),
"_tmpl": attr.label(
allow_single_file = True,
default = "//:push.bara.sky.tmpl",
),
},
executable = True,
)
def _copybara_move_github_pr_impl(ctx):
push = ctx.actions.declare_file("pr.bara.sky")
ctx.actions.expand_template(
template = ctx.file._tmpl,
output = push,
substitutions = {
"{sotRepo}": ctx.attr.sot_repo,
"{sotBranch}": ctx.attr.sot_branch,
"{destinationRepo}": ctx.attr.dest_repo,
"{destinationBranch}": ctx.attr.dest_branch,
"{committer}": ctx.attr.committer,
"{push_files}": ctx.attr.push_files,
"{destination_files}": ctx.attr.destination_files,
"{mode}": ctx.attr.mode,
"{pr_transformations}": str(ctx.attr.pr_transformations).replace('\\"', "🛐").replace('"', "").replace("🛐", '"').replace("{destinationRepo}", ctx.attr.dest_repo),
},
)
copybara = ctx.attr._copybara[DefaultInfo]
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = """
#!/bin/sh
rm -f copy.bara.sky
cp {tmpl} copy.bara.sky
{command} migrate copy.bara.sky pr $@
""".format(
tmpl = str(push.short_path),
command = str(copybara.files_to_run.executable.short_path),
),
)
return DefaultInfo(
executable = ctx.outputs.executable,
runfiles = ctx.runfiles(files = [push] + copybara.files.to_list() + copybara.default_runfiles.files.to_list()),
)
default_move_pr_transformations = [
'metadata.save_author("ORIGINAL_AUTHOR")',
'metadata.expose_label("GITHUB_PR_NUMBER", new_name = "Closes", separator = "{destinationRepo}".replace("[email protected]", " ").replace(".git", "#"))',
]
# move from dest to sot
copybara_move_github_pr = rule(
implementation = _copybara_move_github_pr_impl,
attrs = {
"sot_repo": attr.string(
mandatory = True,
),
"sot_branch": attr.string(
mandatory = True,
),
"dest_repo": attr.string(
mandatory = True,
),
"dest_branch": attr.string(
mandatory = True,
),
"committer": attr.string(),
"push_files": attr.string(), # this is a string since the workflow can run on foreign repos
"destination_files": attr.string(),
"mode": attr.string(
default = "CHANGE_REQUEST",
),
"pr_transformations": attr.string_list(
default = default_move_pr_transformations,
),
"cli_args": attr.string(
mandatory = False,
),
"_copybara": attr.label(
allow_files = True,
default = "@com_github_google_copybara//java/com/google/copybara",
),
"_tmpl": attr.label(
allow_single_file = True,
default = ":pr.bara.sky.tmpl",
),
},
executable = True,
)
def _store_copybara_config(repository_ctx):
version = repository_ctx.attr.copybara_version
version_sha = repository_ctx.attr.copybara_sha256
config_file_content = "\n".join([
"COPYBARA_VERSION='" + version + "'",
"COPYBARA_SHA256='" + version_sha + "'",
])
repository_ctx.file("config.bzl", config_file_content)
repository_ctx.file("BUILD")
_config_repository = repository_rule(
implementation = _store_copybara_config,
attrs = {
"copybara_version": attr.string(
mandatory = True,
),
"copybara_sha256": attr.string(
mandatory = True,
),
},
)
def copybara_config(
copybara_version,
copybara_sha256):
_config_repository(
name = "com_github_rules_copybara_config",
copybara_version = copybara_version,
copybara_sha256 = copybara_sha256,
)