# 工作流的名称
name: DeleteOldRuns

# 工作流运行的名称
run-name: DeleteOldRuns

# 指定此工作流的触发器
on:
  schedule:
    - cron: "0 16 * * *" # 每天北京时间0点执行
  workflow_dispatch: # 可以手动执行
    inputs:
      hour_count:
        description: "You want to delete records from how many hours ago?"
        required: true
        default: "168"
        type: string
jobs:
  # 删除旧的运行记录
  DeleteOldRuns:
    name: DeleteOldRuns

    # 在最新版的Ubuntu系统上运行
    runs-on: ubuntu-latest

    steps:
      # 使用GitHub Actions提供的动作来检出代码库
      - name: Checkout Repository
        uses: actions/checkout@main

      # 配置Python环境
      - name: Set up Python
        uses: actions/setup-python@main
        with:
          python-version: "*"

      - name: Install dependencies
        run: |
          # 使用pip安装项目的依赖项
          pip install requests

      - name: Get Connection
        run: |
          # 获取连接信息
          url="https://ifconfig.me/all"
          response=$(curl -s -X GET "$url")
          echo "$response"

      - name: Run main program
        id: run_main_program
        env:
          HOUR_COUNT: ${{ github.event.inputs.hour_count }}
          GITHUB_ACTIONS: ${{github.actions}}
          GITHUB_TOKEN: ${{ secrets.GHTOKEN }}
          GITHUB_REF_NAME: ${{ github.ref_name }}
          GITHUB_EVENT_NAME: ${{ github.event_name }}
          GITHUB_ACTOR: ${{ github.actor }}
          GITHUB_ACTOR_ID: ${{ github.actor_id }}
          GITHUB_TRIGGERING_ACTOR: ${{ github.triggering_actor }}
          REPOSITORY_NAME: ${{ github.repository }}
          GITHUB_SHA: ${{ github.sha }}
          GITHUB_WORKFLOW: ${{ github.workflow }}
          GITHUB_RUN_NUMBER: ${{ github.run_number }}
          GITHUB_RUN_ID: ${{ github.run_id }}
        run: |
          # 运行主程序
          # 判断 GITHUB_TOKEN 是否存在，如果不存在就退出
          if [ -z "$GITHUB_TOKEN" ]; then
            exit 0
          fi

          # 获取北京时间
          export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')"
          echo "BEIJING_TIME=$BEIJING_TIME" >> $GITHUB_OUTPUT

          # 如果 hour_count 为空，则设置为 168
          if [[ -z "${{ github.event.inputs.hour_count }}" ]]; then
            # 设置步骤输出的 hour_count 为 168
            export HOUR_COUNT="168"
          # 如果 hour_count 不是正整数，则设置为 168
          elif ! [[ "${{ github.event.inputs.hour_count }}" =~ ^[0-9]+$ ]]; then
            # 设置步骤输出的 hour_count 为 168
            export HOUR_COUNT="168"
          # 如果 hour_count 是负数，则设置为 168
          elif [[ "${{ github.event.inputs.hour_count }}" -lt 0 ]]; then
            # 设置步骤输出的 hour_count 为 168
            export HOUR_COUNT="168"
          # 否则，使用输入的 hour_count
          else
            # 设置步骤输出的 hour_count 为输入的值
            export HOUR_COUNT="${{ github.event.inputs.hour_count }}"
          fi

          # 方便通过截图快速定位到用户
          echo "Hour Count: $HOUR_COUNT"
          echo "Branch Name: $GITHUB_REF_NAME"
          echo "Triggered By: $GITHUB_EVENT_NAME"
          echo "Initial Run By: $GITHUB_ACTOR"
          echo "Initial Run By ID: $GITHUB_ACTOR_ID"
          echo "Initiated Run By: $GITHUB_TRIGGERING_ACTOR"
          echo "Repository Name: $REPOSITORY_NAME"
          echo "Commit SHA: $GITHUB_SHA"
          echo "Workflow Name: $GITHUB_WORKFLOW"
          echo "Workflow Number: $GITHUB_RUN_NUMBER"
          echo "Workflow ID: $GITHUB_RUN_ID"
          echo "Beijing Time: $BEIJING_TIME"
          echo "Copyright © 2024 NianBroken. All rights reserved."
          echo "Author's website: https://www.nianbroken.top/"
          echo "Author's Repository URL: https://github.com/NianBroken/ZFCheckScores/"

          echo "------"
          # 运行主程序
          python scripts/delete_old_runs.py

      - name: Delete __pycache__ folder
        run: |
          # 删除__pycache__文件夹
          rm -rf __pycache__
