Esempio n. 1
0
def _sync_apps_command(args: SyncAppsCommand.Args) -> None:
    team_config_git_repo_api = GitRepoApiFactory.create(
        args, args.organisation, args.repository_name)
    root_config_git_repo_api = GitRepoApiFactory.create(
        args, args.root_organisation, args.root_repository_name)
    with GitRepo(team_config_git_repo_api) as team_config_git_repo:
        with GitRepo(root_config_git_repo_api) as root_config_git_repo:
            __sync_apps(team_config_git_repo, root_config_git_repo,
                        args.git_user, args.git_email)
Esempio n. 2
0
    def execute(self) -> None:
        git_repo_api = self.__create_git_repo_api()
        with GitRepo(git_repo_api) as git_repo:
            git_repo.clone()

            if self.__args.create_pr:
                pr_branch = f"gitopscli-deploy-{str(uuid.uuid4())[:8]}"
                git_repo.new_branch(pr_branch)

            updated_values = self.__update_values(git_repo)
            if not updated_values:
                logging.info("All values already up-to-date. I'm done here.")
                return

            git_repo.push()

        if self.__args.create_pr:
            title, description = self.__create_pull_request_title_and_description(
                updated_values)
            pr_id = git_repo_api.create_pull_request_to_default_branch(
                pr_branch, title, description).pr_id

            if self.__args.auto_merge:
                git_repo_api.merge_pull_request(pr_id,
                                                self.__args.merge_method)
                git_repo_api.delete_branch(pr_branch)
Esempio n. 3
0
    def test_push_commit_hook_error_reason_is_shown(self, logging_mock):
        repo_dir = self.__origin.working_dir
        with open(f"{repo_dir}/.git/hooks/pre-receive",
                  "w") as pre_receive_hook:
            pre_receive_hook.write('echo >&2 "we reject this push"; exit 1')
        chmod(f"{repo_dir}/.git/hooks/pre-receive",
              stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)

        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()

            with open(testee.get_full_file_path("foo.md"), "w") as readme:
                readme.write("new file")
            util_repo = Repo(testee.get_full_file_path("."))
            util_repo.git.add("--all")
            util_repo.config_writer().set_value("user", "email",
                                                "*****@*****.**").release()
            util_repo.git.commit("-m", "new commit")

            logging_mock.reset_mock()

            with pytest.raises(GitOpsException) as ex:
                testee.push("master")
            assert "pre-receive" in str(
                ex.value) and "we reject this push" in str(ex.value)
        logging_mock.info.assert_called_once_with("Pushing branch: %s",
                                                  "master")
Esempio n. 4
0
    def execute(self) -> None:
        gitops_config = self.__get_gitops_config()
        preview_id = self.__args.preview_id

        preview_target_git_repo_api = self.__create_preview_target_git_repo_api(
            gitops_config)
        with GitRepo(preview_target_git_repo_api) as preview_target_git_repo:
            preview_target_git_repo.clone(gitops_config.preview_target_branch)

            preview_namespace = gitops_config.get_preview_namespace(preview_id)
            logging.info("Preview folder name: %s", preview_namespace)

            preview_folder_exists = self.__delete_folder_if_exists(
                preview_target_git_repo, preview_namespace)
            if not preview_folder_exists:
                if self.__args.expect_preview_exists:
                    raise GitOpsException(
                        f"There was no preview with name: {preview_namespace}")
                logging.info(
                    "No preview environment for '%s' and preview id '%s'. I'm done here.",
                    gitops_config.application_name,
                    preview_id,
                )
                return

            self.__commit_and_push(
                preview_target_git_repo,
                f"Delete preview environment for '{gitops_config.application_name}' and preview id '{preview_id}'.",
            )
Esempio n. 5
0
    def execute(self, ) -> None:
        gitops_config = self.__get_gitops_config()
        self.__create_preview_info_file(gitops_config)
        route_host = gitops_config.get_route_host(self.__args.preview_id)

        team_config_git_repo_api = self.__create_team_config_git_repo_api(
            gitops_config)
        with GitRepo(team_config_git_repo_api) as team_config_git_repo:
            team_config_git_repo.clone()

            created_new_preview = self.__create_preview_from_template_if_not_existing(
                team_config_git_repo, gitops_config)

            any_values_replaced = self.__replace_values(
                team_config_git_repo, gitops_config)

            if not created_new_preview and not any_values_replaced:
                self.__deployment_already_up_to_date_callback(route_host)
                logging.info(
                    "The preview is already up-to-date. I'm done here.")
                return

            self.__commit_and_push(
                team_config_git_repo,
                f"{'Create new' if created_new_preview else 'Update'} preview environment for "
                f"'{gitops_config.application_name}' and git hash '{self.__args.git_hash}'.",
            )

        if created_new_preview:
            self.__deployment_created_callback(route_host)
        else:
            self.__deployment_updated_callback(route_host)
