Skip to content

Harwest Submissions #16

Harwest Submissions

Harwest Submissions #16

Workflow file for this run

name: Harwest Submissions
on:
# Daily schedule - 11:35 PM BDT (5:35 PM UTC)
schedule:
- cron: '35 17 * * *' # Runs daily at 5:35 PM UTC (11:35 PM BDT)
# Allow manual trigger
workflow_dispatch:
inputs:
platform:
description: 'Platform to harvest (codeforces, atcoder, or all)'
required: false
default: 'all'
full_scan:
description: 'Perform full scan'
required: false
type: boolean
default: false
env:
PYTHON_VERSION: '3.9'
jobs:
check-activity:
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
should_run: ${{ steps.activity.outputs.should_run }}
days_since_commit: ${{ steps.activity.outputs.days_since_commit }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history to check activity
- name: Check repository activity
id: activity
run: |
# Get the date of the last commit (excluding workflow commits)
LAST_COMMIT_DATE=$(git log -1 --format=%ct --author-date-order --all --invert-grep --grep="^Harwest Submissions\|^GitHub Actions" || git log -1 --format=%ct)
CURRENT_DATE=$(date +%s)
# Calculate days since last commit
DAYS_DIFF=$(( (CURRENT_DATE - LAST_COMMIT_DATE) / 86400 ))
echo "days_since_commit=$DAYS_DIFF" >> $GITHUB_OUTPUT
echo "📅 Days since last commit: $DAYS_DIFF"
# If this is a scheduled run (not manual)
if [ "${{ github.event_name }}" = "schedule" ]; then
# If more than 30 days, only run on 1st of the month
if [ $DAYS_DIFF -gt 30 ]; then
CURRENT_DAY=$(date +%d)
if [ "$CURRENT_DAY" = "01" ]; then
echo "should_run=true" >> $GITHUB_OUTPUT
echo "🗓️ Running monthly check (1st of month) - Repository inactive for $DAYS_DIFF days"
else
echo "should_run=false" >> $GITHUB_OUTPUT
echo "⏭️ Skipping - Repository inactive for $DAYS_DIFF days, will run on 1st of month"
fi
else
echo "should_run=true" >> $GITHUB_OUTPUT
echo "✅ Running daily check - Repository active (last commit $DAYS_DIFF days ago)"
fi
else
# Manual trigger always runs
echo "should_run=true" >> $GITHUB_OUTPUT
echo "▶️ Manual trigger - Running regardless of activity"
fi
harvest:
needs: check-activity
if: needs.check-activity.outputs.should_run == 'true'
runs-on: ubuntu-latest
permissions:
contents: write # Required to push changes
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Configure Git
run: |
# Try to get author info from config/users.json, fallback to defaults
if [ -f config/users.json ]; then
AUTHOR_NAME=$(python -c "import json; data=json.load(open('config/users.json')); print(data.get('name', 'GitHub Actions'))" 2>/dev/null || echo "GitHub Actions")
AUTHOR_EMAIL=$(python -c "import json; data=json.load(open('config/users.json')); print(data.get('email', '[email protected]'))" 2>/dev/null || echo "[email protected]")
else
AUTHOR_NAME="GitHub Actions"
AUTHOR_EMAIL="[email protected]"
fi
echo "🔧 Configuring git with author: $AUTHOR_NAME <$AUTHOR_EMAIL>"
git config --global user.name "$AUTHOR_NAME"
git config --global user.email "$AUTHOR_EMAIL"
echo "✅ Git configured successfully"
- name: Check configuration
id: check_config
run: |
if [ ! -f config/users.json ]; then
echo "❌ Error: config/users.json not found"
echo "Please create config/users.json with your usernames"
exit 1
fi
# Check if any usernames are configured
CODEFORCES_COUNT=$(python3 -c "import json; data=json.load(open('config/users.json')); print(len(data.get('codeforces', [])))")
ATCODER_COUNT=$(python3 -c "import json; data=json.load(open('config/users.json')); print(len(data.get('atcoder', [])))")
echo "📊 Configuration:"
echo " Codeforces users: $CODEFORCES_COUNT"
echo " AtCoder users: $ATCODER_COUNT"
if [ "$CODEFORCES_COUNT" -eq 0 ] && [ "$ATCODER_COUNT" -eq 0 ]; then
echo "⚠️ Warning: No usernames configured in config/users.json"
echo "has_users=false" >> $GITHUB_OUTPUT
else
echo "has_users=true" >> $GITHUB_OUTPUT
fi
- name: Set environment variables
if: steps.check_config.outputs.has_users == 'true'
run: |
# Set environment variables for automation
echo "SUBMISSIONS_DIR=${{ vars.SUBMISSIONS_DIR || './submissions' }}" >> $GITHUB_ENV
echo "GIT_AUTHOR_NAME=${{ vars.GIT_AUTHOR_NAME || 'GitHub Actions' }}" >> $GITHUB_ENV
echo "GIT_AUTHOR_EMAIL=${{ vars.GIT_AUTHOR_EMAIL || '[email protected]' }}" >> $GITHUB_ENV
echo "GITHUB_REPOSITORY_URL=${{ github.server_url }}/${{ github.repository }}.git" >> $GITHUB_ENV
echo "GITHUB_SERVER_URL=${{ github.server_url }}" >> $GITHUB_ENV
echo "GITHUB_REPOSITORY=${{ github.repository }}" >> $GITHUB_ENV
echo "GITHUB_REF_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
echo "✅ Environment variables configured for automation"
- name: Harvest Codeforces submissions
if: steps.check_config.outputs.has_users == 'true' && (github.event.inputs.platform == 'codeforces' || github.event.inputs.platform == 'all' || github.event.inputs.platform == '')
run: |
# Always do full scan for scheduled runs, check manual input for manual triggers
FULL_SCAN_FLAG="--full-scan"
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ github.event.inputs.full_scan }}" != "true" ]; then
FULL_SCAN_FLAG=""
fi
echo "⛏️ Harvesting Codeforces submissions (full scan: $([ -n \"$FULL_SCAN_FLAG\" ] && echo 'yes' || echo 'no'))..."
python3 -m harwest codeforces $FULL_SCAN_FLAG --auto
- name: Harvest AtCoder submissions
if: steps.check_config.outputs.has_users == 'true' && (github.event.inputs.platform == 'atcoder' || github.event.inputs.platform == 'all' || github.event.inputs.platform == '')
run: |
# Always do full scan for scheduled runs, check manual input for manual triggers
FULL_SCAN_FLAG="--full-scan"
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ github.event.inputs.full_scan }}" != "true" ]; then
FULL_SCAN_FLAG=""
fi
echo "⛏️ Harvesting AtCoder submissions (full scan: $([ -n \"$FULL_SCAN_FLAG\" ] && echo 'yes' || echo 'no'))..."
python3 -m harwest atcoder $FULL_SCAN_FLAG --auto
- name: Push changes
if: steps.check_config.outputs.has_users == 'true'
run: |
# Check if there are changes to commit
if [ -n "$(git status --porcelain)" ]; then
echo "📤 Pushing changes to repository..."
git add .
if git commit -m "Update submissions - $(date +'%Y-%m-%d %H:%M:%S UTC')"; then
if git push; then
echo "✅ Changes pushed successfully"
else
echo "⚠️ Warning: Failed to push changes"
exit 1
fi
else
echo "⚠️ Warning: Failed to commit changes"
exit 1
fi
else
echo "ℹ️ No new submissions found"
fi
- name: Log schedule status
if: always()
run: |
DAYS=${{ needs.check-activity.outputs.days_since_commit }}
if [ "$DAYS" -gt 30 ]; then
echo "📅 Schedule Status: MONTHLY (inactive for $DAYS days)"
echo "Next run: 1st of next month"
else
echo "📅 Schedule Status: DAILY (last activity $DAYS days ago)"
echo "Next run: Tomorrow at midnight UTC"
fi