Skip to content

Commit

Permalink
Merge pull request #2 from ruchernchong/1-run-python-script-when-a-pd…
Browse files Browse the repository at this point in the history
…f-file-is-added-or-updated

Add GHA workflow to monitor for PDF changes
  • Loading branch information
ruchernchong authored May 25, 2024
2 parents 9e311cc + 7352283 commit b54e148
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
56 changes: 56 additions & 0 deletions .github/workflows/pdf-monitor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: PDF Monitor Workflow

on:
push:
paths:
- '**/*.pdf'

jobs:
run-python-script:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.12
cache: pip
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Find Changed PDF Files
id: find_pdfs
run: |
if [ $(git rev-parse --is-shallow-repository) = true ]; then
# For shallow clones, fetch the full history
git fetch --unshallow
fi
if [ $(git rev-list --count HEAD) -eq 1 ]; then
# If only one commit exists, list all PDFs
pdf_files=$(git ls-files '*.pdf')
else
# For subsequent commits, list changed PDFs
pdf_files=$(git diff --name-only HEAD^ HEAD | grep '.pdf$')
fi
echo "pdf_files=$pdf_files" >> $GITHUB_ENV
- name: Run Python Script
if: env.pdf_files != ''
run: |
for pdf_file in ${{ env.pdf_files }}; do
python main.py "$pdf_file"
done
- name: Commit and Push Changes
if: ${{ env.pdf_files != '' }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add .
if git diff --cached --quiet; then
echo "No changes to commit"
else
git commit -m "Automated commit of changes made by PDF Monitor Workflow"
git push
fi
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import matplotlib.pyplot as plt
import numpy as np

Expand All @@ -6,7 +8,7 @@
from utils.replace_text import replace_text

# Step 1: Extract data from the PDF
data = read_pdf.extract_data_from_pdf('Rev_by_Mkt_Qtrly_Trend_Q125.pdf')
data = read_pdf.extract_data_from_pdf(sys.argv[1])

# Step 2: Assign data to variables
quarters = data['quarters']
Expand Down

0 comments on commit b54e148

Please sign in to comment.