Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f239d22

Browse files
authored
Enforce no formatting changes for PRs via CI (psfGH-2951)
Now PRs will run two diff-shades jobs, "preview-changes" which formats all projects with preview=True, and "assert-no-changes" which formats all projects with preview=False. The latter also fails if any changes were made. Pushes to main will only run "preview-changes" Also the workflow_dispatch feature was dropped since it was complicating everything for little gain.
1 parent bd1e980 commit f239d22

4 files changed

Lines changed: 121 additions & 151 deletions

File tree

.github/workflows/diff_shades.yml

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,61 @@ name: diff-shades
33
on:
44
push:
55
branches: [main]
6-
paths-ignore: ["docs/**", "tests/**", "**.md", "**.rst"]
6+
paths: ["src/**", "setup.*", "pyproject.toml", ".github/workflows/*"]
77

88
pull_request:
9-
paths-ignore: ["docs/**", "tests/**", "**.md", "**.rst"]
10-
11-
workflow_dispatch:
12-
inputs:
13-
baseline:
14-
description: >
15-
The baseline revision. Pro-tip, use `.pypi` to use the latest version
16-
on PyPI or `.XXX` to use a PR.
17-
required: true
18-
default: main
19-
baseline-args:
20-
description: "Custom Black arguments (eg. -l 79)"
21-
required: false
22-
target:
23-
description: >
24-
The target revision to compare against the baseline. Same tip applies here.
25-
required: true
26-
target-args:
27-
description: "Custom Black arguments (eg. -S)"
28-
required: false
9+
paths: ["src/**", "setup.*", "pyproject.toml", ".github/workflows/*"]
2910

3011
concurrency:
3112
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
3213
cancel-in-progress: true
3314

3415
jobs:
16+
configure:
17+
runs-on: ubuntu-latest
18+
outputs:
19+
matrix: ${{ steps.set-config.outputs.matrix }}
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
- uses: actions/setup-python@v3
24+
25+
- name: Install diff-shades and support dependencies
26+
run: |
27+
python -m pip install click packaging urllib3
28+
python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip
29+
30+
- name: Calculate run configuration & metadata
31+
id: set-config
32+
env:
33+
GITHUB_TOKEN: ${{ github.token }}
34+
run: >
35+
python scripts/diff_shades_gha_helper.py config ${{ github.event_name }} ${{ matrix.mode }}
36+
3537
analysis:
36-
name: analysis / linux
38+
name: analysis / ${{ matrix.mode }}
39+
needs: configure
3740
runs-on: ubuntu-latest
3841
env:
3942
# Clang is less picky with the C code it's given than gcc (and may
4043
# generate faster binaries too).
4144
CC: clang-12
45+
strategy:
46+
fail-fast: false
47+
matrix:
48+
include: ${{ fromJson(needs.configure.outputs.matrix )}}
4249

4350
steps:
4451
- name: Checkout this repository (full clone)
4552
uses: actions/checkout@v3
4653
with:
54+
# The baseline revision could be rather old so a full clone is ideal.
4755
fetch-depth: 0
4856

4957
- uses: actions/setup-python@v3
5058

5159
- name: Install diff-shades and support dependencies
5260
run: |
53-
python -m pip install pip --upgrade
5461
python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip
5562
python -m pip install click packaging urllib3
5663
python -m pip install -r .github/mypyc-requirements.txt
@@ -59,92 +66,89 @@ jobs:
5966
git config user.name "diff-shades-gha"
6067
git config user.email "[email protected]"
6168
62-
- name: Calculate run configuration & metadata
63-
id: config
64-
env:
65-
GITHUB_TOKEN: ${{ github.token }}
66-
run: >
67-
python helper.py config ${{ github.event_name }}
68-
${{ github.event.inputs.baseline }} ${{ github.event.inputs.target }}
69-
--baseline-args "${{ github.event.inputs.baseline-args }}"
70-
7169
- name: Attempt to use cached baseline analysis
7270
id: baseline-cache
7371
uses: actions/[email protected]
7472
with:
75-
path: ${{ steps.config.outputs.baseline-analysis }}
76-
key: ${{ steps.config.outputs.baseline-cache-key }}
73+
path: ${{ matrix.baseline-analysis }}
74+
key: ${{ matrix.baseline-cache-key }}
7775

