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

T6789: parametrized the path for unused import check and used merge ref for checkout #59

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 55 additions & 45 deletions .github/workflows/check-unused-imports.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: Check for unused imports using Pylint
on:
workflow_call:
inputs:
check_file_path:
description: 'Path to check for unused py imports'
required: false
default: 'src/migration-scripts/'
type: string
jobs:
check-unused-imports:
runs-on: ubuntu-latest
Expand All @@ -9,63 +15,67 @@ jobs:
contents: read
steps:

- name: Checkout pull request head
- name: Checkout pull request merge ref
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}

- uses: dorny/paths-filter@v3
id: py-changes
ref: refs/pull/${{ github.event.pull_request.number }}/merge
repository: ${{ github.event.pull_request.base.repo.full_name }}

- name: Get changed files
id: changed-files
uses: actions/github-script@v4
with:
filters: |
src:
- 'src/**'
script: |
const prNumber = context.payload.pull_request.number;
const { data: files } = await github.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
const checkFilePath = '${{ inputs.check_file_path }}';
const changedFiles = files.map(file => file.filename).filter(file => file.startsWith(checkFilePath));
console.log("Changed files:", changedFiles);
return { result: changedFiles.join(',') };

- name: Get changed py files
run: |
echo "Changed files: ${{ steps.changed-files.outputs.result }}"
py_files=$(echo ${{ steps.changed-files.outputs.result }} | sed 's/{result://; s/}//' | tr ',' ' ')
echo "CHANGED_PY_FILES=$py_files" >> $GITHUB_ENV

- name: Set up Python
if: steps.py-changes.outputs.src == 'true'
if: ${{ env.CHANGED_PY_FILES != '' }}
uses: actions/setup-python@v3
with:
python-version: 3.11

- name: Install dependencies
if: steps.py-changes.outputs.src == 'true'
if: ${{ env.CHANGED_PY_FILES != '' }}
run: |
python -m pip install --upgrade pip
pip install pylint

- name: Check unused imports (pylint)
if: steps.py-changes.outputs.src == 'true'
run: |
pylint_files=$(git ls-files *.py src/migration-scripts)
if: ${{ env.CHANGED_PY_FILES != '' }}
run: |
set +e
pylint --disable=all --enable=W0611 $pylint_files

# - name: Analysing the code with pylint
# if: steps.py-changes.outputs.src == 'true'
# run: |
# pylint_files=$(git ls-files *.py src/migration-scripts)
# set +e
# output=$(pylint --disable=all --enable=W0611 --output-format=json $pylint_files 2>&1)
# exit_code=$?
# set -e
# if [[ $exit_code -eq 0 ]]; then
# echo "error_message=No issues in unused-imports" >> $GITHUB_ENV
# echo "No issues in unused-imports"
# else
# formatted_output=$(echo "$output" | jq -r '.[] | "\(.type): \(.message) in \(.path):\(.line)"')
# echo "$formatted_output"
# formatted_env_output=$(echo "$formatted_output" | tr '\n' '. ')
# echo "error_message=${formatted_env_output}" >> $GITHUB_ENV
# exit 1
# fi

# - name: Add PR comment
# if: always() && steps.py-changes.outputs.src == 'true'
# uses: mshick/add-pr-comment@v2
# with:
# message: |-
# ${{ env.error_message == 'No issues in unused-imports' && '👍' || '❌' }}
# ${{ env.error_message }}
# message-id: "UNUSED_IMPORTS_VALIDATION"
# allow-repeats: false
output=$(pylint --disable=all --enable=W0611 $CHANGED_PY_FILES)
exit_code=$?
set -e
echo "$output"
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
message=":white_check_mark: No issues found in *unused-imports* check."
if [[ $exit_code -ne 0 ]]; then
message=":x: Issues found in *unused-imports* check. "
fi
echo "WORKFLOW_MESSAGE=${message}. Please refer the [workflow run]($run_url)" >> $GITHUB_ENV
exit $exit_code

- name: Add PR comment
if: ${{ always() && env.WORKFLOW_MESSAGE != '' }}
uses: mshick/add-pr-comment@v2
with:
message: |-
${{ env.WORKFLOW_MESSAGE }}
message-id: "UNUSED_IMPORTS_VALIDATION"
allow-repeats: false