fn(arguments$0, request$1) =>
let project$2 = @definy.mcp-arg-string(arguments: $0, key: "project", default: "");
let branch$3 = @definy.mcp-arg-string(arguments: $0, key: "branch", default: "main");
let part$4 = @definy.mcp-arg-string(arguments: $0, key: "part", default: "");
let to$5 = @definy.mcp-arg-string(arguments: $0, key: "to", default: "");
let dot$6 = @core.string-index-of(text: $5, target: ".", start: 0);
let to_module$7 = @core.string-slice(value: $5, start: 0, end: @std.max(a: 0, b: $6));
let to_name$8 = @core.string-slice(value: $5, start: @core.plus(left: $6, right: 1), end: @core.string-length(value: $5));
match @core.or(
left: @core.less-than(left: $6, right: 1),
right: @core.or(
left: @core.equal(left: $8, right: ""),
right: @core.less-than-or-equal(left: 0, right: @core.string-index-of(text: $8, target: ".", start: 0))
)
) {
true =>
@definy.mcp-text-error(text: "to must be in the form 'module.name'"),
false =>
@definy.mcp-with-account(
then: fn(account$9) =>
@definy.with-branch-part-contents(
project: $2,
branch: $3,
part: $4,
then: fn(commit$10, target$11, parts$12) =>
match @core.list-find-last-by-field(list: $12, field: "name", value: $5) {
#some(existing$13) =>
@definy.mcp-text-error(text: @core.string-concat(left: "A part with the name already exists: ", right: $5)),
#none(_) =>
let new_id$14 = match @core.list-get(list: @core.module-part-ids(module_name: $7, part_names: [$8]).part_ids, index: 0) {
#some(id$15) =>
$15,
#none(_) =>
""
};
let referrers$16 = @definy.part-referrers(parts: $12, id: $11.id);
match @definy.rename-uploads(target: $11, referrers: $16, to_module: $7, to_name: $8, new_id: $14) {
#error(message$17) =>
@definy.mcp-text-error(text: $17),
#ok(uploads$18) =>
@definy.store-write-contents(
items: @core.list-fold(
T: @definy.mcp-part-upload,
A: List<@definy.content-item>,
reducer: fn(acc$19, item$20) =>
match $20.content {
#some(content$21) =>
@core.list-append(list: $19, item: $21),
#none(_) =>
$19
},
init: [],
xs: $18
),
then: fn(stored$22) =>
@definy.read-snapshot-content(
event_hash: $10.event_hash,
then: fn(commit$23) =>
match $23 {
#none(_) =>
@definy.mcp-text-error(text: "The latest snapshot of the branch could not be read"),
#some(latest$24) =>
@definy.mcp-submit-content(
account: $9,
after_time_ms: $10.commit.time_ms,
content: #project_commit<@core.event-content>({
project_name: $24.project_name,
branch: $24.branch,
project_description: $24.project_description,
message: @definy.mcp-arg-string(
arguments: $0,
key: "message",
default: @core.string-join(items: ["Rename ", $4, " to ", $5], separator: "")
),
parent_commit_hash: #some($10.event_hash),
merged_commit_hash: #none,
modules: @definy.apply-part-changes(modules: $24.modules, uploads: $18)
}),
then: fn(accepted$25, text$26) =>
match $25 {
true =>
@definy.mcp-text(
text: @core.string-join(
items: @core.list-concat(
left: [
@core.string-join(
items: [
"Renamed ",
$4,
" to ",
$5,
" in branch ",
$3,
" of project ",
$2,
" and changed the references in ",
@std.number-to-string(value: @core.list-length(value: $16)),
" parts"
],
separator: ""
)
],
right: @core.list-concat(
left: @core.list-map(
T: @definy.snapshot-check-part,
U: String,
f: fn(item$27) =>
@core.string-concat(left: " ", right: $27.name),
xs: $16
),
right: [$26]
)
),
separator: "\n"
)
),
false =>
@definy.mcp-text-error(text: $26)
}
)
}
)
)
}
}
)
)
}