From fff8f76caeca5c7473add2ffff1ed8c5c1a85a06 Mon Sep 17 00:00:00 2001 From: "Ja (Thanakul) Wattanawong" Date: Wed, 29 Jul 2020 01:37:50 -0700 Subject: [PATCH] modify_and_branch implementation simplified Working with the Git data structures is very powerful (and we will need to do it if we want to modify multiple files in one commit etc.), but for changing one file there is no need. This is a lot simpler to understand and both are functionally equivalent. --- ocflib/infra/github.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/ocflib/infra/github.py b/ocflib/infra/github.py index 0df7fd9f..2a7d6377 100644 --- a/ocflib/infra/github.py +++ b/ocflib/infra/github.py @@ -1,6 +1,4 @@ from github import Github -from github import InputGitTreeElement - class GithubCredentials(): """Basic class to store Github credentials and verify input""" @@ -47,16 +45,17 @@ def get_file(self, filename): Note that GitHub's API only supports files up to 1MB in size.""" return self._github.get_contents(filename).decoded_content.decode('utf-8') - def modify_and_branch(self, base_branch, new_branch_name, commit_message, filename, file_content): + def modify_and_branch(self, base_branch, new_branch_name, commit_message, file_path, file_contents): """Create a new branch from base_branch, makes changes to a file, and commits it to the new branch.""" - - base_sha = self._github.get_git_ref('heads/{}'.format(base_branch)).object.sha - base_tree = self._github.get_git_tree(base_sha) - element = InputGitTreeElement(filename, '100644', 'blob', file_content) - tree = self._github.create_git_tree([element], base_tree) - - parent = self._github.get_git_commit(base_sha) - commit = self._github.create_git_commit(commit_message, tree, [parent]) - - self._github.create_git_ref('refs/heads/{}'.format(new_branch_name), commit.sha) + + base_sha = self._github.get_branch(base_branch).commit.sha + self._github.create_git_ref('refs/heads/{}'.format(new_branch_name), base_sha) + current_contents = self._github.get_contents(file_path, ref=new_branch_name) + self._github.update_file(file_path, + commit_message, + file_contents, + current_contents.sha, + branch=new_branch_name) + +