From b676fb6aca3ec8a98ac6d216b69e40ccf8483e69 Mon Sep 17 00:00:00 2001 From: NianBroken Date: Sat, 3 Aug 2024 00:47:39 +0800 Subject: [PATCH] Optimize some code --- .github/workflows/delete_old_runs.yml | 18 +++---- .github/workflows/main.yml | 68 ++++++++++++++----------- main.py | 11 +++-- scripts/user_login.py | 71 ++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 46 deletions(-) diff --git a/.github/workflows/delete_old_runs.yml b/.github/workflows/delete_old_runs.yml index 9276d3a..5fabda8 100644 --- a/.github/workflows/delete_old_runs.yml +++ b/.github/workflows/delete_old_runs.yml @@ -24,13 +24,6 @@ jobs: runs-on: ubuntu-latest steps: - # 判断 GITHUB_TOKEN 是否存在,如果不存在就退出 - - name: Check Secret - run: | - if [ -z "$GITHUB_TOKEN" ]; then - exit 0 - fi - # 使用GitHub Actions提供的动作来检出代码库 - name: Checkout Repository uses: actions/checkout@main @@ -70,6 +63,11 @@ jobs: 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 @@ -92,9 +90,6 @@ jobs: export HOUR_COUNT="${{ github.event.inputs.hour_count }}" fi - python scripts/delete_old_runs.py - echo "------" - # 方便通过截图快速定位到用户 echo "Hour Count: $HOUR_COUNT" echo "Branch Name: $GITHUB_REF_NAME" @@ -112,6 +107,9 @@ jobs: 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__文件夹 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7f7bbc1..b0c92e6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -78,6 +78,40 @@ jobs: GITHUB_RUN_ID: ${{ github.run_id }} run: | # 运行主程序 + # 判断环境变量是否为空 + if [ -z "$URL" ]; then + echo "URL Secret is empty!" + echo "::error title=URL Secret is empty!" \ + "::Your 'URL' Secret is not filled with any" \ + "information, so your program will not work!" + Whether_Secret_is_empty=true + fi + if [ -z "$USERNAME" ]; then + echo "USERNAME Secret is empty!" + echo "::error title=USERNAME Secret is empty!" \ + "::Your 'USERNAME' Secret is not filled with any" \ + "information, so your program will not work!" + Whether_Secret_is_empty=true + fi + if [ -z "$PASSWORD" ]; then + echo "PASSWORD Secret is empty!" + echo "::error title=PASSWORD Secret is empty!" \ + "::Your 'PASSWORD' Secret is not filled with any" \ + "information, so your program will not work!" + Whether_Secret_is_empty=true + fi + if [ -z "$TOKEN" ]; then + echo "TOKEN Secret is empty!" + echo "::error title=TOKEN Secret is empty!" \ + "::Your 'TOKEN' Secret is not filled with any" \ + "information, so your program will not work!" + Whether_Secret_is_empty=true + fi + if [ "$Whether_Secret_is_empty" = true ]; then + echo "One or more Secrets are empty." + exit 1 + fi + # 判断是否需要强制推送信息 if [ -z "${{ github.event.inputs.force_push_message }}" ]; then export FORCE_PUSH_MESSAGE="False" @@ -86,34 +120,6 @@ jobs: fi export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')" echo "BEIJING_TIME=$BEIJING_TIME" >> $GITHUB_OUTPUT - python main.py - echo "------" - - # 判断环境变量是否为空 - if [ -z "$URL" ]; then - echo "URL Secret is empty!" - echo "::error title=URL Secret is empty!" \ - "::Your 'URL' Secret is not filled with any" \ - "information, so your program will not work!" - fi - if [ -z "$USERNAME" ]; then - echo "USERNAME Secret is empty!" - echo "::error title=USERNAME Secret is empty!" \ - "::Your 'USERNAME' Secret is not filled with any" \ - "information, so your program will not work!" - fi - if [ -z "$PASSWORD" ]; then - echo "PASSWORD Secret is empty!" - echo "::error title=PASSWORD Secret is empty!" \ - "::Your 'PASSWORD' Secret is not filled with any" \ - "information, so your program will not work!" - fi - if [ -z "$TOKEN" ]; then - echo "TOKEN Secret is empty!" - echo "::error title=TOKEN Secret is empty!" \ - "::Your 'TOKEN' Secret is not filled with any" \ - "information, so your program will not work!" - fi # 定义 mask_middle 函数,接收一个字符串作为输入参数 mask_middle() { @@ -150,6 +156,7 @@ jobs: # 组合协议、匿名化后的域名和路径,输出结果 echo "${protocol}${anonymized_domain}${path}" } + LOGINURL="xtgl/login_slogin.html" # 方便通过截图快速定位到用户 @@ -172,6 +179,9 @@ jobs: echo "Author's website: https://www.nianbroken.top/" echo "Author's Repository URL: https://github.com/NianBroken/ZFCheckScores/" + echo "------" + python main.py + - name: Delete __pycache__ folder run: | # 删除__pycache__文件夹 @@ -183,4 +193,4 @@ jobs: # 将更改强制推送到运行分支 git add . git commit -m "Update from GitHub Actions" || true - git push origin $GITHUB_REF_NAME --force + git push origin $GITHUB_REF_NAME --force \ No newline at end of file diff --git a/main.py b/main.py index 48f6a77..3484518 100644 --- a/main.py +++ b/main.py @@ -156,7 +156,7 @@ workflow_info = ( f"Beijing Time:{beijing_time}" ) -copyright = "Copyright © 2024 NianBroken. All rights reserved." +copyright_text = "Copyright © 2024 NianBroken. All rights reserved." # 第一次运行时的提示文本 first_run_text = ( @@ -174,7 +174,7 @@ integrated_send_info = ( f"{integrated_grade_info}\n" f"{selected_courses_filtering}\n" f"{workflow_info if github_actions else current_time}\n" - f"{copyright}" + f"{copyright_text}" ) # 整合首次运行时需要使用到的所有信息 @@ -234,10 +234,11 @@ with open(info_file_path, "r") as info_file: if run_log: print(run_log) - # 整合JobSummary信息 - github_step_summary_run_log = f"# 正方教务管理系统成绩推送\n{run_log}\n{workflow_info}\n{copyright}" - + # 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中 if github_actions: + # 整合JobSummary信息 + github_step_summary_run_log = f"# 正方教务管理系统成绩推送\n{run_log}\n{workflow_info}\n{copyright_text}" + # 将任意个数的换行替换为两个换行 github_step_summary_run_log = re.sub("\n+", "\n\n", github_step_summary_run_log) diff --git a/scripts/user_login.py b/scripts/user_login.py index df3fa91..065431f 100644 --- a/scripts/user_login.py +++ b/scripts/user_login.py @@ -1,7 +1,62 @@ +import os +import re import sys from pprint import pprint from .zfn_api import Client +# 从环境变量中提取教务系统的URL、用户名、密码和TOKEN等信息 +force_push_message = os.environ.get("FORCE_PUSH_MESSAGE") +github_actions = os.environ.get("GITHUB_ACTIONS") +github_ref_name = os.environ.get("GITHUB_REF_NAME") +github_event_name = os.environ.get("GITHUB_EVENT_NAME") +github_actor = os.environ.get("GITHUB_ACTOR") +github_actor_id = os.environ.get("GITHUB_ACTOR_ID") +github_triggering_actor = os.environ.get("GITHUB_TRIGGERING_ACTOR") +repository_name = os.environ.get("REPOSITORY_NAME") +github_sha = os.environ.get("GITHUB_SHA") +github_workflow = os.environ.get("GITHUB_WORKFLOW") +github_run_number = os.environ.get("GITHUB_RUN_NUMBER") +github_run_id = os.environ.get("GITHUB_RUN_ID") +beijing_time = os.environ.get("BEIJING_TIME") +github_step_summary = os.environ.get("GITHUB_STEP_SUMMARY") + +# 工作流信息 +workflow_info = ( + f"------\n" + f"工作流信息:\n" + f"Force Push Message:{force_push_message}\n" + f"Branch Name:{github_ref_name}\n" + f"Triggered By:{github_event_name}\n" + f"Initial Run By:{github_actor}\n" + f"Initial Run By ID:{github_actor_id}\n" + f"Initiated Run By:{github_triggering_actor}\n" + f"Repository Name:{repository_name}\n" + f"Commit SHA:{github_sha}\n" + f"Workflow Name:{github_workflow}\n" + f"Workflow Number:{github_run_number}\n" + f"Workflow ID:{github_run_id}\n" + f"Beijing Time:{beijing_time}" +) + +copyright_text = "Copyright © 2024 NianBroken. All rights reserved." + + +def write_github_summary(run_log, lgn_code): + summary_log = ( + f"# 正方教务管理系统成绩推送\n" + f"你因 **{run_log}** 原因而登录失败,错误代码为 **{lgn_code}**。\n" + f"若你不明白或不理解为什么登录失败,请到上游仓库的 " + f"[Issue](https://github.com/NianBroken/ZFCheckScores/issues/new 'Issue') 中寻求帮助。\n" + f"{workflow_info}\n" + f"{copyright_text}" + ) + # 将任意个数的换行替换为两个换行 + summary_log = re.sub("\n+", "\n\n", summary_log) + + # 将登录需要验证码写入到 GitHub Actions 的环境文件中 + with open(github_step_summary, "w", encoding="utf-8") as file: + file.write(summary_log) + def login(url, username, password): cookies = {} @@ -23,7 +78,13 @@ def login(url, username, password): if cookies == {}: lgn = student_client.login(username, password) if lgn["code"] == 1001: - sys.exit("你的教务系统登录时需要验证码,因此你无法使用此项目。") + run_log = "登录需要验证码" + + # 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中 + if github_actions: + write_github_summary(run_log, lgn["code"]) + + sys.exit(f"你因{run_log}原因而登录失败,错误代码为{lgn['code']}。") """ verify_data = lgn["data"] with open(os.path.abspath("kaptcha.png"), "wb") as pic: @@ -37,6 +98,12 @@ def login(url, username, password): """ elif lgn["code"] != 1000: pprint(lgn) - sys.exit("登录失败") + run_log = lgn["msg"] + + # 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中 + if github_actions: + write_github_summary(run_log, lgn["code"]) + + sys.exit(1) return student_client