-
Notifications
You must be signed in to change notification settings - Fork 1.4k
164 lines (146 loc) · 6.23 KB
/
format-auto-fix.yml
File metadata and controls
164 lines (146 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: SDK Format Auto-Fix
on:
workflow_dispatch:
inputs:
pr_number:
description: PR number to run format fix on
required: true
type: string
jobs:
fix-format:
runs-on: ubuntu-latest
# One run per PR at a time; queued runs wait rather than being cancelled.
concurrency:
group: format-auto-fix-${{ github.event.inputs.pr_number }}
cancel-in-progress: false
permissions:
contents: write
pull-requests: write
steps:
- name: Fetch and validate PR
id: find-pr
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const prNumberStr = context.payload.inputs?.pr_number;
if (!prNumberStr) {
core.warning('No pr_number input provided');
core.setOutput('has-pr', 'false');
return;
}
const prNumber = parseInt(prNumberStr);
core.info(`Got PR #${prNumber} from workflow_dispatch input`);
const { data: pr } = await github.rest.pulls.get({
...context.repo,
pull_number: prNumber,
});
if (pr.state !== 'open') {
core.info(`PR #${prNumber} is not open — skipping`);
core.setOutput('has-pr', 'false');
return;
}
core.info(`Processing PR #${pr.number} on branch ${pr.head.ref}`);
core.setOutput('has-pr', 'true');
core.setOutput('pr-branch', pr.head.ref);
core.setOutput('pr-repo', pr.head.repo.full_name);
- name: Checkout PR branch
if: steps.find-pr.outputs.has-pr == 'true'
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ steps.find-pr.outputs.pr-branch }}
repository: ${{ steps.find-pr.outputs.pr-repo }}
# Token with write access needed to push the fix commit back
token: ${{ secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup Node.js
if: steps.find-pr.outputs.has-pr == 'true'
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
with:
node-version: 'lts/*'
- name: Setup pnpm
if: steps.find-pr.outputs.has-pr == 'true'
uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0
with:
version: '10.33.0'
- name: Find affected packages and run pnpm format
if: steps.find-pr.outputs.has-pr == 'true'
run: |
UPSTREAM_REPO_URL="${{ github.server_url }}/${{ github.repository }}.git"
UPSTREAM_DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
# `origin` points at the PR head repository after checkout, which may be a fork.
# Fetch the workflow repository's default branch explicitly and diff against that.
if git remote get-url upstream >/dev/null 2>&1; then
git remote set-url upstream "$UPSTREAM_REPO_URL"
else
git remote add upstream "$UPSTREAM_REPO_URL"
fi
git fetch --no-tags upstream "${UPSTREAM_DEFAULT_BRANCH}:refs/remotes/upstream/${UPSTREAM_DEFAULT_BRANCH}"
# Find unique package directories (sdk/<service>/<package>) from changed files
PKGS=$(git diff --name-only "upstream/${UPSTREAM_DEFAULT_BRANCH}...HEAD" \
| grep '^sdk/' \
| sed 's|\(sdk/[^/]*/[^/]*\)/.*|\1|' \
| sort -u)
echo "Affected packages:"
echo "$PKGS"
if [ -z "$PKGS" ]; then
echo "No sdk/ packages changed, nothing to format"
exit 0
fi
# Build pnpm filter args (e.g. --filter=@azure/arm-foo...)
FILTERS=""
for pkg in $PKGS; do
if [ -f "$pkg/package.json" ]; then
PKG_NAME=$(node -p "require('./$pkg/package.json').name")
FILTERS="$FILTERS --filter=${PKG_NAME}"
fi
done
# Install only the affected packages and their dependencies
# --ignore-scripts prevents pre/postinstall lifecycle scripts from running
pnpm install $FILTERS --frozen-lockfile=false --ignore-scripts
# Run format in each affected package directory
for pkg in $PKGS; do
if [ -f "$pkg/package.json" ]; then
echo "Running pnpm format in $pkg"
(cd "$pkg" && pnpm format)
fi
done
- name: Commit and push formatting changes
if: steps.find-pr.outputs.has-pr == 'true'
id: commit
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if git diff --quiet; then
echo "No formatting changes needed"
echo "CHANGES=false" >> $GITHUB_OUTPUT
else
git diff --name-only \
| grep -E '\.(ts|mts|cts|tsx|js|mjs|cjs|jsx|json|md|yaml|yml)$' \
| xargs -r git add --
if git diff --cached --quiet; then
echo "Formatting changes were detected, but none matched the commit allow-list"
echo "CHANGES=false" >> $GITHUB_OUTPUT
else
git commit -m "chore: apply prettier format fixes"
git push
echo "CHANGES=true" >> $GITHUB_OUTPUT
fi
fi
- name: Post result comment
if: steps.find-pr.outputs.has-pr == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
env:
CHANGES: ${{ steps.commit.outputs.CHANGES }}
with:
script: |
const prNumber = parseInt(context.payload.inputs.pr_number);
if (process.env.CHANGES === 'true') {
await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body: '✅ Automatically ran `pnpm format` and pushed the formatting fixes. Check-format should now pass.'
});
core.info(`Posted success comment on PR #${prNumber}`);
} else {
core.info(`No formatting changes were needed for PR #${prNumber}`);
}