name: CheckScores

on:
  schedule:
    - cron: "*/30 * * * *" # 每30分钟执行一次
  workflow_dispatch:
    inputs:
      force_push_message:
        description: "是否强制推送信息"
        required: true
        default: "False"
        type: choice
        options:
          - "True"
          - "False"

jobs:
  CheckScores:
    # 在最新版的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: "*"

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

      # 配置Git用户信息
      - name: Configure Git
        run: |
          git config --global user.email "actions@github.com"
          git config --global user.name "GitHub Actions"

      # 添加上游仓库作为远程仓库，并获取上游仓库的main分支的最新更改
      - name: Fetch upstream changes
        run: |
          git remote add upstream https://github.com/NianBroken/ZFCheckScores.git
          git fetch upstream main

      # 创建一个备份目录，并将当前目录中的所有.txt文件复制到备份目录
      - name: Backup local .txt files
        run: |
          mkdir -p backup
          cp -r *.txt backup/ || echo "No .txt files to backup"

      # 将上游main分支的更改强制推送到当前main分支
      - name: Force push changes from upstream to current main branch
        run: |
          git checkout main
          git reset --hard upstream/main
          git push origin main --force || (for i in {1..10}; do
          echo "------" &&
          echo "你需要手动同步上游分支" &&
          echo "同步方法如下：" &&
          echo "1. 打开你的仓库首页：https://github.com/${{ github.repository }}" &&
          echo "2. 点击 Sync fork"&&
          echo "3. 点击 Update branch 或 Discard xxx commits";
          done; exit 1)

      # 从备份目录将.txt文件还原到当前目录，然后删除备份目录
      - name: Restore local .txt files and delete backup
        run: |
          cp -r backup/*.txt . || echo "No .txt files to restore"
          rm -rf backup

      # 运行主程序main.py
      - name: Run main.py
        env:
          FORCE_PUSH_MESSAGE: ${{ github.event.inputs.force_push_message }}
          URL: ${{ secrets.URL }}
          USERNAME: ${{ secrets.USERNAME }}
          PASSWORD: ${{ secrets.PASSWORD }}
          TOKEN: ${{ secrets.TOKEN }}
          GITHUB_EVENT_NAME: ${{ github.event_name }}
          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: |
          if [ -z "${{ github.event.inputs.force_push_message }}" ]; then
            export FORCE_PUSH_MESSAGE="False"
          else
            export FORCE_PUSH_MESSAGE="${{ github.event.inputs.force_push_message }}"
          fi
          export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')"
          python main.py
          echo "------"
          # 判断环境变量是否为空
          if [ -z "$URL" ]; then
            echo "URL secret is empty!"
          fi
          if [ -z "$USERNAME" ]; then
            echo "USERNAME secret is empty!"
          fi
          if [ -z "$PASSWORD" ]; then
            echo "PASSWORD secret is empty!"
          fi
          if [ -z "$TOKEN" ]; then
            echo "TOKEN secret is empty!"
          fi
          # 方便通过截图快速定位到用户
          echo "Force Push Message: $FORCE_PUSH_MESSAGE"
          echo "Triggered By: ${{ github.event_name }}"
          echo "Run By: ${{ github.triggering_actor }}"
          echo "Repository Name: ${{ github.repository }}"
          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" | tee time.txt

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

      # 将当前目录中的所有更改添加到Git的暂存区
      - name: Add changes
        run: |
          git add .

      # 提交更改到Git仓库
      - name: Commit changes
        run: |
          git commit -m "Update from GitHub Actions" || true

      # 将更改强制推送到main分支
      - name: Force push changes to main branch
        run: |
          git push origin main --force
