def test_commit_with_push(self): mock_repo = mock.MagicMock() mock_repo.active_branch.name = "name" with mock.patch("licenses.git_utils.run_git") as mock_run_git: commit_and_push_changes(mock_repo, "commit msg", "", push=True) self.assertEqual( [ mock.call( mock_repo, ["git", "commit", "--quiet", "-am", "commit msg"], ), mock.call( mock_repo, ["git", "status", "--untracked", "--short"] ), mock.call( mock_repo, [ "git", "push", "-u", "origin", mock_repo.active_branch.name, ], ), ], mock_run_git.call_args_list, )
def handle_updated_translation_branch(self, repo, legalcodes): # legalcodes whose translations have been updated and all belong to the same translation branch # if we update the branch, we'll also publish updates to its static files. if not legalcodes: return branch_name = legalcodes[0].branch_name() language_code = legalcodes[0].language_code version = legalcodes[0].license.version self.say(2, f"Updating branch {branch_name}") setup_local_branch(repo, branch_name) # Track the translation update using a TranslationBranch object # First-party/Local from licenses.models import TranslationBranch branch_object, _ = TranslationBranch.objects.get_or_create( branch_name=branch_name, language_code=language_code, version=version, complete=False, ) for legalcode in legalcodes: self.update_branch_for_legalcode(repo, legalcode, branch_object) self.say(2, "Publishing static files") call_command("publish", branch_name=branch_name) repo.index.add([ os.path.relpath(settings.DISTILL_DIR, settings.TRANSLATION_REPOSITORY_DIRECTORY) ]) # Commit and push this branch self.say(2, "Committing and pushing") commit_and_push_changes(repo, "Translation changes from Transifex.", "", push=True) self.say( 2, f"Updated branch {branch_name} with updated translations and pushed" ) # Don't need local branch anymore kill_branch(repo, branch_name) # Now that we know the new changes are upstream, save the LegalCode # objects with their new translation_last_updates, and the branch object. # First-party/Local from licenses.models import LegalCode LegalCode.objects.bulk_update( legalcodes, fields=["translation_last_update"], ) branch_object.save()
def publish_branch(self, branch: str): """Workflow for publishing a single branch""" print(f"Publishing branch {branch}") with git.Repo(settings.TRANSLATION_REPOSITORY_DIRECTORY) as repo: setup_local_branch(repo, branch) self.run_django_distill() if repo.is_dirty(untracked_files=True): # Add any changes and new files commit_and_push_changes(repo, "Updated built HTML files", self.relpath, push=self.push) if repo.is_dirty(untracked_files=True): raise Exception( "Something went wrong, the repo is still dirty") else: print(f"\n{branch} build dir is up to date.\n")
def test_changes_are_added(self): self.local_repo.heads.otherbranch.checkout() file_to_delete = self.add_file( self.local_repo ) # This is automatically committed path_to_delete = os.path.join(self.local_repo_path, file_to_delete) file_to_change = self.add_file(self.local_repo) path_to_change = os.path.join(self.local_repo_path, file_to_change) os.remove(path_to_delete) untracked_file_to_add = "untracked_file_to_add" path_to_add = os.path.join(self.local_repo_path, untracked_file_to_add) with open(path_to_add, "w") as f: f.write("untracked") with open(path_to_change, "w") as f: f.write("Now this file has different content") commit_and_push_changes( self.local_repo, "Add and remove test", "", push=False ) subprocess.run( ["git", "status"], cwd=self.local_repo_path, ) self.assertFalse(self.local_repo.is_dirty()) # Switch to main - these files don't exist self.local_repo.heads.main.checkout() self.assertFalse(os.path.exists(path_to_add)) self.assertFalse(os.path.exists(path_to_change)) self.assertFalse(os.path.exists(path_to_delete)) # Switch to otherbranch - these files are as expected. self.local_repo.heads.otherbranch.checkout() self.assertTrue(os.path.exists(path_to_add)) self.assertTrue(os.path.exists(path_to_change)) self.assertEqual( "Now this file has different content", open(path_to_change, "r").read(), ) self.assertFalse(os.path.exists(path_to_delete))