name: "Clean old cloudflare pages preview urls and nightly build"
on:
  schedule:
    - cron: "0 0 * * *" # every day at 00:00
  workflow_dispatch:

jobs:
  clean-cloudflare-pages-preview-urls:
    strategy:
      matrix:
        project: ["jan", "nitro"]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-python@v4
        with:
          python-version: '3.x'
      - name: install requests
        run: |
          python3 -m pip install requests pytz tqdm
      - name: Python Inline script
        uses: jannekem/run-python-script-action@v1
        with: 
          script: |
            import requests
            from datetime import datetime, UTC
            from pytz import timezone
            from tqdm import tqdm
            
            # Configuration
            endpoint = "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/${{ matrix.project }}/deployments"
            expiration_days = 3
            headers = {
                "Content-Type": "application/json;charset=UTF-8",
                "Authorization": "Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}"
            }
            utc_tz = timezone('UTC')

            # Fetch the list of deployments
            response = requests.get(endpoint, headers=headers)
            deployments = response.json()

            for deployment in tqdm(deployments['result']):
                # Calculate the age of the deployment
                created_on = datetime.strptime(deployment['created_on'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=utc_tz)
                if (datetime.now(UTC) - created_on).days > expiration_days:
                    # Delete the deployment
                    delete_response = requests.delete(f"{endpoint}/{deployment['id']}", headers=headers)
                    if delete_response.status_code == 200:
                        print(f"Deleted deployment: {deployment['id']}")
                    else:
                        print(f"Failed to delete deployment: {deployment['id']}")
  clean-cloudflare-r2:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - name: install-aws-cli-action
        uses: unfor19/install-aws-cli-action@v1
      - name: Delete object older than 10 days
        run: |
          # Get the list of objects in the 'latest' folder
            OBJECTS=$(aws s3api list-objects --bucket ${{ secrets.CLOUDFLARE_R2_BUCKET_NAME }} --prefix "latest/" --query 'Contents[?LastModified<`'$(date -d "$current_date -10 days" -u +"%Y-%m-%dT%H:%M:%SZ")'`].{Key: Key}' --endpoint-url https://${{ secrets.CLOUDFLARE_ACCOUNT_ID }}.r2.cloudflarestorage.com | jq -c .)
          
          # Create a JSON file for the delete operation
          echo "{\"Objects\": $OBJECTS, \"Quiet\": false}" > delete.json
          
          # Delete the objects
          echo q | aws s3api delete-objects --bucket ${{ secrets.CLOUDFLARE_R2_BUCKET_NAME }} --delete file://delete.json --endpoint-url https://${{ secrets.CLOUDFLARE_ACCOUNT_ID }}.r2.cloudflarestorage.com
          
          # Remove the JSON file
          rm delete.json
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: auto
          AWS_EC2_METADATA_DISABLED: "true"
