def test_create_blob__error(self): repo = mock.Mock(spec=Repository) repo.create_blob.return_value = None commit = CommitDir(repo) commit.dry_run = False with self.assertRaises(GithubException): self.assertTrue(commit._create_blob(b"", "local_path"))
def test_validate_dirs(self): repo = mock.Mock(spec=Repository) commit = CommitDir(repo) with self.assertRaises(GithubException): commit._validate_dirs("bogus", None) _, repo_dir = commit._validate_dirs(".", None) self.assertEqual("", repo_dir) _, repo_dir = commit._validate_dirs(".", "./test/") self.assertEqual("test", repo_dir)
def _run_task(self): # args branch = self.options.get( "branch", self.project_config.project__apexdoc__branch ) if not branch: raise GithubException("Unable to determine branch name") local_dir = self.options.get("dir_local") if not local_dir: local_base_dir = ( self.project_config.project__apexdoc__dir if self.project_config.project__apexdoc__dir else self.project_config.repo_root ) local_dir = os.path.join(local_base_dir, "ApexDocumentation") repo_dir = self.options.get( "dir_repo", self.project_config.project__apexdoc__repo_dir ) dry_run = process_bool_arg(self.options.get("dry_run", False)) commit_message = self.options.get("commit_message", "Update Apex docs") # get API repo = self.get_repo() # commit author = {"name": self.github.user().name, "email": self.github_config.email} commit_dir = CommitDir(repo, self.logger, author) commit_dir(local_dir, branch, repo_dir, commit_message, dry_run)
def commit_changes_to_github( *, user, scratch_org, repo_id, branch, desired_changes, commit_message, target_directory, originating_user_id, ): with local_github_checkout(user, repo_id, branch) as project_path: # This won't return anything in-memory, but rather it will emit # files which we then copy into a source checkout, and then # commit and push all that. run_retrieve_task( user, scratch_org, project_path, desired_changes, target_directory, originating_user_id, ) repo = get_repo_info(user, repo_id=repo_id) author = {"name": user.username, "email": user.email} local_dir = os.path.join(project_path, target_directory) CommitDir(repo, author=author)( local_dir, branch, repo_dir=target_directory, commit_message=commit_message )
def _run_task(self): # args branch = self.options.get( 'branch', self.project_config.project__apexdoc__branch, ) if not branch: raise GithubException('Unable to determine branch name') if 'dir_local' in self.options: local_dir = self.options['dir_local'] else: local_base_dir = ( self.project_config.project__apexdoc__dir if self.project_config.project__apexdoc__dir else self.project_config.repo_root ) local_dir = os.path.join(local_base_dir, 'ApexDocumentation') repo_dir = self.options.get( 'dir_repo', self.project_config.project__apexdoc__repo_dir, ) dry_run = process_bool_arg(self.options.get('dry_run', False)) commit_message = self.options.get('commit_message', 'Update Apex docs') # get API repo = self.get_repo() # commit author = { 'name': self.github.user().name, 'email': self.github_config.email, } commit_dir = CommitDir(repo, self.logger, author) commit_dir(local_dir, branch, repo_dir, commit_message, dry_run)
def test_call__no_changes(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.tree = mock.Mock(return_value=mock.Mock(recurse=mock.Mock( return_value=mock.Mock(to_json=mock.Mock( return_value={"tree": []}))))) commit = CommitDir(repo) commit(d, "master", commit_message="msg") repo.create_commit.assert_not_called()
def _create_commit(self, path): committer = CommitDir(self.target_repo, logger=self.logger) message = f"Publishing release {self.options['version']}" return committer( path, self.options["branch"], repo_dir="", commit_message=message, dry_run=self.options["dry_run"], )
def test_call__no_changes(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.tree = mock.Mock( return_value=Tree( {"url": "string", "sha": "tree-ish-hash", "tree": []}, None ) ) commit = CommitDir(repo) commit(d, "main", commit_message="msg") repo.create_commit.assert_not_called()
def test_call__error_creating_commit(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.create_commit.return_value = None repo.tree = mock.Mock(return_value=mock.Mock(recurse=mock.Mock( return_value=mock.Mock(to_json=mock.Mock( return_value={"tree": []}))))) with open("new", "w") as f: f.write("new") commit = CommitDir(repo) with self.assertRaises(GithubException): commit(d, "master", commit_message="msg")
def _create_commit(self, path): committer = CommitDir(self.target_repo, logger=self.logger) message = f"Published content from ref {self.ref}" if "version" in self.options: message += f'\n\nVersion {self.options["version"]}' return committer( path, self.options["branch"], repo_dir="", commit_message=message, dry_run=self.options["dry_run"], )
def test_call__error_creating_commit(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.create_commit.return_value = None repo.tree = mock.Mock( return_value=Tree( {"url": "string", "sha": "tree-ish-hash", "tree": []}, None ) ) with open("new", "w") as f: f.write("new") commit = CommitDir(repo) with self.assertRaises(GithubException): commit(d, "main", commit_message="msg")
def test_call(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.owner = "SalesforceFoundation" repo.name = "TestRepo" repo.tree = mock.Mock(return_value=mock.Mock(recurse=mock.Mock( return_value=mock.Mock(to_json=mock.Mock( return_value={ "tree": [ { "type": "tree", "path": "dir", "sha": "bogus1", }, { "type": "blob", "path": "file_outside_dir", "sha": "bogus2", }, { "type": "blob", "path": os.path.join("dir", "unchanged"), "sha": hashlib.sha1(b"blob 0\0").hexdigest(), }, { "type": "blob", "path": os.path.join("dir", "modified"), "sha": "bogus3", }, { "type": "blob", "path": os.path.join("dir", "removed"), "sha": "bogus4", }, ] }))))) commit = CommitDir(repo) os.mkdir("dir") with open("unchanged", "w") as f: f.write("") with open("modified", "w") as f: f.write("modified") with open("new", "w") as f: f.write("new") with open(".hidden", "w") as f: pass commit(d, "master", "dir", dry_run=True) commit(d, "master", "dir", commit_message="msg") repo.create_commit.assert_called_once()
def test_call(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.owner = "SalesforceFoundation" repo.name = "TestRepo" repo.tree = mock.Mock() repo.tree.return_value = Tree( { "url": "string", "sha": "tree-ish-hash", "tree": [ { "type": "tree", "mode": "100644", "path": "dir", "sha": "bogus1", }, { "type": "blob", "mode": "100644", "path": "file_outside_dir", "sha": "bogus2", }, { "type": "blob", "mode": "100644", "path": "dir/deleteme", "sha": "deletedfilesha", }, { "type": "blob", "mode": "100644", "path": "dir/unchanged", "sha": hashlib.sha1(b"blob 0\0").hexdigest(), }, { "type": "blob", "mode": "100644", "path": "dir/modified", "sha": "bogus3", }, ], }, None, ) commit = CommitDir(repo) with open("unchanged", "w") as f: f.write("") with open("modified", "w") as f: f.write("modified") with open("new", "w") as f: f.write("new") with open(".hidden", "w") as f: pass commit(d, "main", "dir", dry_run=True) assert commit.new_tree_list == [ { "sha": "bogus2", "mode": "100644", "path": "file_outside_dir", "size": None, "type": "blob", }, { "sha": hashlib.sha1(b"blob 0\0").hexdigest(), "mode": "100644", "path": "dir/unchanged", "size": None, "type": "blob", }, { "sha": None, "mode": "100644", "path": "dir/modified", "size": None, "type": "blob", }, {"path": "dir/new", "mode": "100644", "type": "blob", "sha": None}, ] commit(d, "main", "dir", commit_message="msg") repo.create_commit.assert_called_once()
def test_create_blob__handles_decode_error(self): repo = mock.Mock(spec=Repository) commit = CommitDir(repo) self.assertTrue(commit._create_blob(b"\x9c"))
def test_create_blob__handles_decode_error(self): repo = mock.Mock(spec=Repository) commit = CommitDir(repo) commit.dry_run = False self.assertTrue(commit._create_blob(b"\x9c", "local_path"))