Coverage for gitlab_projects.py: 78%
94 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
1"""Perform actions on gitlab projects."""
3import sys
4from argparse import ArgumentDefaultsHelpFormatter, Namespace
5from pathlib import Path
7from sel_tools.code_evaluation.evaluate_code import evaluate_code
8from sel_tools.code_evaluation.jobs.factory import EvaluationJobFactory
9from sel_tools.code_evaluation.report import write_evaluation_reports
10from sel_tools.diff_creation.create_diff import create_diff
11from sel_tools.diff_creation.report import write_diff_reports
12from sel_tools.file_export.export_item import export_items
13from sel_tools.file_parsing.slide_parser import get_tasks_from_slides
14from sel_tools.gitlab_api.add_user import add_users
15from sel_tools.gitlab_api.comment_issue import comment_issues
16from sel_tools.gitlab_api.create_commit import commit_changes, upload_files
17from sel_tools.gitlab_api.create_issue import create_issues
18from sel_tools.gitlab_api.fetch_repo import fetch_repos
19from sel_tools.utils.args import ArgumentParserFactory
20from sel_tools.utils.comment import Comment
21from sel_tools.utils.task import configure_tasks
24def edit_create_issues(args: Namespace) -> None:
25 """Default action for create_issues subcommand."""
26 tasks = get_tasks_from_slides(Path(args.issue_md_slides.name))
27 tasks = configure_tasks(tasks, args.due_date, args.homework_number)
28 create_issues(tasks, Path(args.student_repo_info_file.name), args.gitlab_token)
31def edit_comment_issue(args: Namespace) -> None:
32 """Default action for comment_issue subcommand."""
33 comment = Comment.create(args.issue_number, args.message, args.state_event)
34 comment_issues(comment, Path(args.student_repo_info_file.name), args.gitlab_token)
37def edit_fetch_code(args: Namespace) -> None:
38 """Default action for fetch_code subcommand."""
39 fetch_repos(args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token)
42def edit_evaluate_code(args: Namespace) -> None:
43 """Default action for evaluate_code subcommand."""
44 gitlab_projects = fetch_repos(args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token)
45 factory = EvaluationJobFactory.load_factory_from_file(args.job_factory)
46 evaluation_reports = evaluate_code(factory, gitlab_projects, args.homework_number, args.evaluation_date)
47 write_evaluation_reports(evaluation_reports, f"homework-{args.homework_number}-report")
48 diff_reports = create_diff(
49 [project.local_path for project in gitlab_projects],
50 args.date_last_homework,
51 args.evaluation_date,
52 )
53 write_diff_reports(diff_reports, f"homework-{args.homework_number}-diff")
56def edit_upload_files(args: Namespace) -> None:
57 """Default action for upload_files subcommand."""
58 upload_files(args.source_path, Path(args.student_repo_info_file.name), args.gitlab_token)
61def edit_commit_changes(args: Namespace) -> None:
62 """Default action for commit_changes subcommand."""
63 gitlab_projects = fetch_repos(args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token)
64 student_repos = [project.local_path for project in gitlab_projects]
65 export_items(args.source_path, student_repos, args.keep_solutions)
66 commit_changes(student_repos, args.message)
69def edit_add_users(args: Namespace) -> None:
70 """Default action for add_users subcommand."""
71 add_users(
72 Path(args.student_repo_info_file.name),
73 Path(args.student_group_info_file.name),
74 args.gitlab_token,
75 )
78def parse_arguments(arguments: list[str]) -> Namespace:
79 """Parse CLI arguments."""
80 # pylint: disable=too-many-locals
82 parser = ArgumentParserFactory.default_parser(__doc__).parser
83 subparsers = parser.add_subparsers(title="actions", dest="actions", help="sub-command help", required=True)
85 # Common arguments
86 factory = ArgumentParserFactory.parent_parser()
87 factory.add_student_repo_info_file()
88 factory.add_gitlab_token()
90 # Create issues parser
91 create_issue_factory = factory.copy()
92 create_issue_factory.add_issue_md_slide()
93 create_issue_factory.add_homework_number()
94 create_issue_factory.add_due_date()
95 parser_issues = subparsers.add_parser(
96 "create_issues",
97 parents=[create_issue_factory.parser],
98 formatter_class=ArgumentDefaultsHelpFormatter,
99 description="Create Gitlab issues from homework slides",
100 help="Create Gitlab issues from homework slides",
101 )
102 parser_issues.set_defaults(func=edit_create_issues)
104 # Comment issues parser
105 comment_issue_factory = factory.copy()
106 comment_issue_factory.add_issue_number()
107 comment_issue_factory.add_message("Message or path to a .md file")
108 comment_issue_factory.add_state_event()
109 parser_comment = subparsers.add_parser(
110 "comment_issue",
111 parents=[comment_issue_factory.parser],
112 formatter_class=ArgumentDefaultsHelpFormatter,
113 description="Comment to Gitlab issues via message or markdown slides",
114 help="Comment to Gitlab issues via message or markdown slides",
115 )
116 parser_comment.set_defaults(func=edit_comment_issue)
118 # Fetch code parser
119 fetch_code_factory = factory.copy()
120 fetch_code_factory.add_workspace()
121 parser_fetch = subparsers.add_parser(
122 "fetch_code",
123 parents=[fetch_code_factory.parser],
124 formatter_class=ArgumentDefaultsHelpFormatter,
125 description="Fetch (clone or pull) Gitlab repositories",
126 help="Fetch (clone or pull) Gitlab repositories",
127 )
128 parser_fetch.set_defaults(func=edit_fetch_code)
130 # Evaluate code parser
131 evaluate_code_factory = factory.copy()
132 evaluate_code_factory.add_homework_number()
133 evaluate_code_factory.add_job_factory_path()
134 evaluate_code_factory.add_workspace()
135 evaluate_code_factory.add_date_sine_last_homework()
136 evaluate_code_factory.add_evaluation_date()
137 parser_evaluate = subparsers.add_parser(
138 "evaluate_code",
139 parents=[evaluate_code_factory.parser],
140 formatter_class=ArgumentDefaultsHelpFormatter,
141 description="Fetch (clone or pull) Gitlab repositories and evaluate code",
142 help="Fetch (clone or pull) Gitlab repositories and evaluate code",
143 )
144 parser_evaluate.set_defaults(func=edit_evaluate_code)
146 # Upload files parser
147 upload_files_factory = factory.copy()
148 upload_files_factory.add_source_folder(None)
149 parser_upload_files = subparsers.add_parser(
150 "upload_files",
151 parents=[upload_files_factory.parser],
152 formatter_class=ArgumentDefaultsHelpFormatter,
153 description="Upload files via commit to Gitlab from source folder",
154 help="Upload files via commit to Gitlab from source folder",
155 )
156 parser_upload_files.set_defaults(func=edit_upload_files)
158 # Commit changes parser
159 commit_changes_factory = factory.copy()
160 commit_changes_factory.add_source_folder(None)
161 commit_changes_factory.add_message("Commit message used for all repos")
162 commit_changes_factory.add_workspace()
163 commit_changes_factory.add_keep_solutions()
164 parser_commit_changes = subparsers.add_parser(
165 "commit_changes",
166 parents=[commit_changes_factory.parser],
167 formatter_class=ArgumentDefaultsHelpFormatter,
168 description="Copy source folder to workspace and commit the changes",
169 help="Copy source folder to workspace and commit the changes",
170 )
171 parser_commit_changes.set_defaults(func=edit_commit_changes)
173 # Add users parser
174 add_users_factory = factory.copy()
175 add_users_factory.add_student_group_info_file()
176 parser_add_users = subparsers.add_parser(
177 "add_users",
178 parents=[add_users_factory.parser],
179 formatter_class=ArgumentDefaultsHelpFormatter,
180 description="Add all users to their respective repositories",
181 help="Add all users to their respective repositories",
182 )
183 parser_add_users.set_defaults(func=edit_add_users)
185 return parser.parse_args(arguments[1:])
188def main() -> None:
189 """main."""
190 args = parse_arguments(sys.argv)
191 args.func(args)
194if __name__ == "__main__":
195 main()