From acd57fb7c5945bacd09315a07d5453586acadcff Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Mon, 16 Jul 2012 11:34:24 -0400 Subject: [PATCH] Save author info in commit messages. * add optional author_str param to export_to_github * pass the django user info to that param --- cms/djangoapps/contentstore/views.py | 18 +++++++++++++++++- cms/djangoapps/github_sync/__init__.py | 14 +++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 33eacd228b..8e07d92203 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -82,6 +82,21 @@ def edit_item(request): }) +def user_author_string(user): + '''Get an author string for commits by this user. Format: + first last . + + If the first and last names are blank, uses the username instead. + Assumes that the email is not blank. + ''' + f = user.first_name + l = user.last_name + if f == '' and l == '': + f = user.username + return '{first} {last} <{email}>'.format(first=f, + last=l, + email=user.email) + @login_required @expect_json def save_item(request): @@ -98,6 +113,7 @@ def save_item(request): course_location = Location(item_id)._replace(category='course', name=None) courses = modulestore().get_items(course_location, depth=None) for course in courses: - export_to_github(course, "CMS Edit") + author_string = user_author_string(request.user) + export_to_github(course, "CMS Edit", author_string) return HttpResponse(json.dumps({})) diff --git a/cms/djangoapps/github_sync/__init__.py b/cms/djangoapps/github_sync/__init__.py index b8d9dbd683..9f490119af 100644 --- a/cms/djangoapps/github_sync/__init__.py +++ b/cms/djangoapps/github_sync/__init__.py @@ -38,7 +38,12 @@ def import_from_github(repo_settings): return git_repo.head.commit.hexsha, module_store.courses[course_dir] -def export_to_github(course, commit_message): +def export_to_github(course, commit_message, author_str=None): + ''' + Commit any changes to the specified course with given commit message, + and push to github (if MITX_FEATURES['GITHUB_PUSH'] is True). + If author_str is specified, uses it in the commit. + ''' repo_path = settings.DATA_DIR / course.metadata.get('course_dir', course.location.course) fs = OSFS(repo_path) xml = course.export_to_xml(fs) @@ -49,8 +54,11 @@ def export_to_github(course, commit_message): git_repo = Repo(repo_path) if git_repo.is_dirty(): git_repo.git.add(A=True) - git_repo.git.commit(m=commit_message) - + if author_str is not None: + git_repo.git.commit(m=commit_message, author=author_str) + else: + git_repo.git.commit(m=commit_message) + origin = git_repo.remotes.origin if settings.MITX_FEATURES['GITHUB_PUSH']: push_infos = origin.push()