From 7352283a236986f3d94b915db656155b9645498c Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Sun, 26 May 2024 01:52:57 +0800 Subject: [PATCH] Add GHA workflow to monitor for PDF changes --- .github/workflows/pdf-monitor.yml | 56 +++++++++++++++++++++++++++++++ main.py | 4 ++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/pdf-monitor.yml diff --git a/.github/workflows/pdf-monitor.yml b/.github/workflows/pdf-monitor.yml new file mode 100644 index 0000000..ebee37a --- /dev/null +++ b/.github/workflows/pdf-monitor.yml @@ -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 \ No newline at end of file diff --git a/main.py b/main.py index 6f43842..8541074 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +import sys + import matplotlib.pyplot as plt import numpy as np @@ -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']