Skip to content
164 changes: 164 additions & 0 deletions .github/workflows/format-auto-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Comment thread
skywing918 marked this conversation as resolved.
Outdated
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'
Comment thread
jeremymeng marked this conversation as resolved.
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 }}
Comment thread
skywing918 marked this conversation as resolved.
Outdated
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
Comment thread
skywing918 marked this conversation as resolved.
Outdated

# 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)
Comment thread
skywing918 marked this conversation as resolved.
Outdated
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
Comment thread
skywing918 marked this conversation as resolved.

- 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}`);
}
55 changes: 38 additions & 17 deletions .github/workflows/mgmt-review.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading