Skip to content

Commit

Permalink
Refactor duplication by creating a generic linting script
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndpnt committed May 10, 2024
1 parent cb310dc commit 4253f37
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
16 changes: 0 additions & 16 deletions .github/lint-changed-python-files.sh

This file was deleted.

16 changes: 0 additions & 16 deletions .github/lint-changed-yaml-tests.sh

This file was deleted.

32 changes: 32 additions & 0 deletions .github/lint-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/env bash

# Usage: lint-files.sh <file_pattern> <linter_command>
#
# Example usage:
# lint-files.sh "*.py" "flake8"
# lint-files.sh "tests/*.yaml" "yamllint"

file_pattern=$1
linter_command=$2

if [ -z "$file_pattern" ] || [ -z "$linter_command" ]
then
echo "Usage: $0 <file_pattern> <linter_command>"
exit 1
fi

last_tagged_commit=$(git describe --tags --abbrev=0 --first-parent 2>/dev/null) # Attempt to find the last tagged commit in the direct ancestry of the main branch avoiding tags introduced by merge commits from other branches

if [ -z "$last_tagged_commit" ]
then
last_tagged_commit=$(git rev-list --max-parents=0 HEAD) # Fallback to finding the root commit if no tags are present
fi

if ! changed_files=$(git diff-index --name-only --diff-filter=ACMR --exit-code $last_tagged_commit -- "$file_pattern")
then
echo "Linting the following files:"
echo "$changed_files"
$linter_command $changed_files
else
echo "No changed files matching pattern '$file_pattern' to lint."
fi
6 changes: 3 additions & 3 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
- run: make check-style

- name: Lint Python files
run: "${GITHUB_WORKSPACE}/.github/lint-changed-python-files.sh"

run: "${GITHUB_WORKSPACE}/.github/lint-files.sh '*.py' 'flake8'"
- name: Lint YAML tests
run: "${GITHUB_WORKSPACE}/.github/lint-changed-yaml-tests.sh"
run: "${GITHUB-WORKSPACE}/.github/lint-files.sh 'tests/*.yaml' 'yamllint'"

test-yaml:
runs-on: ubuntu-22.04
Expand Down

0 comments on commit 4253f37

Please sign in to comment.