Skip to content
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

feat(ci): Run lint --fix with dirtybot #17619

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
20 changes: 19 additions & 1 deletion .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ jobs:
if: ${{ github.ref == 'ref/heads/main' }}
run: ./scripts/ci/20_check-formatting.sh "check"
- name: NX format:write
if: ${{ github.event_name == 'pull_request' }}
if: ${{ github.event_name == 'pull_request' && !contains(github.event.pull_request.labels.*.name, 'dirty bypass') }}
run: |
./scripts/ci/20_check-formatting.sh "write"
./infra/scripts/ci/git-check-dirty.sh "/" "nx format:write" "dirtybot"
Expand All @@ -362,6 +362,11 @@ jobs:
matrix: ${{ fromJson(needs.prepare.outputs.LINT_CHUNKS) }}
steps:
- uses: actions/checkout@v4
if: ${{ github.event_name == 'pull_request' }}
# Needed for doing git commit in git-check-dirty script
with:
token: ${{ secrets.DIRTY_FIX_BOT_TOKEN }}
ref: ${{ github.event.pull_request.head.ref }}
- uses: actions/setup-node@v4
with:
node-version-file: 'package.json'
Expand All @@ -374,6 +379,19 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
keys: ${{ needs.prepare.outputs.CACHE_KEYS }}
enable-cache: 'node_modules,generated-files'
- name: Lintfix
if: ${{ !contains(github.event.pull_request.labels.*.name, 'dirty bypass') }}
run: |
set -euo pipefail
echo "Running lint --fix for affected projects: ${AFFECTED_PROJECTS}"
./infra/scripts/ci/git-check-dirty.sh \
-p "/" \
-a "nx run-many --target lint --fix --parallel ${MAX_JOBS} --projects=${AFFECTED_PROJECTS//[[\]]}" \
-o "dirtybot" \
-r || {
echo "Error: Lintfix failed. Check the logs above for details." >&2
exit 1
}
- name: Linting
run: ./scripts/ci/run-in-parallel-native.sh lint

Expand Down
63 changes: 60 additions & 3 deletions infra/scripts/ci/git-check-dirty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,53 @@
set -euxo pipefail

DIR="$(git rev-parse --show-toplevel)"

show_help() {
cat <<EOF
Usage:
$0 <path> <action> <owner>
$0 [options]

The options -p, -a, -o are required if using the second form

Options:
-p <path> path to the file to check
-a <action> action to run
-o <owner> owner to commit as
-c check mode
-r run mode
EOF
}

if [[ $# -eq 0 ]]; then
show_help && exit 0
fi

rel_path=$1
abs_path=$DIR/$rel_path
action=$2
owner="${3:-github actions}"
check_mode="${CHECK_MODE:-}"
run_mode="${RUN_MODE:-}"

while getopts "p:a:o:crh" opt; do
case $opt in
p) abs_path="$DIR/$OPTARG" ;;
a) action="$OPTARG" ;;
o) owner="$OPTARG" ;;
c) check_mode=true ;;
r) run_mode=true ;;
h) show_help && exit 0 ;;
*) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done

for opt in "$abs_path" "$action" "$owner"; do
if [[ -z "$opt" ]] || [[ "$opt" = -* ]]; then
echo "A required argument is missing" >&2
exit 1
fi
done

commit_as_github_actions() {
git config user.name 'github-actions[bot]'
Expand All @@ -17,10 +60,22 @@ commit_as_dirty_bot() {
git config user.email '[email protected]'
}

if [[ "$run_mode" == true ]]; then
# Split action into array safely:
IFS=' ' read -r -a action_array <<<"$action"
if ! yarn "${action_array[@]}"; then
echo "Error: Failed to execute yarn command: ${action_array[*]}" >&2
exit 1
fi
fi

if [[ "$check_mode" == true ]] && ! git diff --quiet; then
echo "found unstaged files from $action, aborting"
exit 1
fi
if [[ $(git diff --stat "$abs_path") != '' ]]; then
echo "changes found in $rel_path that will be commited"
git diff "$abs_path"
git add "$abs_path"
git diff "$abs_path" || true
if [ "$owner" == "github actions" ]; then
commit_as_github_actions
elif [ "$owner" == "dirtybot" ]; then
Expand All @@ -29,7 +84,9 @@ if [[ $(git diff --stat "$abs_path") != '' ]]; then
echo "Error: Unknown owner!"
exit 1
fi
git commit -m "chore: $action update dirty files"
git commit -am "chore: update dirty files

$action"
git push
else
echo "found no unstaged files from $action, nothing to commit"
Expand Down
Loading