feat: load env from pydantic-settings
This commit is contained in:
		
							parent
							
								
									332efed675
								
							
						
					
					
						commit
						ae79299bf5
					
				|  | @ -15,6 +15,7 @@ from joint_teapot.teapot import Teapot | ||||||
| from joint_teapot.utils import joj3 | from joint_teapot.utils import joj3 | ||||||
| from joint_teapot.utils.logger import logger, set_logger | from joint_teapot.utils.logger import logger, set_logger | ||||||
| from joint_teapot.utils.main import first | from joint_teapot.utils.main import first | ||||||
|  | from joint_teapot.workers.joj import JOJ | ||||||
| 
 | 
 | ||||||
| if TYPE_CHECKING: | if TYPE_CHECKING: | ||||||
|     import focs_gitea |     import focs_gitea | ||||||
|  | @ -276,42 +277,33 @@ def joj3_all_env( | ||||||
|     set_settings(Settings(_env_file=env_path)) |     set_settings(Settings(_env_file=env_path)) | ||||||
|     set_logger(settings.stderr_log_level) |     set_logger(settings.stderr_log_level) | ||||||
|     logger.info(f"debug log to file: {settings.log_file_path}") |     logger.info(f"debug log to file: {settings.log_file_path}") | ||||||
|     submitter = os.getenv("GITHUB_ACTOR", "") |     env = joj3.Env() | ||||||
|     run_number = os.getenv("GITHUB_RUN_NUMBER", "") |  | ||||||
|     commit_hash = os.getenv("GITHUB_SHA", "") |  | ||||||
|     repository = os.getenv("GITHUB_REPOSITORY", "") |  | ||||||
|     if "" in ( |     if "" in ( | ||||||
|         submitter, |         env.github_actor, | ||||||
|         run_number, |         env.github_run_number, | ||||||
|         commit_hash, |         env.github_sha, | ||||||
|         repository, |         env.github_repository, | ||||||
|     ): |     ): | ||||||
|         logger.error("missing required env var") |         logger.error("missing required env var") | ||||||
|         raise Exit(code=1) |         raise Exit(code=1) | ||||||
|     run_id = os.getenv("JOJ3_RUN_ID", "") |     submitter_repo_name = env.github_repository.split("/")[-1] | ||||||
|     exercise_name = os.getenv("JOJ3_CONF_NAME", "") |     total_score = joj3.get_total_score(env.joj3_output_path) | ||||||
|     groups = os.getenv("JOJ3_GROUPS", "") |  | ||||||
|     commit_msg = os.getenv("JOJ3_COMMIT_MSG", "") |  | ||||||
|     force_quit_stage_name = os.getenv("JOJ3_FORCE_QUIT_STAGE_NAME") or "" |  | ||||||
|     score_file_path = os.getenv("JOJ3_OUTPUT_PATH", "") |  | ||||||
|     submitter_repo_name = (repository or "").split("/")[-1] |  | ||||||
|     total_score = joj3.get_total_score(score_file_path) |  | ||||||
|     res = { |     res = { | ||||||
|         "totalScore": total_score, |         "totalScore": total_score, | ||||||
|         "cappedTotalScore": ( |         "cappedTotalScore": ( | ||||||
|             total_score if max_total_score < 0 else min(total_score, max_total_score) |             total_score if max_total_score < 0 else min(total_score, max_total_score) | ||||||
|         ), |         ), | ||||||
|         "forceQuit": force_quit_stage_name != "", |         "forceQuit": env.joj3_force_quit_stage_name != "", | ||||||
|         "forceQuitStageName": force_quit_stage_name, |         "forceQuitStageName": env.joj3_force_quit_stage_name, | ||||||
|         "issue": 0, |         "issue": 0, | ||||||
|         "action": int(run_number) if run_number != "" else 0, |         "action": int(env.github_run_number), | ||||||
|         "sha": commit_hash, |         "sha": env.github_sha, | ||||||
|         "commitMsg": commit_msg, |         "commitMsg": env.joj3_commit_msg, | ||||||
|     } |     } | ||||||
|     gitea_actions_url = ( |     gitea_actions_url = ( | ||||||
|         f"https://{settings.gitea_domain_name}{settings.gitea_suffix}/" |         f"https://{settings.gitea_domain_name}{settings.gitea_suffix}/" | ||||||
|         + f"{settings.gitea_org_name}/{submitter_repo_name}/" |         + f"{settings.gitea_org_name}/{submitter_repo_name}/" | ||||||
|         + f"actions/runs/{run_number}" |         + f"actions/runs/{env.github_run_number}" | ||||||
|     ) |     ) | ||||||
|     submitter_repo_url = ( |     submitter_repo_url = ( | ||||||
|         f"https://{settings.gitea_domain_name}{settings.gitea_suffix}/" |         f"https://{settings.gitea_domain_name}{settings.gitea_suffix}/" | ||||||
|  | @ -320,18 +312,18 @@ def joj3_all_env( | ||||||
|     gitea_issue_url = "" |     gitea_issue_url = "" | ||||||
|     if not skip_result_issue: |     if not skip_result_issue: | ||||||
|         title, comment = joj3.generate_title_and_comment( |         title, comment = joj3.generate_title_and_comment( | ||||||
|             score_file_path, |             env.joj3_output_path, | ||||||
|             gitea_actions_url, |             gitea_actions_url, | ||||||
|             run_number, |             env.github_run_number, | ||||||
|             exercise_name, |             env.joj3_conf_name, | ||||||
|             submitter, |             env.github_actor, | ||||||
|             commit_hash, |             env.github_sha, | ||||||
|             submitter_in_issue_title, |             submitter_in_issue_title, | ||||||
|             run_id, |             env.joj3_run_id, | ||||||
|             max_total_score, |             max_total_score, | ||||||
|         ) |         ) | ||||||
|         title_prefix = joj3.get_title_prefix( |         title_prefix = joj3.get_title_prefix( | ||||||
|             exercise_name, submitter, submitter_in_issue_title |             env.joj3_conf_name, env.github_actor, submitter_in_issue_title | ||||||
|         ) |         ) | ||||||
|         joj3_issue: focs_gitea.Issue |         joj3_issue: focs_gitea.Issue | ||||||
|         issue: focs_gitea.Issue |         issue: focs_gitea.Issue | ||||||
|  | @ -391,25 +383,25 @@ def joj3_all_env( | ||||||
|             repo.git.reset("--hard", "origin/grading") |             repo.git.reset("--hard", "origin/grading") | ||||||
|             if not skip_scoreboard: |             if not skip_scoreboard: | ||||||
|                 joj3.generate_scoreboard( |                 joj3.generate_scoreboard( | ||||||
|                     score_file_path, |                     env.joj3_output_path, | ||||||
|                     submitter, |                     env.github_actor, | ||||||
|                     os.path.join(repo_path, scoreboard_filename), |                     os.path.join(repo_path, scoreboard_filename), | ||||||
|                     exercise_name, |                     env.joj3_conf_name, | ||||||
|                 ) |                 ) | ||||||
|                 tea.pot.git.add_commit( |                 tea.pot.git.add_commit( | ||||||
|                     grading_repo_name, |                     grading_repo_name, | ||||||
|                     [scoreboard_filename], |                     [scoreboard_filename], | ||||||
|                     ( |                     ( | ||||||
|                         f"joj3: update scoreboard for {exercise_name} by @{submitter} in " |                         f"joj3: update scoreboard for {env.joj3_conf_name} by @{env.github_actor} in " | ||||||
|                         f"{settings.gitea_org_name}/{submitter_repo_name}@{commit_hash}\n\n" |                         f"{settings.gitea_org_name}/{submitter_repo_name}@{env.github_sha}\n\n" | ||||||
|                         f"gitea actions link: {gitea_actions_url}\n" |                         f"gitea actions link: {gitea_actions_url}\n" | ||||||
|                         f"gitea issue link: {gitea_issue_url}\n" |                         f"gitea issue link: {gitea_issue_url}\n" | ||||||
|                         f"groups: {groups}\n" |                         f"groups: {env.joj3_groups}\n" | ||||||
|                     ), |                     ), | ||||||
|                 ) |                 ) | ||||||
|             if not skip_failed_table: |             if not skip_failed_table: | ||||||
|                 joj3.generate_failed_table( |                 joj3.generate_failed_table( | ||||||
|                     score_file_path, |                     env.joj3_output_path, | ||||||
|                     submitter_repo_name, |                     submitter_repo_name, | ||||||
|                     submitter_repo_url, |                     submitter_repo_url, | ||||||
|                     os.path.join(repo_path, failed_table_filename), |                     os.path.join(repo_path, failed_table_filename), | ||||||
|  | @ -419,11 +411,11 @@ def joj3_all_env( | ||||||
|                     grading_repo_name, |                     grading_repo_name, | ||||||
|                     [failed_table_filename], |                     [failed_table_filename], | ||||||
|                     ( |                     ( | ||||||
|                         f"joj3: update failed table for {exercise_name} by @{submitter} in " |                         f"joj3: update failed table for {env.joj3_conf_name} by @{env.github_actor} in " | ||||||
|                         f"{settings.gitea_org_name}/{submitter_repo_name}@{commit_hash}\n\n" |                         f"{settings.gitea_org_name}/{submitter_repo_name}@{env.github_sha}\n\n" | ||||||
|                         f"gitea actions link: {gitea_actions_url}\n" |                         f"gitea actions link: {gitea_actions_url}\n" | ||||||
|                         f"gitea issue link: {gitea_issue_url}\n" |                         f"gitea issue link: {gitea_issue_url}\n" | ||||||
|                         f"groups: {groups}\n" |                         f"groups: {env.joj3_groups}\n" | ||||||
|                     ), |                     ), | ||||||
|                 ) |                 ) | ||||||
|             push_info_list = tea.pot.git.push(grading_repo_name) |             push_info_list = tea.pot.git.push(grading_repo_name) | ||||||
|  | @ -467,17 +459,14 @@ def joj3_check_env( | ||||||
|     app.pretty_exceptions_enable = False |     app.pretty_exceptions_enable = False | ||||||
|     set_settings(Settings(_env_file=env_path)) |     set_settings(Settings(_env_file=env_path)) | ||||||
|     set_logger(settings.stderr_log_level) |     set_logger(settings.stderr_log_level) | ||||||
|     submitter = os.getenv("GITHUB_ACTOR", "") |     env = joj3.Env() | ||||||
|     repository = os.getenv("GITHUB_REPOSITORY", "") |  | ||||||
|     if "" in ( |     if "" in ( | ||||||
|         submitter, |         env.github_actor, | ||||||
|         repository, |         env.github_repository, | ||||||
|     ): |     ): | ||||||
|         logger.error("missing required env var") |         logger.error("missing required env var") | ||||||
|         raise Exit(code=1) |         raise Exit(code=1) | ||||||
|     exercise_name = os.getenv("JOJ3_CONF_NAME", "") |     submitter_repo_name = env.github_repository.split("/")[-1] | ||||||
|     groups = os.getenv("JOJ3_GROUPS", "") |  | ||||||
|     submitter_repo_name = repository.split("/")[-1] |  | ||||||
|     repo: Repo = tea.pot.git.get_repo(grading_repo_name) |     repo: Repo = tea.pot.git.get_repo(grading_repo_name) | ||||||
|     now = datetime.now(timezone.utc) |     now = datetime.now(timezone.utc) | ||||||
|     items = group_config.split(",") |     items = group_config.split(",") | ||||||
|  | @ -511,8 +500,8 @@ def joj3_check_env( | ||||||
|                 continue |                 continue | ||||||
|             d = match.groupdict() |             d = match.groupdict() | ||||||
|             if ( |             if ( | ||||||
|                 exercise_name != d["exercise_name"] |                 env.joj3_conf_name != d["exercise_name"] | ||||||
|                 or submitter != d["submitter"] |                 or env.github_actor != d["submitter"] | ||||||
|                 or submitter_repo_name != d["submitter_repo_name"] |                 or submitter_repo_name != d["submitter_repo_name"] | ||||||
|             ): |             ): | ||||||
|                 continue |                 continue | ||||||
|  | @ -539,7 +528,7 @@ def joj3_check_env( | ||||||
|                     continue |                     continue | ||||||
|             submit_count += 1 |             submit_count += 1 | ||||||
|         logger.info( |         logger.info( | ||||||
|             f"submitter {submitter} is submitting for the {submit_count + 1} time, " |             f"submitter {env.github_actor} is submitting for the {submit_count + 1} time, " | ||||||
|             f"{min(0, max_count - submit_count - 1)} time(s) remaining, " |             f"{min(0, max_count - submit_count - 1)} time(s) remaining, " | ||||||
|             f"group={name}, " |             f"group={name}, " | ||||||
|             f"time period={time_period} hour(s), " |             f"time period={time_period} hour(s), " | ||||||
|  | @ -548,7 +537,7 @@ def joj3_check_env( | ||||||
|         use_group = False |         use_group = False | ||||||
|         if name: |         if name: | ||||||
|             comment += f"keyword `{name}` " |             comment += f"keyword `{name}` " | ||||||
|             for group in groups or "": |             for group in env.joj3_groups or "": | ||||||
|                 if group.lower() == name.lower(): |                 if group.lower() == name.lower(): | ||||||
|                     use_group = True |                     use_group = True | ||||||
|                     break |                     break | ||||||
|  |  | ||||||
|  | @ -5,9 +5,26 @@ import os | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
| from typing import Any, Dict, List, Tuple | from typing import Any, Dict, List, Tuple | ||||||
| 
 | 
 | ||||||
|  | from pydantic_settings import BaseSettings | ||||||
|  | 
 | ||||||
| from joint_teapot.utils.logger import logger | from joint_teapot.utils.logger import logger | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | class Env(BaseSettings): | ||||||
|  |     github_actor: str = "" | ||||||
|  |     github_repository: str = "" | ||||||
|  |     github_sha: str = "" | ||||||
|  |     github_ref: str = "" | ||||||
|  |     github_workflow: str = "" | ||||||
|  |     github_run_number: str = "0" | ||||||
|  |     joj3_conf_name: str = "" | ||||||
|  |     joj3_groups: str = "" | ||||||
|  |     joj3_run_id: str = "" | ||||||
|  |     joj3_commit_msg: str = "" | ||||||
|  |     joj3_force_quit_stage_name: str = "" | ||||||
|  |     joj3_output_path: str = "" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def get_total_score(score_file_path: str) -> int: | def get_total_score(score_file_path: str) -> int: | ||||||
|     with open(score_file_path) as json_file: |     with open(score_file_path) as json_file: | ||||||
|         stages: List[Dict[str, Any]] = json.load(json_file) |         stages: List[Dict[str, Any]] = json.load(json_file) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user