From 4d13670a264aa50f4a0e830a22b3724dd79ad22b Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 23 Nov 2023 23:03:43 +0100 Subject: [PATCH] added pushing untracked and modified --- Dockerfile | 7 ++--- README.md | 1 + valhalla.yml | 7 ++--- valhalla/ci_provider/get_token.py | 15 +++++++++ valhalla/commit/commit.py | 51 +++++++++++++++++++++++-------- valhalla/common/get_config.py | 16 +++++++--- valhalla/main.py | 7 +++-- 7 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 valhalla/ci_provider/get_token.py diff --git a/Dockerfile b/Dockerfile index c77bdb8..55657da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,9 +19,6 @@ ARG WORKING_REPO_PATH="/repository" RUN mkdir $WORKING_REPO_PATH WORKDIR $WORKING_REPO_PATH -## TESTS -ENV CI_COMMIT_BRANCH="release-1.2.3" -RUN git clone https://gitlab.com/peter.zmilczak/test-valhalla.git . -ADD valhalla.yml $WORKING_REPO_PATH -CMD ["python3", "/opt/valhalla"] + +CMD ["python3", "-u", "/opt/valhalla"] diff --git a/README.md b/README.md index f1696cd..b6c3a0f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ saving time, and promoting compliance with established regulations. - ls -l - echo "Good job!" & ps ``` +- Create access token and pass it to CI with variable `VALHALLA_TOKEN` ### 🔸 usage diff --git a/valhalla.yml b/valhalla.yml index 3a32517..f4c11fe 100644 --- a/valhalla.yml +++ b/valhalla.yml @@ -2,8 +2,7 @@ git_host: gitlab project_name: valhalla commit: enabled: True + username: Test1234 + email: test-valhalla@logchange.dev before: - - echo "test" - - echo "test2" - - ls -l - - echo "Good job!" & ps + - echo "test" > some_file3.md diff --git a/valhalla/ci_provider/get_token.py b/valhalla/ci_provider/get_token.py new file mode 100644 index 0000000..978d40f --- /dev/null +++ b/valhalla/ci_provider/get_token.py @@ -0,0 +1,15 @@ +import os + +from valhalla.common.logger import info, error + + +def get_token() -> str: + token = os.environ.get('VALHALLA_TOKEN') + + if token: + info(f'Variable VALHALLA_TOKEN is set to: {"*" * len(token)}') + + return token + else: + error('VALHALLA_TOKEN environment variable is not set!') + exit(-1) diff --git a/valhalla/commit/commit.py b/valhalla/commit/commit.py index b2bad13..1411e5c 100644 --- a/valhalla/commit/commit.py +++ b/valhalla/commit/commit.py @@ -4,9 +4,20 @@ class GitRepository: - def __init__(self): + def __init__(self, git_username, git_email): self.repository = Repo.init(".") + if git_username is None or git_username == "": + info("Git username not set, using default valhalla-bot") + git_username = "valhalla-bot" + + if git_email is None or git_email == "": + info("Git email not set, using default valhalla-bot@logchange.dev") + git_email = "valhalla-bot@logchange.dev" + + self.repository.config_writer().set_value("user", "name", git_username).release() + self.repository.config_writer().set_value("user", "email", git_email).release() + def status(self): info("----------------------") info("Git status") @@ -25,22 +36,36 @@ def commit(self, msg: str, add=True): self.status() if add: - if self.repository.is_dirty(): - untracked = self.repository.untracked_files - for f in untracked: - self.repository.index.add(f) - info(f"{f} added to stage") + untracked = self.repository.untracked_files + for f in untracked: + self.repository.git.add(f) + info(f"Untracked file: {f} added to stage") else: info(f"add={add}, skipping adding untracked files") - self.repository.index.commit(msg) + modified = self.repository.index.diff(None) + for f in modified: + self.repository.git.add(f.a_path) + info(f"Modified file: {f.a_path} added to stage") + + commit = self.repository.index.commit(msg) + info(f"Created commit: {commit}") self.status() - def push(self): - origin = self.repository.remote('origin') - origin.push() + def push(self, token): + info("Preparing to push") + + branch = self.repository.active_branch + + info(f"Current branch: {branch}") + self.repository.git.push(self.__get_push_url(token), str(branch)) + info("Performed push") -# if __name__ == '__main__': -# git_repo = GitRepository() -# git_repo.commit("testing commiting") + def __get_push_url(self, token): + origin = self.repository.remote(name='origin') + remote_url = origin.url + info(f"Remote url: {remote_url}") + remote_url = remote_url.replace("https://", "").replace("http://", "") + push_url = "https://{}:{}@{}".format("valhalla-bot", token, remote_url) + return push_url diff --git a/valhalla/common/get_config.py b/valhalla/common/get_config.py index a6c559f..2092ae3 100644 --- a/valhalla/common/get_config.py +++ b/valhalla/common/get_config.py @@ -6,14 +6,18 @@ class Commit: - def __init__(self, enabled: bool, before_commands: list[str]): + def __init__(self, enabled: bool, git_username: str, git_email: str, before_commands: list[str]): self.enabled = enabled + self.git_username = git_username + self.git_email = git_email self.before_commands = before_commands def __repr__(self): - return f" Commit( \n" \ - f" enabled={self.enabled} \n" \ - f" before_commands={self.before_commands} \n" \ + return f" \nCommit( \n" \ + f" enabled={self.enabled} \n" \ + f" git_username={self.git_username} \n" \ + f" git_email={self.git_email} \n" \ + f" before_commands={self.before_commands} \n" \ f" )" @@ -55,8 +59,10 @@ def get_config(path): def get_commit_part(commit: dict): enabled = commit['enabled'] + git_username = commit['username'] + git_email = commit['email'] before_commands = commit['before'] - return Commit(str_to_bool(enabled), before_commands) + return Commit(str_to_bool(enabled), git_username, git_email, before_commands) def str_to_bool(value: str) -> bool: diff --git a/valhalla/main.py b/valhalla/main.py index b74d62a..2f7f1ca 100644 --- a/valhalla/main.py +++ b/valhalla/main.py @@ -1,3 +1,4 @@ +from valhalla.ci_provider.get_token import get_token from valhalla.commit import before from valhalla.ci_provider.gitlab.get_version import get_version_number_to_release from valhalla.commit.commit import GitRepository @@ -15,9 +16,11 @@ def start(): if config.commit.enabled: info("Commit enabled is True so scripts, commit, push will be performed") before.execute(config.commit.before_commands) - git = GitRepository() + git = GitRepository(config.commit.git_username, config.commit.git_email) git.commit(f"Releasing version {project.version}") - git.push() + token = get_token() + git.push(token) + info("Pushed successful!") else: info("Commit disabled(enabled: False), skipping scripts, commit, push")