Last active
August 21, 2024 08:51
-
-
Save ulusoyca/dd6decf99980a65509873e1d810f72c3 to your computer and use it in GitHub Desktop.
check-for-tests-in-prs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check and Comment on Missing Tests | |
description: Checks if changed .dart files in a PR have corresponding _test.dart files and comments on the PR if tests are missing. | |
inputs: | |
github-token: | |
description: 'GitHub token to post comments' | |
required: true | |
outputs: | |
missing_tests: | |
description: 'List of files missing tests' | |
runs: | |
using: "composite" | |
steps: | |
# Step 1: Check out the repository | |
- name: Check out the repository | |
uses: actions/checkout@v2 | |
# Step 2: Identify changed .dart files | |
- name: Identify changed .dart files | |
id: identify-changes | |
shell: bash | |
run: | | |
# Get a list of all added or modified .dart files in the PR | |
git diff --name-only --diff-filter=AM origin/master | grep '\.dart$' > changed_files.txt || true | |
# Check if changed_files.txt is empty | |
if [[ ! -s changed_files.txt ]]; then | |
echo "No .dart files have been modified or added." | |
# Set an environment variable to indicate no files were found | |
echo "no_changed_files=true" >> $GITHUB_ENV | |
else | |
# Output the changed files for debugging | |
echo "Changed .dart files:" | |
cat changed_files.txt | |
echo "no_changed_files=false" >> $GITHUB_ENV | |
fi | |
# Step 3: Check for corresponding test files | |
- name: Check for corresponding test files | |
id: check-tests | |
if: env.no_changed_files == 'false' | |
shell: bash | |
run: | | |
# Initialize an empty variable to store files without corresponding tests | |
missing_tests="" | |
# Loop over each changed .dart file | |
while IFS= read -r file; do | |
# Skip files that do not start with lib/ unless they are in the test/ directory | |
if [[ ! $file =~ ^lib/ && ! $file =~ ^test/ ]]; then | |
continue | |
fi | |
# Further exclude specific patterns within the lib/ directory | |
if [[ $file =~ \.g\.dart$ || $file =~ \.freezed\.dart$ || $file =~ ^lib/localization/ ]]; then | |
continue | |
fi | |
# Check if the file is not a test file and whether it has a corresponding _test.dart file | |
if [[ ! $file =~ _test\.dart$ ]]; then | |
test_file="${file%.*}_test.dart" | |
if ! grep -q "$test_file" changed_files.txt; then | |
missing_tests="${missing_tests}${file}\n" | |
fi | |
fi | |
done < changed_files.txt | |
# Use GITHUB_ENV to set the missing_tests output | |
echo "missing_tests=${missing_tests}" >> $GITHUB_ENV | |
# Debugging: Output the files missing tests | |
echo "Files missing tests:" | |
echo "${missing_tests}" | |
# Step 4: Comment on PR if tests are missing | |
- name: Comment on PR if tests are missing | |
if: ${{ env.missing_tests != '' }} | |
uses: actions/[email protected] | |
with: | |
github-token: ${{ inputs.github-token }} | |
script: | | |
const missingFiles = `${{ env.missing_tests }}`.split('\n').filter(file => file); | |
let body = '๐ Hi there!'; | |
if (missingFiles.length > 0) { | |
body += `\n\n๐จ It looks like you've added or modified some files without adding corresponding tests:\n${missingFiles.map(file => `- ${file}`).join('\n')}`; | |
body += `\n\n**However, there are some cases where you might not need to add or modify tests:**\n`; | |
body += `- The PR is solely focused on **refactoring** existing fully covered code without changing functionality.\n`; | |
body += `- The PR involves **documentation updates** or **comments** that do not affect code execution.\n`; | |
body += `- The PR is making **non-functional changes** like formatting, renaming variables, or improving code readability.\n`; | |
body += `- The untested files are pure entity classes or data models without business logic.\n`; | |
body += `- The existing tests already cover the changes, and no new tests are necessary.\n\n`; | |
body += `If any of these apply, feel free to dismiss this warning. ๐\n`; | |
} else { | |
body += `\n\nโ Great job! All modified files have corresponding tests.`; | |
} | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment