-
Notifications
You must be signed in to change notification settings - Fork 194
Add workflow and script to clean Dependabot description bodies #989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mhucka
wants to merge
5
commits into
quantumlib:main
Choose a base branch
from
mhucka:mh-pr-cleaner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+504
−0
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c36930c
Fix incorrect path in git checkout
mhucka ea62bcc
Add handling of debug flag
mhucka 2663b47
Add script to clean up Dependabot PR descriptions
mhucka a97aa66
Fix formatting
mhucka 6e8574e
Disable pylint long-line warnings for this test file
mhucka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| name: Dependabot PR cleaner | ||
| run-name: >- | ||
| Clean up description of PR ${{github.event.pull_request.number}} on | ||
| ${{github.ref_name}} by @${{github.actor}} | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: | ||
| - opened | ||
| - synchronize | ||
| - reopened | ||
|
|
||
| workflow_dispatch: | ||
| inputs: | ||
| pr-number: | ||
| description: 'The PR number of the PR to clean:' | ||
| required: true | ||
| debug: | ||
| description: 'Run with debugging options' | ||
| type: boolean | ||
| default: true | ||
|
|
||
| # Declare default workflow permissions as read only. | ||
| permissions: read-all | ||
|
|
||
| jobs: | ||
| clean-pr-description: | ||
| if: >- | ||
| ${{github.actor == 'dependabot[bot]' && | ||
| github.repository_owner == 'quantumlib'}} | ||
| name: Clean PR description | ||
| runs-on: ubuntu-slim | ||
| timeout-minutes: 5 | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| steps: | ||
| - name: Check out a copy of the git repository | ||
| uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 | ||
| with: | ||
| sparse-checkout: | | ||
| ./dev_tools/ci/dependabot_pr_description_cleaner.py | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v5 | ||
| with: | ||
| cache: pip | ||
|
|
||
| - name: Install dependencies | ||
| run: pip install html2text | ||
|
|
||
| - name: Clean the PR description | ||
| env: | ||
| GH_REPO: ${{github.repository}} | ||
| GH_TOKEN: ${{secrets.GITHUB_TOKEN}} | ||
| PR_NUMBER: ${{inputs.pr-number || github.event.pull_request.number}} | ||
| SHELLOPTS: ${{inputs.debug && 'xtrace' || '' }} | ||
| run: | | ||
| gh pr view ${{env.PR_NUMBER}} --json body --jq .body > body.txt | ||
| python3 dev_tools/ci/dependabot_pr_description_cleaner.py body.txt | ||
| gh pr edit ${{env.PR_NUMBER}} --body-file body.txt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Convert and streamline description bodies of Dependabot PRs.""" | ||
|
|
||
| import os | ||
| import re | ||
| import sys | ||
|
|
||
| import html2text | ||
|
|
||
|
|
||
| def process_text(content): | ||
| """Take PR description body and clean it. | ||
|
|
||
| Cleaning steps applied: | ||
| * Remove the section "Commits" | ||
| * Convert HTML to Markdown | ||
| * Remove the section "Dependabot commands and options" | ||
| """ | ||
|
|
||
| # Pattern: match the start of the details block for Commits, lazy match | ||
| # until the closing details tag, and match the following br tag if present. | ||
| content = re.sub( | ||
| r'<details>\s*<summary>Commits</summary>.*?</details>\s*(<br\s*/?>)?', | ||
| '', | ||
| content, | ||
| flags=re.DOTALL | re.IGNORECASE | ||
| ) | ||
|
|
||
| h = html2text.HTML2Text() | ||
| h.body_width = 0 | ||
| markdown_content = h.handle(content) | ||
|
|
||
| target_phrase = "Dependabot commands and options" | ||
| if target_phrase in markdown_content: | ||
| # Split at the last occurrence of the phrase. | ||
| parts = markdown_content.rsplit(target_phrase, 1) | ||
| pre_text = parts[0] | ||
| pre_text = pre_text.rstrip() | ||
| # Remove the marker if present at the end. | ||
| if pre_text.endswith(r'\---'): | ||
| pre_text = pre_text[:-4] | ||
| elif pre_text.endswith('---'): | ||
| pre_text = pre_text[:-3] | ||
| markdown_content = pre_text.strip() | ||
|
|
||
| return markdown_content | ||
|
|
||
|
|
||
| def clean_body(file_path): | ||
| """Reads the file, cleans the text, and writes it back to the same file.""" | ||
| with open(file_path, 'r') as f: | ||
| content = f.read() | ||
|
|
||
| markdown_content = process_text(content) | ||
|
|
||
| # Write back to file. | ||
| with open(file_path, 'w') as f: | ||
| f.write(markdown_content) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| if len(sys.argv) < 2: | ||
| script_name = os.path.basename(sys.argv[0]) | ||
| print(f"Usage: python3 {script_name} FILE") | ||
| sys.exit(1) | ||
| clean_body(sys.argv[1]) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / Scorecard
Pinned-Dependencies Medium