Skip to content

Commit

Permalink
Merge pull request #225 from IanCa/main
Browse files Browse the repository at this point in the history
Update verify branch pre-commit hook, add workflow
  • Loading branch information
VisLab authored Jul 16, 2024
2 parents 2f45891 + deb5f5a commit 76a4dcf
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/pre_commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
on:
pull_request:
types: [labeled, opened, reopened, synchronize]
push:
branches: ["develop*"]

jobs:
run-precommit:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Get latest push changes
id: changed-files
uses: tj-actions/changed-files@v44
with:
since_last_remote_commit: true

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Install Pre-commit
run: pip install pre-commit

- name: Run pre-commit on changed files
run: |
if [ -n "${{ steps.changed-files.outputs.all_changed_files }}" ]; then
pre-commit run --files ${{ steps.changed-files.outputs.all_changed_files }}
else
echo "No files have changed, skipping pre-commit."
fi
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: local
hooks:
- id: check-branch-and-files
name: Check branch and modified files
entry: './scripts/verify_branch.sh'
language: script
types: [file]
pass_filenames: true
always_run: true
61 changes: 61 additions & 0 deletions scripts/verify_branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
# Script to verify changes are on the correct branch
# only applies to branches that start with develop-

# Try to use the BRANCH_NAME environment variable first
if [ -z "$BRANCH_NAME" ]; then
# Environment variable is empty or not set, fallback to git command
echo "Finding branch name from git"
branch_name=$(git rev-parse --abbrev-ref HEAD)
else
# Use the environment variable
branch_name=$BRANCH_NAME
fi


# Echo the branch name and changed files
echo "branch_name: $branch_name"
echo "files: $@"
# Check if the branch name does not start with 'develop-'
if [[ ! "$branch_name" =~ ^develop- ]]; then
echo "This script only processes branches starting with 'develop-'. Exiting..."
exit 0
fi

# Strip 'develop-' from the branch name
branch_name=${branch_name#develop-}

# Verify the branch name is correct after modification
echo "Processed branch_name: $branch_name"

# Define base file pattern based on branch name
if [[ "$branch_name" == "standard" ]]; then
base_pattern="standard_schema/"
else
base_pattern="library_schemas/${branch_name}/"
fi

# Define the second pattern by appending 'prerelease' to the base pattern
file_pattern="${base_pattern}prerelease/"

# Check if the file paths start with the defined pattern for specified extensions
for file in "$@"; do # "$@" will contain the list of modified files passed by pre-commit
extension="${file##*.}"
# Check files based on their extension
if [[ "$extension" == "xml" || "$extension" == "mediawiki" || "$extension" == "tsv" ]]; then
if [[ "$file" != "$file_pattern"* ]]; then
error_message+="Error: '$file' with extension .$extension should start with '$file_pattern'\n"
fi
else
# Allow other files to be modified anywhere under the base pattern directory
if [[ "$file" != "$base_pattern"* ]]; then
error_message+="Error: '$file' should not be modified on this branch. Only files under '$base_pattern' directory\n"
fi
fi
done

# Print all accumulated errors and exit if there were any
if [[ -n "$error_message" ]]; then
echo -e "$error_message"
exit 1
fi

0 comments on commit 76a4dcf

Please sign in to comment.