Esempio n. 6
0
 def test_clone_unknown_url(self, logging_mock):
     self.__mock_repo_api.get_clone_url.return_value = "invalid_url"
     with GitRepo(self.__mock_repo_api) as testee:
         with pytest.raises(GitOpsException) as ex:
             testee.clone()
         self.assertEqual("Error cloning 'invalid_url'", str(ex.value))
     logging_mock.info.assert_called_once_with(
         "Cloning repository: %s", self.__mock_repo_api.get_clone_url())
Esempio n. 7
0
    def test_push_no_changes(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            logging_mock.reset_mock()

            testee.push("master")
        logging_mock.info.assert_called_once_with("Pushing branch: %s",
                                                  "master")
Esempio n. 8
0
    def test_push_current_branch(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            testee.new_branch("foo")
            logging_mock.reset_mock()

            testee.push()  # current branch
        logging_mock.info.assert_called_once_with("Pushing branch: %s", "foo")
Esempio n. 9
0
    def test_finalize(self):
        testee = GitRepo(self.__mock_repo_api)

        testee.clone()

        tmp_dir = testee.get_full_file_path("..")
        self.assertTrue(path.exists(tmp_dir))

        testee.finalize()
        self.assertFalse(path.exists(tmp_dir))
Esempio n. 10
0
    def test_new_branch_name_collision(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            logging_mock.reset_mock()

            with pytest.raises(GitOpsException) as ex:
                testee.new_branch("master")
            self.assertEqual("Error creating new branch 'master'.",
                             str(ex.value))
        logging_mock.info.assert_called_once_with("Creating new branch: %s",
                                                  "master")
Esempio n. 11
0
    def test_push_unknown_branch(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            logging_mock.reset_mock()

            with pytest.raises(GitOpsException) as ex:
                testee.push("unknown")
            assert str(ex.value).startswith(
                "Error pushing branch 'unknown' to origin")
        logging_mock.info.assert_called_once_with("Pushing branch: %s",
                                                  "unknown")
Esempio n. 12
0
    def test_clone_unknown_branch(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            with pytest.raises(GitOpsException) as ex:
                testee.clone("unknown")
            self.assertEqual(
                f"Error cloning branch 'unknown' of '{self.__mock_repo_api.get_clone_url()}'",
                str(ex.value))

        logging_mock.info.assert_called_once_with(
            "Cloning repository: %s (branch: %s)",
            self.__mock_repo_api.get_clone_url(), "unknown")
Esempio n. 13
0
    def test_clone_without_credentials(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()

            readme = self.__read_file(testee.get_full_file_path("README.md"))
            self.assertEqual("master branch readme", readme)

            self.assertFalse(
                path.exists(testee.get_full_file_path("../credentials.sh")))
        logging_mock.info.assert_called_once_with(
            "Cloning repository: %s", self.__mock_repo_api.get_clone_url())
Esempio n. 14
0
    def test_new_branch(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            logging_mock.reset_mock()

            testee.new_branch("foo")

            repo = Repo(testee.get_full_file_path("."))
            branches = [str(b) for b in repo.branches]
            self.assertIn("foo", branches)
        logging_mock.info.assert_called_once_with("Creating new branch: %s",
                                                  "foo")
Esempio n. 15
0
    def test_enter_and_exit_magic_methods(self):
        testee = GitRepo(self.__mock_repo_api)

        self.assertEqual(testee, testee.__enter__())

        testee.clone()

        tmp_dir = testee.get_full_file_path("..")
        self.assertTrue(path.exists(tmp_dir))

        testee.__exit__(None, None, None)
        self.assertFalse(path.exists(tmp_dir))
Esempio n. 16
0
def load_gitops_config(git_api_config: GitApiConfig, organisation: str,
                       repository_name: str) -> GitOpsConfig:
    git_repo_api = GitRepoApiFactory.create(git_api_config, organisation,
                                            repository_name)
    with GitRepo(git_repo_api) as git_repo:
        git_repo.clone()
        gitops_config_file_path = git_repo.get_full_file_path(
            ".gitops.config.yaml")
        try:
            gitops_config_yaml = yaml_file_load(gitops_config_file_path)
        except FileNotFoundError as ex:
            raise GitOpsException("No such file: .gitops.config.yaml") from ex
    return GitOpsConfig.from_yaml(gitops_config_yaml)
Esempio n. 17
0
    def test_clone_branch(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone("xyz")

            tmp_dir = testee.get_full_file_path("..")
            self.assertTrue(path.exists(tmp_dir))

            readme = self.__read_file(testee.get_full_file_path("README.md"))
            self.assertEqual("xyz branch readme", readme)

        self.assertFalse(path.exists(tmp_dir))
        logging_mock.info.assert_called_once_with(
            "Cloning repository: %s (branch: %s)",
            self.__mock_repo_api.get_clone_url(), "xyz")
Esempio n. 18
0
    def test_commit_nothing_to_commit(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            logging_mock.reset_mock()

            testee.commit(git_user="******",
                          git_email="*****@*****.**",
                          message="empty commit")

            repo = Repo(testee.get_full_file_path("."))
            commits = list(repo.iter_commits("master"))
            self.assertEqual(1, len(commits))
            self.assertEqual("initial commit\n", commits[0].message)
        logging_mock.assert_not_called()
Esempio n. 19
0
    def test_clone_with_credentials(self, logging_mock):
        self.__mock_repo_api.get_username.return_value = "User"
        self.__mock_repo_api.get_password.return_value = "Pass"
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()

            credentials_file = self.__read_file(
                testee.get_full_file_path("../credentials.sh"))
            self.assertEqual(
                """\
#!/bin/sh
echo username=User
echo password=Pass
""",
                credentials_file,
            )
        logging_mock.info.assert_called_once_with(
            "Cloning repository: %s", self.__mock_repo_api.get_clone_url())
Esempio n. 20
0
    def test_push(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()

            with open(testee.get_full_file_path("foo.md"), "w") as readme:
                readme.write("new file")
            util_repo = Repo(testee.get_full_file_path("."))
            util_repo.git.add("--all")
            util_repo.config_writer().set_value("user", "email",
                                                "*****@*****.**").release()
            util_repo.git.commit("-m", "new commit")

            logging_mock.reset_mock()

            testee.push("master")

            commits = list(self.__origin.iter_commits("master"))
            self.assertEqual(2, len(commits))
            self.assertEqual("new commit\n", commits[0].message)
        logging_mock.info.assert_called_once_with("Pushing branch: %s",
                                                  "master")
Esempio n. 21
0
    def test_commit(self, logging_mock):
        with GitRepo(self.__mock_repo_api) as testee:
            testee.clone()
            logging_mock.reset_mock()

            with open(testee.get_full_file_path("foo.md"), "w") as outfile:
                outfile.write("new file")
            with open(testee.get_full_file_path("README.md"), "w") as outfile:
                outfile.write("new content")
            testee.commit(git_user="******",
                          git_email="*****@*****.**",
                          message="new commit")

            repo = Repo(testee.get_full_file_path("."))
            commits = list(repo.iter_commits("master"))
            self.assertEqual(2, len(commits))
            self.assertEqual("new commit\n", commits[0].message)
            self.assertEqual("john doe", commits[0].author.name)
            self.assertEqual("*****@*****.**", commits[0].author.email)
            self.assertIn("foo.md", commits[0].stats.files)
            self.assertIn("README.md", commits[0].stats.files)
        logging_mock.info.assert_called_once_with(
            "Creating commit with message: %s", "new commit")
Esempio n. 22
0
 def test_get_full_file_path(self):
     with GitRepo(self.__mock_repo_api) as testee:
         testee.clone()
         self.assertRegex(testee.get_full_file_path("foo.bar"),
                          r"^/tmp/gitopscli/[0-9a-f\-]+/repo/foo\.bar$")
Esempio n. 23
0
 def test_get_author_from_last_commit_not_cloned_yet(self):
     with GitRepo(self.__mock_repo_api) as testee:
         with pytest.raises(GitOpsException) as ex:
             testee.get_author_from_last_commit()
     self.assertEqual("Repository not cloned yet!", str(ex.value))
Esempio n. 24
0
 def test_get_author_from_last_commit(self):
     with GitRepo(self.__mock_repo_api) as testee:
         testee.clone()
         self.assertEqual("unit tester <*****@*****.**>",
                          testee.get_author_from_last_commit())