Skip to content

Commit 2d4e89f

Browse files
authored
[Bot] Revive prdoc bot (#5648)
Prdoc bot was deleted in #5457 just after being added in #5331 without replacement. Now bringing it back until it is integrated into the new command structure. Formatting is now also fixed, such that the title is always first and the description renders correctly. --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
1 parent 030cb4a commit 2d4e89f

2 files changed

Lines changed: 111 additions & 5 deletions

File tree

.github/scripts/generate-prdoc.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,28 @@ def create_prdoc(pr, audience, title, description, patch, bump, force):
4848
else:
4949
print(f"No preexisting PrDoc for PR {pr}")
5050

51-
prdoc = { "doc": [{}], "crates": [] }
51+
prdoc = { "title": title, "doc": [{}], "crates": [] }
5252

53-
prdoc["title"] = title
5453
prdoc["doc"][0]["audience"] = audience
5554
prdoc["doc"][0]["description"] = description
5655

5756
workspace = Workspace.from_path(".")
5857

5958
modified_paths = []
6059
for diff in whatthepatch.parse_patch(patch):
61-
modified_paths.append(diff.header.new_path)
60+
new_path = diff.header.new_path
61+
# Sometimes this lib returns `/dev/null` as the new path...
62+
if not new_path.startswith("/dev"):
63+
modified_paths.append(new_path)
6264

6365
modified_crates = {}
6466
for p in modified_paths:
6567
# Go up until we find a Cargo.toml
6668
p = os.path.join(workspace.path, p)
6769
while not os.path.exists(os.path.join(p, "Cargo.toml")):
70+
print(f"Could not find Cargo.toml in {p}")
71+
if p == '/':
72+
exit(1)
6873
p = os.path.dirname(p)
6974

7075
with open(os.path.join(p, "Cargo.toml")) as f:
@@ -95,9 +100,19 @@ def create_prdoc(pr, audience, title, description, patch, bump, force):
95100

96101
# write the parsed PR documentation back to the file
97102
with open(path, "w") as f:
98-
yaml.dump(prdoc, f)
103+
yaml.dump(prdoc, f, sort_keys=False)
99104
print(f"PrDoc for PR {pr} written to {path}")
100105

106+
# Make the `description` a multiline string instead of escaping \r\n.
107+
def setup_yaml():
108+
def yaml_multiline_string_presenter(dumper, data):
109+
if len(data.splitlines()) > 1:
110+
data = '\n'.join([line.rstrip() for line in data.strip().splitlines()])
111+
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
112+
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
113+
114+
yaml.add_representer(str, yaml_multiline_string_presenter)
115+
101116
def parse_args():
102117
parser = argparse.ArgumentParser()
103118
parser.add_argument("--pr", type=int, required=True)
@@ -108,6 +123,7 @@ def parse_args():
108123

109124
if __name__ == "__main__":
110125
args = parse_args()
111-
force = True if args.force.lower() == "true" else False
126+
force = True if (args.force or "false").lower() == "true" else False
112127
print(f"Args: {args}, force: {force}")
128+
setup_yaml()
113129
from_pr_number(args.pr, args.audience, args.bump, force)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Command PrDoc
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
pr:
7+
type: number
8+
description: Number of the Pull Request
9+
required: true
10+
bump:
11+
type: choice
12+
description: Default bump level for all crates
13+
default: "TODO"
14+
required: true
15+
options:
16+
- "TODO"
17+
- "no change"
18+
- "patch"
19+
- "minor"
20+
- "major"
21+
audience:
22+
type: choice
23+
description: Audience of the PrDoc
24+
default: "TODO"
25+
required: true
26+
options:
27+
- "TODO"
28+
- "Runtime Dev"
29+
- "Runtime User"
30+
- "Node Dev"
31+
- "Node User"
32+
overwrite:
33+
type: choice
34+
description: Overwrite existing PrDoc
35+
default: "true"
36+
required: true
37+
options:
38+
- "true"
39+
- "false"
40+
41+
concurrency:
42+
group: command-prdoc
43+
cancel-in-progress: true
44+
45+
jobs:
46+
set-image:
47+
runs-on: ubuntu-latest
48+
outputs:
49+
IMAGE: ${{ steps.set_image.outputs.IMAGE }}
50+
steps:
51+
- name: Checkout
52+
uses: actions/checkout@v4
53+
- id: set_image
54+
run: cat .github/env >> $GITHUB_OUTPUT
55+
cmd-prdoc:
56+
needs: [set-image]
57+
runs-on: ubuntu-latest
58+
timeout-minutes: 20
59+
container:
60+
image: ${{ needs.set-image.outputs.IMAGE }}
61+
permissions:
62+
contents: write
63+
pull-requests: write
64+
steps:
65+
- name: Download repo
66+
uses: actions/checkout@v4
67+
- name: Install gh cli
68+
id: gh
69+
uses: ./.github/actions/set-up-gh
70+
with:
71+
pr-number: ${{ inputs.pr }}
72+
GH_TOKEN: ${{ github.token }}
73+
- name: Generate PrDoc
74+
run: |
75+
python3 -m pip install -q cargo-workspace PyGithub whatthepatch pyyaml toml
76+
77+
python3 .github/scripts/generate-prdoc.py --pr "${{ inputs.pr }}" --bump "${{ inputs.bump }}" --audience "${{ inputs.audience }}" --force "${{ inputs.overwrite }}"
78+
79+
- name: Report failure
80+
if: ${{ failure() }}
81+
run: gh pr comment ${{ inputs.pr }} --body "<h2>Command failed ❌</h2> Run by @${{ github.actor }} for <code>${{ github.workflow }}</code> failed. See logs <a href=\"$RUN\">here</a>."
82+
env:
83+
RUN: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
84+
GH_TOKEN: ${{ github.token }}
85+
- name: Push Commit
86+
uses: stefanzweifel/git-auto-commit-action@v5
87+
with:
88+
commit_message: Add PrDoc (auto generated)
89+
branch: ${{ steps.gh.outputs.branch }}
90+
file_pattern: 'prdoc/*.prdoc'

0 commit comments

Comments
 (0)