Skip to content

Commit

Permalink
added pushing untracked and modified
Browse files Browse the repository at this point in the history
  • Loading branch information
marwin1991 committed Nov 23, 2023
1 parent 662a6c8 commit 4d13670
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 29 deletions.
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions valhalla.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions valhalla/ci_provider/get_token.py
Original file line number Diff line number Diff line change
@@ -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)
51 changes: 38 additions & 13 deletions valhalla/commit/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
16 changes: 11 additions & 5 deletions valhalla/common/get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" )"


Expand Down Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions valhalla/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")

Expand Down

0 comments on commit 4d13670

Please sign in to comment.