7876
- name: Build and install baseline revision
7977
if: steps.baseline-cache.outputs.cache-hit != 'true'
8078
env:
8179
GITHUB_TOKEN: ${{ github.token }}
8280
run: >
83-
${{ steps.config.outputs.baseline-setup-cmd }}
81+
${{ matrix.baseline-setup-cmd }}
8482
&& python setup.py --use-mypyc bdist_wheel
8583
&& python -m pip install dist/*.whl && rm build dist -r
8684
8785
- name: Analyze baseline revision
8886
if: steps.baseline-cache.outputs.cache-hit != 'true'
8987
run: >
9088
diff-shades analyze -v --work-dir projects-cache/
91-
${{ steps.config.outputs.baseline-analysis }} -- ${{ github.event.inputs.baseline-args }}
89+
${{ matrix.baseline-analysis }} ${{ matrix.force-flag }}
9290
9391
- name: Build and install target revision
9492
env:
9593
GITHUB_TOKEN: ${{ github.token }}
9694
run: >
97-
${{ steps.config.outputs.target-setup-cmd }}
95+
${{ matrix.target-setup-cmd }}
9896
&& python setup.py --use-mypyc bdist_wheel
9997
&& python -m pip install dist/*.whl
10098
10199
- name: Analyze target revision
102100
run: >
103101
diff-shades analyze -v --work-dir projects-cache/
104-
${{ steps.config.outputs.target-analysis }} --repeat-projects-from
105-
${{ steps.config.outputs.baseline-analysis }} -- ${{ github.event.inputs.target-args }}
102+
${{ matrix.target-analysis }} --repeat-projects-from
103+
${{ matrix.baseline-analysis }} ${{ matrix.force-flag }}
106104
107105
- name: Generate HTML diff report
108106
run: >
109-
diff-shades --dump-html diff.html compare --diff --quiet
110-
${{ steps.config.outputs.baseline-analysis }} ${{ steps.config.outputs.target-analysis }}
107+
diff-shades --dump-html diff.html compare --diff
108+
${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}
111109
112110
- name: Upload diff report
113111
uses: actions/upload-artifact@v2
114112
with:
115-
name: diff.html
113+
name: ${{ matrix.mode }}-diff.html
116114
path: diff.html
117115

118116
- name: Upload baseline analysis
119117
uses: actions/upload-artifact@v2
120118
with:
121-
name: ${{ steps.config.outputs.baseline-analysis }}
122-
path: ${{ steps.config.outputs.baseline-analysis }}
119+
name: ${{ matrix.baseline-analysis }}
120+
path: ${{ matrix.baseline-analysis }}
123121

124122
- name: Upload target analysis
125123
uses: actions/upload-artifact@v2
126124
with:
127-
name: ${{ steps.config.outputs.target-analysis }}
128-
path: ${{ steps.config.outputs.target-analysis }}
125+
name: ${{ matrix.target-analysis }}
126+
path: ${{ matrix.target-analysis }}
129127

130128
- name: Generate summary file (PR only)
131-
if: github.event_name == 'pull_request'
129+
if: github.event_name == 'pull_request' && matrix.mode == 'preview-changes'
132130
run: >
133131
python helper.py comment-body
134-
${{ steps.config.outputs.baseline-analysis }} ${{ steps.config.outputs.target-analysis }}
135-
${{ steps.config.outputs.baseline-sha }} ${{ steps.config.outputs.target-sha }}
132+
${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}
133+
${{ matrix.baseline-sha }} ${{ matrix.target-sha }}
136134
${{ github.event.pull_request.number }}
137135
138136
- name: Upload summary file (PR only)
139-
if: github.event_name == 'pull_request'
137+
if: github.event_name == 'pull_request' && matrix.mode == 'preview-changes'
140138
uses: actions/upload-artifact@v2
141139
with:
142140
name: .pr-comment.json
143141
path: .pr-comment.json
144142

145-
# This is last so the diff-shades-comment workflow can still work even if we
146-
# end up detecting failed files and failing the run.
147-
- name: Check for failed files in both analyses
143+
- name: Verify zero changes (PR only)
144+
if: matrix.mode == 'assert-no-changes'
145+
run: >
146+
diff-shades compare --check ${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}
147+
|| (echo "Please verify you didn't change the stable code style unintentionally!" && exit 1)
148+
149+
- name: Check for failed files for target revision
150+
# Even if the previous step failed, we should still check for failed files.
151+
if: always()
148152
run: >
149-
diff-shades show-failed --check --show-log ${{ steps.config.outputs.baseline-analysis }};
150-
diff-shades show-failed --check --show-log ${{ steps.config.outputs.target-analysis }}
153+
diff-shades show-failed --check --show-log ${{ matrix.target-analysis }}
154+
--check-allow 'sqlalchemy:test/orm/test_relationship_criteria.py'

docs/contributing/gauging_changes.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,50 @@ enough to cause frustration to projects that are already "black formatted".
99

1010
## diff-shades
1111

12-
diff-shades is a tool that runs _Black_ across a list of Git cloneable OSS projects
13-
recording the results. The main highlight feature of diff-shades is being able to
14-
compare two revisions of _Black_. This is incredibly useful as it allows us to see what
15-
exact changes will occur, say merging a certain PR.
12+
diff-shades is a tool that runs _Black_ across a list of open-source projects recording
13+
the results. The main highlight feature of diff-shades is being able to compare two
14+
revisions of _Black_. This is incredibly useful as it allows us to see what exact
15+
changes will occur, say merging a certain PR.
1616

1717
For more information, please see the [diff-shades documentation][diff-shades].
1818

1919
### CI integration
2020

2121
diff-shades is also the tool behind the "diff-shades results comparing ..." /
2222
"diff-shades reports zero changes ..." comments on PRs. The project has a GitHub Actions
23-
workflow which runs diff-shades twice against two revisions of _Black_ according to
24-
these rules:
23+
workflow that analyzes and compares two revisions of _Black_ according to these rules:
2524

2625
| | Baseline revision | Target revision |
2726
| --------------------- | ----------------------- | ---------------------------- |
2827
| On PRs | latest commit on `main` | PR commit with `main` merged |
2928
| On pushes (main only) | latest PyPI version | the pushed commit |
3029

31-
Once finished, a PR comment will be posted embedding a summary of the changes and links
32-
to further information. If there's a pre-existing diff-shades comment, it'll be updated
33-
instead the next time the workflow is triggered on the same PR.
30+
For pushes to main, there's only one analysis job named `preview-changes` where the
31+
preview style is used for all projects.
3432

35-
The workflow uploads 3-4 artifacts upon completion: the two generated analyses (they
36-
have the .json file extension), `diff.html`, and `.pr-comment.json` if triggered by a
37-
PR. The last one is downloaded by the `diff-shades-comment` workflow and shouldn't be
38-
downloaded locally. `diff.html` comes in handy for push-based or manually triggered
39-
runs. And the analyses exist just in case you want to do further analysis using the
40-
collected data locally.
33+
For PRs they get one more analysis job: `assert-no-changes`. It's similar to
34+
`preview-changes` but runs with the stable code style. It will fail if changes were
35+
made. This makes sure code won't be reformatted again and again within the same year in
36+
accordance to Black's stability policy.
4137

42-
Note that the workflow will only fail intentionally if while analyzing a file failed to
38+
Additionally for PRs, a PR comment will be posted embedding a summary of the preview
39+
changes and links to further information. If there's a pre-existing diff-shades comment,
40+
it'll be updated instead the next time the workflow is triggered on the same PR.
41+
42+
```{note}
43+
The `preview-changes` job will only fail intentionally if while analyzing a file failed to
4344
format. Otherwise a failure indicates a bug in the workflow.
45+
```
4446

45-
```{tip}
46-
Maintainers with write access or higher can trigger the workflow manually from the
47-
Actions tab using the `workflow_dispatch` event. Simply select "diff-shades"
48-
from the workflows list on the left, press "Run workflow", and fill in which revisions
49-
and command line arguments to use.
47+
The workflow uploads several artifacts upon completion:
5048

51-
Once finished, check the logs or download the artifacts for local use.
52-
```
49+
- The raw analyses (.json)
50+
- HTML diffs (.html)
51+
- `.pr-comment.json` (if triggered by a PR)
52+
53+
The last one is downloaded by the `diff-shades-comment` workflow and shouldn't be
54+
downloaded locally. The HTML diffs come in handy for push-based where there's no PR to
55+
post a comment. And the analyses exist just in case you want to do further analysis
56+
using the collected data locally.
5357

5458
[diff-shades]: https://github.com/ichard26/diff-shades#readme

0 commit comments

Comments
 (0)