Skip to content

Commit e5f4e39

Browse files
authored
Add isort dependabot auto-migration workflow (#585)
This migration works like `black` migration, it runs `isort` on all files when Dependabot bumps `isort`, so any import-ordering changes introduced by the upgrade are applied automatically to the PR.
2 parents d706338 + 3afcb70 commit e5f4e39

43 files changed

Lines changed: 1647 additions & 18 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ updates:
2929
exclude-patterns:
3030
# pydoclint has shipped breaking changes in patch updates often
3131
- "pydoclint"
32+
- "isort"
3233
minor:
3334
update-types:
3435
- "minor"
@@ -41,6 +42,7 @@ updates:
4142
- "mkdocstrings[python]"
4243
- "pydoclint"
4344
- "pytest-asyncio"
45+
- "isort"
4446
mkdocstrings:
4547
patterns:
4648
- "mkdocstrings*"

.github/workflows/auto-dependabot.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ jobs:
2222
name: Auto-merge Dependabot PR
2323
if: |
2424
github.actor == 'dependabot[bot]' &&
25-
!contains(github.event.pull_request.title, 'Bump black from ')
25+
!contains(github.event.pull_request.title, 'Bump black from ') &&
26+
!contains(github.event.pull_request.title, 'Bump isort from ')
2627
runs-on: ubuntu-slim
2728
steps:
2829
- name: Generate GitHub App token
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Automatic isort migration for Dependabot PRs
2+
#
3+
# When Dependabot upgrades isort, this workflow installs the new version and
4+
# runs `isort .` so the PR already contains any import-ordering changes
5+
# introduced by the upgrade, while leaving the PR open for review.
6+
#
7+
# isort follows SemVer but its release policy
8+
# (https://github.com/PyCQA/isort/blob/main/docs/major_releases/release_policy.md)
9+
# explicitly allows intentional formatting changes in minor releases, and
10+
# patch releases may also adjust output in smaller bug-fix ways. Because of
11+
# that, isort is excluded from the regular `patch` and `minor` Dependabot
12+
# groups: every isort bump produces an individual `Bump isort from …` PR and
13+
# is routed through this migration workflow.
14+
#
15+
# The companion auto-dependabot workflow skips those PRs so they're handled
16+
# exclusively by this migration workflow.
17+
#
18+
# XXX: !!! SECURITY WARNING !!!
19+
# pull_request_target has write access to the repo, and can read secrets.
20+
# This is required because Dependabot PRs are treated as fork PRs: the
21+
# GITHUB_TOKEN is read-only and secrets are unavailable with a plain
22+
# pull_request trigger. The action mitigates the risk by:
23+
# - Never executing code from the PR (the migration script is embedded
24+
# in this workflow file on the base branch, not taken from the PR).
25+
# - Gating migration steps on github.actor == 'dependabot[bot]'.
26+
# - Running checkout with persist-credentials: false and isolating
27+
# push credentials from the migration script environment.
28+
# For more details read:
29+
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
30+
31+
name: isort Migration
32+
33+
on:
34+
merge_group: # To allow using this as a required check for merging
35+
pull_request_target:
36+
types: [opened, synchronize, reopened, labeled, unlabeled]
37+
38+
permissions:
39+
# Commit reformatted files back to the PR branch.
40+
contents: write
41+
# Create and normalize migration state labels.
42+
issues: write
43+
# Read/update pull request metadata and comments.
44+
pull-requests: write
45+
46+
jobs:
47+
isort-migration:
48+
name: Migrate isort
49+
# Skip if it was triggered by the merge queue. We only need the workflow to
50+
# be executed to meet the "Required check" condition for merging, but we
51+
# don't need to actually run the job, having the job present as Skipped is
52+
# enough.
53+
if: |
54+
github.event_name == 'pull_request_target' &&
55+
github.actor == 'dependabot[bot]' &&
56+
contains(github.event.pull_request.title, 'Bump isort from ')
57+
runs-on: ubuntu-24.04
58+
steps:
59+
- name: Generate token
60+
id: create-app-token
61+
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
62+
with:
63+
app-id: ${{ secrets.FREQUENZ_AUTO_DEPENDABOT_APP_ID }}
64+
private-key: ${{ secrets.FREQUENZ_AUTO_DEPENDABOT_APP_PRIVATE_KEY }}
65+
# Push reformatted files to the PR branch.
66+
permission-contents: write
67+
# Create and normalize migration state labels.
68+
permission-issues: write
69+
# Read/update pull request metadata and labels.
70+
permission-pull-requests: write
71+
- name: Migrate
72+
uses: frequenz-floss/gh-action-dependabot-migrate@27763fb5eb56476d91abe00132e8a0614171f92f # v1.2.0
73+
with:
74+
migration-script: |
75+
import os
76+
import subprocess
77+
import sys
78+
79+
version = os.environ["MIGRATION_VERSION"].lstrip("v")
80+
subprocess.run(
81+
[sys.executable, "-Im", "pip", "install", f"isort=={version}"],
82+
check=True,
83+
)
84+
subprocess.run([sys.executable, "-Im", "isort", "."], check=True)
85+
token: ${{ steps.create-app-token.outputs.token }}
86+
auto-merge-on-changes: "false"
87+
version-iteration: "false"
88+
sign-commits: "true"
89+
auto-merged-label: "tool:auto-merged"
90+
migrated-label: "tool:isort:migration:executed"
91+
intervention-pending-label: "tool:isort:migration:intervention-pending"
92+
intervention-done-label: "tool:isort:migration:intervention-done"

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ But you might still need to adapt your code:
3232
- API projects now have a better grpcio/protobuf updates grouping in Dependabot, which should make upgrading easier, and plays nicer with the new `grpc-migration.yaml` workflow.
3333
- API projects should now use the new API-specific *Protect version branches* ruleset variant, which includes the required `Fix gRPC/protobuf runtime floors` check without affecting non-API Python projects.
3434
- Workflows using the `gh-action-dependabot-migrate` are upgraded to the latest version, which avoids unnecessary version iterations.
35+
- Add an `isort-migration.yaml` workflow that automatically reorders imports when Dependabot upgrades `isort`.
3536

3637
## Bug Fixes
3738

@@ -44,6 +45,7 @@ But you might still need to adapt your code:
4445
- The issue template chooser (`config.yml`) no longer includes the `contact_links` section for private repositories, since GitHub Discussions are typically disabled for them.
4546
- Normalized the GitHub Action hashes for `gh-action-setup-git` and `gh-action-setup-python-with-deps` to point to the actual commit object, which is what Dependabot expects.
4647
- API projects now configure black with `extend-exclude = '^/submodules/'` so the formatting check doesn't descend into external git submodules that don't follow our formatting rules.
48+
- API projects now configure isort with `skip_glob = ["submodules/*"]` so the import-sorting check doesn't descend into external git submodules that don't follow our rules.
4749
- `CONTRIBUTING.md`
4850
* Fixed the nox example commands in to use the correct `tests/` directory instead of the non-existent `test/` directory.
4951
* Fixed the wrong mention to PyPI publishing when releasing for private repositories.

0 commit comments

Comments
 (0)