Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 Add commit check #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,57 @@ def github_get_latest_commit_date(repo):
pushed_at = json.loads(res.content).get("pushed_at")
return datetime.datetime.fromisoformat(pushed_at.replace("Z", "+00:00"))

def check_contributors(repo, student):
"""
checks the list of contributors, there can only be the student and teachers

:param repo: repository name (with organization/owner prefix)
:param student: dict with a 'github' key
:returns: True if there are no extra contributors, False otherwise
"""
status_headers = {
"User-Agent": "GitHubGetCommits/1.0",
"Authorization": "token " + settings.github_token,
"Accept": "application/vnd.github.v3.raw",
}
res = requests_retry_session().get(
"https://api.github.com/repos/{}/contributors".format(repo),
headers=status_headers,
timeout=settings.requests_timeout
)
if res.status_code != 200:
raise Exception("GitHub API reported an error while trying to get info about repository '{}'! Message is '{}' ({}).".format(repo, res.reason, res.status_code))
res_json = json.loads(res.content)
for contributor in res_json:
login = contributor["login"]
if login not in settings.teacher_github_logins or login != student["github"]:
return False
return True

def check_test_unchanged(lab_id, repo):
"""
check if the student changed tests

:param lab_id: number of lab
:param repo: repository name (with organization/owner prefix)
:returns: True if student did't change the tests, False otherwise
"""
for file in settings.os_labs[lab_id]['test_files']:
status_headers = {
"User-Agent": "GitHubGetCommits/1.0",
"Authorization": "token " + settings.github_token,
"Accept": "application/vnd.github.v3.raw",
}
res = requests_retry_session().get(
"https://api.github.com/repos/{}/commits?path={}".format(repo, file),
headers=status_headers,
timeout=settings.requests_timeout
)
if res.status_code != 200:
raise Exception("GitHub API reported an error while trying to get info about repository '{}'! Message is '{}' ({}).".format(repo, res.reason, res.status_code))
res_json = json.loads(res.content)
for commit in res_json:
author = commit["commit"]["author"]["name"]
if author not in settings.teacher_github_logins:
return False
return True
6 changes: 6 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ def check_lab(lab_id, groups, data, data_update=[]):
# check TASKID from logs
if common.get_task_id(log) != student_task_id:
google_sheets.set_student_lab_status(data, student, lab_id_int, "?! Wrong TASKID!", data_update=data_update)
# check is the student is the author of commits
elif not common.check_contributors(repo, student):
google_sheets.set_student_lab_status(data, student, lab_id_int, "?! Wrong Author!", data_update=data_update)
# check if the tests are unchanged
elif not common.check_test_unchanged(lab_id, repo):
google_sheets.set_student_lab_status(data, student, lab_id_int, "?! Tests modified!", data_update=data_update)
else:
# everything looks good, go on and update lab status
student_dt = isoparse(completion_date)
Expand Down
6 changes: 6 additions & 0 deletions settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ google_spreadsheet_id = "1ymyU98eB0HYUzVTgrArbtEOkiU3lnKSOS6BUNkssbTE"
# MOSS
moss_userid = None # PLACE YOUR MOSS USER ID HERE

teacher_github_logins = [ "Mark Polyak", "markpolyak" ]

# номер лабораторной работы и количество вариантов
os_labs = {
'1': {
Expand All @@ -35,6 +37,7 @@ os_labs = {
'github_prefix': 'os-task1',
'penalty_max': 6,
'files': ['lab1.sh'],
'test_files': ['test.sh'],
'moss': {'language': 'c', 'max-matches': 1000, 'basefiles': [{'repo': 'k43guap/os-course-task1', 'filename': 'lab1.sh'}]}, #{'language': 'ascii', 'max-matches': 10, 'directory': 0, 'basefiles': []},
},
'2': {
Expand All @@ -43,6 +46,7 @@ os_labs = {
'github_prefix': 'os-task2',
'penalty_max': 8,
'files': ['lab2.cpp'],
'test_files': ['test'],
'moss': {'language': 'cc', 'max-matches': 100, 'basefiles': [
{'repo': 'k43guap/os-course-task2', 'filename': 'lab2.cpp'},
{'repo': 'k43guap/os-course-task2', 'filename': 'examples/ex3.cpp'},
Expand All @@ -54,6 +58,7 @@ os_labs = {
'github_prefix': 'os-task3',
'penalty_max': 8,
'files': ['lab3.cpp'],
'test_files': ['test'],
'moss': {'language': 'cc', 'max-matches': 100, 'basefiles': [
{'repo': 'k43guap/os-course-task3', 'filename': 'lab3.cpp'},
]},
Expand All @@ -65,6 +70,7 @@ os_labs = {
'penalty_max': 10,
'penalty_policy': {},
'files': ['lab4.cpp'],
'test_files': ['test', 'run_test.sh', 'tests.sh'],
'moss': {'language': 'cc', 'max-matches': 100, 'basefiles': [
{'repo': 'k43guap/os-course-task4', 'filename': 'lab4.cpp'},
]},
Expand Down