Skip to content

Commit

Permalink
Protect fenced code-blocks when running update_pr_body.py (#4362)
Browse files Browse the repository at this point in the history
### What
- Resolves: #3972

Previously, code-blocks like this would cause the jinja template parsing
to fail:
```
    std::vector<std::vector<std::array<float, 3>>> points = {
        {{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}},
        {{1.f, 0.f, 0.f}, {1.f, 0.f, 1.f}},
        {{1.f, 1.f, 0.f}, {1.f, 1.f, 1.f}},
        {{0.f, 1.f, 0.f}, {0.f, 1.f, 1.f}},
    };

    rec.log("segments", rerun::LineStrips3D(points));
```

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [app.rerun.io](https://app.rerun.io/pr/4362) (if
applicable)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4362)
- [Docs
preview](https://rerun.io/preview/52d837c54819dffb64afec7382dd3ae4c60556da/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/52d837c54819dffb64afec7382dd3ae4c60556da/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
jleibs authored and teh-cmc committed Nov 30, 2023
1 parent 4115a09 commit 6b3f61a
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion scripts/ci/update_pr_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,64 @@
"- [Examples preview](https://rerun.io/preview/{{ pr.commit }}/examples) <!--EXAMPLES-PREVIEW-->"
)

# Need to protect code-blocks in the PR template.
# See https://github.com/rerun-io/rerun/issues/3972
#
# Could not get this to work with jinja-markdown extension so replicating the functionality from:
# https://github.com/digital-land/design-system/commit/14e678ccb3e1e62da18072ccf035f3d0e7467f3c

CODE_BLOCK_PLACEHOLDER = "{CODE BLOCK PLACEHOLDER}"


def index_code_block(lines: list[str]) -> tuple[bool, int, int]:
if "```" in lines:
opening_line = lines.index("```")
closing_line = lines.index("```", opening_line + 1)
return True, opening_line, closing_line
return False, 0, 0


# extracts code blocks from lines of text
# also replaces code_blocks with a placeholder string
def extract_code_blocks(lines: list[str]) -> list[list[str]]:
code_blocks = []
has_code_blocks = True
while has_code_blocks:
if "```" in lines:
block, op, cl = index_code_block(lines)
if block:
code_blocks.append(lines[op : cl + 1])
del lines[op + 1 : cl + 1]
lines[op] = CODE_BLOCK_PLACEHOLDER
else:
has_code_blocks = False
else:
has_code_blocks = False
return code_blocks


def insert_lines(lines: list[str], lines_to_insert: list[str], start_from: int) -> None:
insert_pt = start_from
for line in lines_to_insert:
lines.insert(insert_pt, line)
insert_pt = insert_pt + 1


def insert_code_blocks(lines: list[str], code_blocks: list[list[str]]) -> list[str]:
has_placeholders = True
while has_placeholders:
if CODE_BLOCK_PLACEHOLDER in lines:
idx = lines.index(CODE_BLOCK_PLACEHOLDER)
try:
block = code_blocks.pop(0)
insert_lines(lines, block, idx + 1)
del lines[idx]
except IndexError:
lines[idx] = "{Error: Couldn't re-insert code-block}"
else:
has_placeholders = False
return lines


def encode_uri_component(value: str) -> str:
return urllib.parse.quote(value, safe="")
Expand Down Expand Up @@ -71,14 +129,21 @@ def main() -> None:
new_body[:examples_preview_link_start] + EXAMPLES_PREVIEW_BARE_LINK + new_body[examples_preview_link_end:]
)

new_body = env.from_string(new_body).render(
lines = new_body.splitlines()
codeblocks = extract_code_blocks(lines)
text = "\n".join(lines)

new_body = env.from_string(text).render(
pr={
"number": args.pr_number,
"branch": pr.head.ref,
"commit": latest_commit.sha,
},
)

lines = new_body.split("\n")
new_body = "\n".join(insert_code_blocks(lines, codeblocks))

if new_body != pr.body:
print("updated pr body")
pr.edit(body=new_body)
Expand Down

0 comments on commit 6b3f61a

Please sign in to comment.