Skip to content

Commit

Permalink
ADD automatic adding of ta to github org team
Browse files Browse the repository at this point in the history
  • Loading branch information
wabscale committed Sep 1, 2022
1 parent fadc969 commit 0df1b47
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
17 changes: 17 additions & 0 deletions api/anubis/github/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from anubis.github.api import github_rest


def list_members(org: str, team: str) -> list[str]:
return [
user['login']
for user in github_rest(f"/orgs/{org}/teams/{team}/members")
if 'login' in user
]


def add_member(org: str, team: str, username: str):
github_rest(f"/orgs/{org}/teams/{team}/memberships/{username}", method="put")


def remove_member(org: str, team: str, username: str):
github_rest(f"/orgs/{org}/teams/{team}/memberships/{username}", method="delete")
33 changes: 29 additions & 4 deletions api/anubis/views/admin/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from anubis.utils.data import req_assert, row2dict
from anubis.utils.http import error_response, success_response
from anubis.utils.http.decorators import json_endpoint, json_response
from anubis.github.team import add_member, remove_member

courses_ = Blueprint("admin-courses", __name__, url_prefix="/admin/courses")

Expand Down Expand Up @@ -325,7 +326,7 @@ def admin_course_make_ta_id(user_id: str):
"""

# Get the user that will be the TA
other = User.query.filter(User.id == user_id).first()
other: User = User.query.filter(User.id == user_id).first()

# Check that the user exists
req_assert(other is not None, message="user does not exist")
Expand Down Expand Up @@ -358,6 +359,12 @@ def admin_course_make_ta_id(user_id: str):
db.session.add(ta)
db.session.commit()

add_member(
course_context.github_org,
course_context.github_ta_team_slug,
other.github_username
)

# Return the status
return success_response({"status": "TA added to course"})

Expand All @@ -377,7 +384,7 @@ def admin_course_remove_ta_id(user_id: str):
assert_course_superuser(course_context.id)

# Get the user object for the specified user
other = User.query.filter(User.id == user_id).first()
other: User = User.query.filter(User.id == user_id).first()

# Make sure that the other user exists
req_assert(other is not None, message="user does not exist")
Expand All @@ -395,6 +402,12 @@ def admin_course_remove_ta_id(user_id: str):
# Commit the delete
db.session.commit()

remove_member(
course_context.github_org,
course_context.github_ta_team_slug,
other.github_username
)

# Return the status
return success_response(
{
Expand All @@ -416,7 +429,7 @@ def admin_course_make_professor_id(user_id: str):
"""

# Get the other user
other = User.query.filter(User.id == user_id).first()
other: User = User.query.filter(User.id == user_id).first()

# Make sure they exist
req_assert(other is not None, message="user does not exist")
Expand Down Expand Up @@ -450,6 +463,12 @@ def admin_course_make_professor_id(user_id: str):
db.session.add(prof)
db.session.commit()

add_member(
course_context.github_org,
course_context.github_ta_team_slug,
other.github_username
)

# Return the status
return success_response({"status": "Professor added to course"})

Expand All @@ -466,7 +485,7 @@ def admin_course_remove_professor_id(user_id: str):
"""

# Get the other user
other = User.query.filter(User.id == user_id).first()
other: User = User.query.filter(User.id == user_id).first()

# Make sure the other user exists
req_assert(other is not None, message="user does not exist")
Expand All @@ -480,6 +499,12 @@ def admin_course_remove_professor_id(user_id: str):
# Commit the delete
db.session.commit()

remove_member(
course_context.github_org,
course_context.github_ta_team_slug,
other.github_username
)

# Return the status
return success_response(
{
Expand Down
47 changes: 47 additions & 0 deletions api/jobs/daily_cleanup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import traceback

from anubis.github.team import add_member, list_members
from anubis.lms.assignments import get_recent_assignments
from anubis.lms.courses import get_active_courses
from anubis.lms.courses import get_course_tas, get_course_professors, get_course_users, user_to_user_id_set
Expand All @@ -12,6 +15,47 @@
from anubis.utils.logging import logger


def reap_github_admin_teams():
"""
Make sure tas are properly added to groups
:return:
"""

# Get all current courses
courses: list[Course] = get_active_courses()

# Iterate through all courses within the system
for course in courses:
if course.github_ta_team_slug == '' or course.github_ta_team_slug is None:
logger.info(f'Skipping reap_github_ta_teams course_id = {course.id}')
continue

logger.info(f'Inspecting github ta permissions course_id = {course.id} '
f'org = "{course.github_org}" team = "{course.github_ta_team_slug}"')

# Get all students, professors and TAs for course
tas: list[User] = get_course_tas(course)
profs: list[User] = get_course_professors(course)
members: list[str] = list_members(course.github_org, course.github_ta_team_slug)

for user in set(tas).union(set(profs)):
if user.github_username == '' or user.github_username is None:
logger.info(f'User does not have github linked yet, skipping for now')
continue

if user.github_username in members:
logger.info(f'Skipping adding user to team. Already member user = "{user.id}"')
continue

logger.info(f'Adding user to team. Not already member user = "{user.id}"')

try:
add_member(course.github_org, course.github_ta_team_slug, user.github_username)
except Exception as e:
logger.error(f'Could not complete member add {e}\n\n' + traceback.format_exc())


def reap_ta_professor():
"""
Find TAs and Professors that do not have an InCourse row
Expand Down Expand Up @@ -86,6 +130,9 @@ def reap():
# Fix question assignments
fix_question_assignments()

# Check that course admins are in ta group on GitHub
reap_github_admin_teams()


if __name__ == "__main__":
print("")
Expand Down

0 comments on commit 0df1b47

Please sign in to comment.