Skip to content

Commit

Permalink
feat: Expose git author parameters
Browse files Browse the repository at this point in the history
Add optional git author parameters to all commands creating git commits.
If no author is provided the git user and email will be used for both committer and author.

Example:
```bash
gitopscli deploy \
  --git-user "GitOps CLI" \
  --git-email "gitopscli@baloise.dev" \
  --git-author-name "John Doe" \
  --git-author-email "john@baloise.dev" \
  ...
```

Resolves #195
  • Loading branch information
christiansiegel committed Aug 8, 2023
1 parent 70c69fc commit d61deda
Show file tree
Hide file tree
Showing 22 changed files with 460 additions and 85 deletions.
22 changes: 15 additions & 7 deletions docs/commands/create-pr-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ gitopscli create-pr-preview \

## Usage
```
usage: gitopscli create-pr-preview [-h] --username USERNAME --password PASSWORD
[--git-user GIT_USER] [--git-email GIT_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
--pr-id PR_ID
[--parent-id PARENT_ID] [-v [VERBOSE]]
usage: gitopscli create-pr-preview [-h] --username USERNAME --password
PASSWORD [--git-user GIT_USER]
[--git-email GIT_EMAIL]
[--git-author-name GIT_AUTHOR_NAME]
[--git-author-email GIT_AUTHOR_EMAIL]
--organisation ORGANISATION
--repository-name REPOSITORY_NAME
[--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
--pr-id PR_ID [--parent-id PARENT_ID]
[-v [VERBOSE]]
options:
-h, --help show this help message and exit
Expand All @@ -39,6 +43,10 @@ options:
--git-user GIT_USER Git Username
--git-email GIT_EMAIL
Git User Email
--git-author-name GIT_AUTHOR_NAME
Git Author Name
--git-author-email GIT_AUTHOR_EMAIL
Git Author Email
--organisation ORGANISATION
Apps Git organisation/projectKey
--repository-name REPOSITORY_NAME
Expand Down
8 changes: 7 additions & 1 deletion docs/commands/create-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ gitopscli create-preview \
```
usage: gitopscli create-preview [-h] --username USERNAME --password PASSWORD
[--git-user GIT_USER] [--git-email GIT_EMAIL]
[--git-author-name GIT_AUTHOR_NAME]
[--git-author-email GIT_AUTHOR_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
--preview-id PREVIEW_ID
--git-hash GIT_HASH --preview-id PREVIEW_ID
[-v [VERBOSE]]
options:
Expand All @@ -51,6 +53,10 @@ options:
--git-user GIT_USER Git Username
--git-email GIT_EMAIL
Git User Email
--git-author-name GIT_AUTHOR_NAME
Git Author Name
--git-author-email GIT_AUTHOR_EMAIL
Git Author Email
--organisation ORGANISATION
Apps Git organisation/projectKey
--repository-name REPOSITORY_NAME
Expand Down
12 changes: 9 additions & 3 deletions docs/commands/delete-pr-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ gitopscli delete-pr-preview \
```
usage: gitopscli delete-pr-preview [-h] --username USERNAME --password
PASSWORD [--git-user GIT_USER]
[--git-email GIT_EMAIL] --organisation
ORGANISATION --repository-name
REPOSITORY_NAME
[--git-email GIT_EMAIL]
[--git-author-name GIT_AUTHOR_NAME]
[--git-author-email GIT_AUTHOR_EMAIL]
--organisation ORGANISATION
--repository-name REPOSITORY_NAME
[--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
--branch BRANCH
Expand All @@ -38,6 +40,10 @@ options:
--git-user GIT_USER Git Username
--git-email GIT_EMAIL
Git User Email
--git-author-name GIT_AUTHOR_NAME
Git Author Name
--git-author-email GIT_AUTHOR_EMAIL
Git Author Email
--organisation ORGANISATION
Apps Git organisation/projectKey
--repository-name REPOSITORY_NAME
Expand Down
6 changes: 6 additions & 0 deletions docs/commands/delete-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ gitopscli delete-preview \
```
usage: gitopscli delete-preview [-h] --username USERNAME --password PASSWORD
[--git-user GIT_USER] [--git-email GIT_EMAIL]
[--git-author-name GIT_AUTHOR_NAME]
[--git-author-email GIT_AUTHOR_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
Expand All @@ -36,6 +38,10 @@ options:
--git-user GIT_USER Git Username
--git-email GIT_EMAIL
Git User Email
--git-author-name GIT_AUTHOR_NAME
Git Author Name
--git-author-email GIT_AUTHOR_EMAIL
Git Author Email
--organisation ORGANISATION
Apps Git organisation/projectKey
--repository-name REPOSITORY_NAME
Expand Down
25 changes: 18 additions & 7 deletions docs/commands/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,37 @@ usage: gitopscli deploy [-h] --file FILE --values VALUES
[--single-commit [SINGLE_COMMIT]]
[--commit-message COMMIT_MESSAGE] --username USERNAME
--password PASSWORD [--git-user GIT_USER]
[--git-email GIT_EMAIL] --organisation ORGANISATION
--repository-name REPOSITORY_NAME
[--git-provider GIT_PROVIDER]
[--git-email GIT_EMAIL]
[--git-author-name GIT_AUTHOR_NAME]
[--git-author-email GIT_AUTHOR_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
[--create-pr [CREATE_PR]] [--auto-merge [AUTO_MERGE]]
[--merge-method MERGE_METHOD] [-v [VERBOSE]]
[--merge-method MERGE_METHOD] [--json [JSON]]
[--pr-labels PR_LABELS]
[--merge-parameters MERGE_PARAMETERS] [-v [VERBOSE]]
options:
-h, --help show this help message and exit
--file FILE YAML file path
--values VALUES YAML/JSON object with the YAML path as key and the desired value as value
--values VALUES YAML/JSON object with the YAML path as key and the
desired value as value
--single-commit [SINGLE_COMMIT]
Create only single commit for all updates
--commit-message COMMIT_MESSAGE
Specify exact commit message of deployment commit
--username USERNAME Git username (alternative: GITOPSCLI_USERNAME env variable)
--password PASSWORD Git password or token (alternative: GITOPSCLI_PASSWORD env variable)
--username USERNAME Git username (alternative: GITOPSCLI_USERNAME env
variable)
--password PASSWORD Git password or token (alternative: GITOPSCLI_PASSWORD
env variable)
--git-user GIT_USER Git Username
--git-email GIT_EMAIL
Git User Email
--git-author-name GIT_AUTHOR_NAME
Git Author Name
--git-author-email GIT_AUTHOR_EMAIL
Git Author Email
--organisation ORGANISATION
Apps Git organisation/projectKey
--repository-name REPOSITORY_NAME
Expand Down
6 changes: 6 additions & 0 deletions docs/commands/sync-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ gitopscli sync-apps \
```
usage: gitopscli sync-apps [-h] --username USERNAME --password PASSWORD
[--git-user GIT_USER] [--git-email GIT_EMAIL]
[--git-author-name GIT_AUTHOR_NAME]
[--git-author-email GIT_AUTHOR_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
Expand All @@ -117,6 +119,10 @@ options:
--git-user GIT_USER Git Username
--git-email GIT_EMAIL
Git User Email
--git-author-name GIT_AUTHOR_NAME
Git Author Name
--git-author-email GIT_AUTHOR_EMAIL
Git Author Email
--organisation ORGANISATION
Apps Git organisation/projectKey
--repository-name REPOSITORY_NAME
Expand Down
2 changes: 2 additions & 0 deletions gitopscli/cliparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def __add_git_credentials_args(deploy_p: ArgumentParser) -> None:
def __add_git_commit_user_args(deploy_p: ArgumentParser) -> None:
deploy_p.add_argument("--git-user", help="Git Username", default="GitOpsCLI")
deploy_p.add_argument("--git-email", help="Git User Email", default="gitopscli@baloise.dev")
deploy_p.add_argument("--git-author-name", help="Git Author Name")
deploy_p.add_argument("--git-author-email", help="Git Author Email")


def __add_git_org_and_repo_args(deploy_p: ArgumentParser) -> None:
Expand Down
5 changes: 5 additions & 0 deletions gitopscli/commands/create_pr_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Args(GitApiConfig):
git_user: str
git_email: str

git_author_name: Optional[str]
git_author_email: Optional[str]

organisation: str
repository_name: str

Expand All @@ -37,6 +40,8 @@ def execute(self) -> None:
password=args.password,
git_user=args.git_user,
git_email=args.git_email,
git_author_name=args.git_author_name,
git_author_email=args.git_author_email,
organisation=args.organisation,
repository_name=args.repository_name,
git_provider=args.git_provider,
Expand Down
13 changes: 11 additions & 2 deletions gitopscli/commands/create_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import shutil
from dataclasses import dataclass
from typing import Any, Callable
from typing import Any, Callable, Optional
from gitopscli.git_api import GitApiConfig, GitRepo, GitRepoApi, GitRepoApiFactory
from gitopscli.io_api.yaml_util import update_yaml_file, YAMLException, yaml_file_dump
from gitopscli.gitops_config import GitOpsConfig
Expand All @@ -17,6 +17,9 @@ class Args(GitApiConfig):
git_user: str
git_email: str

git_author_name: Optional[str]
git_author_email: Optional[str]

organisation: str
repository_name: str

Expand Down Expand Up @@ -82,7 +85,13 @@ def execute(self) -> None:
self.__deployment_updated_callback(gitops_config.get_updated_message(context))

def __commit_and_push(self, git_repo: GitRepo, message: str) -> None:
git_repo.commit(self.__args.git_user, self.__args.git_email, message)
git_repo.commit(
self.__args.git_user,
self.__args.git_email,
self.__args.git_author_name,
self.__args.git_author_email,
message,
)
git_repo.push()

def __get_gitops_config(self) -> GitOpsConfig:
Expand Down
6 changes: 6 additions & 0 deletions gitopscli/commands/delete_pr_preview.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from typing import Optional
from gitopscli.git_api import GitApiConfig
from .delete_preview import DeletePreviewCommand
from .command import Command
Expand All @@ -10,6 +11,9 @@ class Args(GitApiConfig):
git_user: str
git_email: str

git_author_name: Optional[str]
git_author_email: Optional[str]

organisation: str
repository_name: str

Expand All @@ -27,6 +31,8 @@ def execute(self) -> None:
password=args.password,
git_user=args.git_user,
git_email=args.git_email,
git_author_name=args.git_author_name,
git_author_email=args.git_author_email,
organisation=args.organisation,
repository_name=args.repository_name,
git_provider=args.git_provider,
Expand Down
12 changes: 11 additions & 1 deletion gitopscli/commands/delete_preview.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import shutil
from typing import Optional
from dataclasses import dataclass
from gitopscli.git_api import GitApiConfig, GitRepo, GitRepoApiFactory, GitRepoApi
from gitopscli.gitops_config import GitOpsConfig
Expand All @@ -15,6 +16,9 @@ class Args(GitApiConfig):
git_user: str
git_email: str

git_author_name: Optional[str]
git_author_email: Optional[str]

organisation: str
repository_name: str

Expand Down Expand Up @@ -60,7 +64,13 @@ def __create_preview_target_git_repo_api(self, gitops_config: GitOpsConfig) -> G
)

def __commit_and_push(self, git_repo: GitRepo, message: str) -> None:
git_repo.commit(self.__args.git_user, self.__args.git_email, message)
git_repo.commit(
self.__args.git_user,
self.__args.git_email,
self.__args.git_author_name,
self.__args.git_author_email,
message,
)
git_repo.push()

@staticmethod
Expand Down
12 changes: 11 additions & 1 deletion gitopscli/commands/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@


class DeployCommand(Command):
# pylint: disable=too-many-instance-attributes
@dataclass(frozen=True)
class Args(GitApiConfig):
git_user: str
git_email: str

git_author_name: Optional[str]
git_author_email: Optional[str]

organisation: str
repository_name: str

Expand Down Expand Up @@ -119,6 +123,12 @@ def __create_pull_request_title_and_description(self, updated_values: Dict[str,
return title, description

def __commit(self, git_repo: GitRepo, message: str) -> None:
commit_hash = git_repo.commit(self.__args.git_user, self.__args.git_email, message)
commit_hash = git_repo.commit(
self.__args.git_user,
self.__args.git_email,
self.__args.git_author_name,
self.__args.git_author_email,
message,
)
if commit_hash:
self.__commit_hashes.append(commit_hash)
44 changes: 39 additions & 5 deletions gitopscli/commands/sync_apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional
from dataclasses import dataclass
from gitopscli.git_api import GitApiConfig, GitRepo, GitRepoApiFactory
from gitopscli.gitops_exception import GitOpsException
Expand All @@ -14,6 +15,9 @@ class Args(GitApiConfig):
git_user: str
git_email: str

git_author_name: Optional[str]
git_author_email: Optional[str]

organisation: str
repository_name: str

Expand All @@ -32,11 +36,25 @@ def _sync_apps_command(args: SyncAppsCommand.Args) -> None:
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)
__sync_apps(
team_config_git_repo,
root_config_git_repo,
args.git_user,
args.git_email,
args.git_author_name,
args.git_author_email,
)


# TODO: BETTER NAMES FOR STUFF HERE pylint: disable=fixme
def __sync_apps(tenant_git_repo: GitRepo, root_git_repo: GitRepo, git_user: str, git_email: str) -> None:
def __sync_apps(
tenant_git_repo: GitRepo,
root_git_repo: GitRepo,
git_user: str,
git_email: str,
git_author_name: Optional[str],
git_author_email: Optional[str],
) -> None:
logging.info("Team config repository: %s", tenant_git_repo.get_clone_url())
logging.info("Root config repository: %s", root_git_repo.get_clone_url())
root_repo = create_root_repo(root_repo=root_git_repo)
Expand All @@ -55,14 +73,30 @@ def __sync_apps(tenant_git_repo: GitRepo, root_git_repo: GitRepo, git_user: str,
logging.info("Appling changes to: %s", root_repo_tenant.file_path)
yaml_file_dump(root_repo_tenant.yaml, root_repo_tenant.file_path)
logging.info("Commiting and pushing changes to %s", root_git_repo.get_clone_url())
__commit_and_push(tenant_git_repo, root_git_repo, git_user, git_email, root_repo_tenant.file_path)
__commit_and_push(
tenant_git_repo,
root_git_repo,
git_user,
git_email,
git_author_name,
git_author_email,
root_repo_tenant.file_path,
)
else:
logging.info("No changes applied to %s", root_repo_tenant.file_path)


def __commit_and_push(
team_config_git_repo: GitRepo, root_config_git_repo: GitRepo, git_user: str, git_email: str, app_file_name: str
team_config_git_repo: GitRepo,
root_config_git_repo: GitRepo,
git_user: str,
git_email: str,
git_author_name: Optional[str],
git_author_email: Optional[str],
app_file_name: str,
) -> None:
author = team_config_git_repo.get_author_from_last_commit()
root_config_git_repo.commit(git_user, git_email, f"{author} updated " + app_file_name)
root_config_git_repo.commit(
git_user, git_email, git_author_name, git_author_email, f"{author} updated " + app_file_name
)
root_config_git_repo.push()
Loading

0 comments on commit d61deda

Please sign in to comment.