Commit acd57fb7 by Victor Shnayder

Save author info in commit messages.

* add optional author_str param to export_to_github
* pass the django user info to that param
parent 942ea2c7
...@@ -82,6 +82,21 @@ def edit_item(request): ...@@ -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 <email@email.com>.
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 @login_required
@expect_json @expect_json
def save_item(request): def save_item(request):
...@@ -98,6 +113,7 @@ def save_item(request): ...@@ -98,6 +113,7 @@ def save_item(request):
course_location = Location(item_id)._replace(category='course', name=None) course_location = Location(item_id)._replace(category='course', name=None)
courses = modulestore().get_items(course_location, depth=None) courses = modulestore().get_items(course_location, depth=None)
for course in courses: 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({})) return HttpResponse(json.dumps({}))
...@@ -38,7 +38,12 @@ def import_from_github(repo_settings): ...@@ -38,7 +38,12 @@ def import_from_github(repo_settings):
return git_repo.head.commit.hexsha, module_store.courses[course_dir] 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) repo_path = settings.DATA_DIR / course.metadata.get('course_dir', course.location.course)
fs = OSFS(repo_path) fs = OSFS(repo_path)
xml = course.export_to_xml(fs) xml = course.export_to_xml(fs)
...@@ -49,8 +54,11 @@ def export_to_github(course, commit_message): ...@@ -49,8 +54,11 @@ def export_to_github(course, commit_message):
git_repo = Repo(repo_path) git_repo = Repo(repo_path)
if git_repo.is_dirty(): if git_repo.is_dirty():
git_repo.git.add(A=True) 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 origin = git_repo.remotes.origin
if settings.MITX_FEATURES['GITHUB_PUSH']: if settings.MITX_FEATURES['GITHUB_PUSH']:
push_infos = origin.push() push_infos = origin.push()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment