Skip to content

Commit

Permalink
Use GitLab for syncing upstream->downstream.
Browse files Browse the repository at this point in the history
This pull request allows using gitlab for
syncing upstream->downstream.

The rest of function remains. Like
git clonedownstream uses dist-git

Signed-off-by: Petr "Stone" Hracek <[email protected]>
  • Loading branch information
phracek committed Jun 7, 2023
1 parent 6fed0f1 commit e0a1e03
Show file tree
Hide file tree
Showing 14 changed files with 1,383 additions and 24 deletions.
1 change: 1 addition & 0 deletions Dockerfile.generator
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RUN dnf install -y go-md2man \
python3-GitPython \
python3-requests-kerberos \
python3-pip \
python3-gitlab \
distgen

COPY ./ ${CWT_DIR}/
Expand Down
5 changes: 5 additions & 0 deletions container_workflow_tool/cli_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def get_parser(self):
parsers['git'].add_argument('--rebuild-reason', help='Use a custom reason for rebuilding')
parsers['git'].add_argument('--commit-msg', help='Use a custom message instead of the default one')
parsers['git'].add_argument('--check-script', help='Script/command to be run when checking repositories')
parsers['git'].add_argument(
'--gitlab', action='store_true', default=False,
help='File a merge request to corresponding repository instead of directly to dist-git'
)
parsers['build'].add_argument(
'--repo-url', help='Set the url of a .repo file to be used when building the image'
)
Expand Down Expand Up @@ -106,6 +110,7 @@ def git_usage(self):
--commit-msg - Use a custom message instead of the default one
--rebuild-reason - Use a custom reason for rebuilding
--check-script - Script/command to be run when checking repositories
--gitlab - Use GitLab for filling merge requests instead of direct pushing to dist-git
"""
return action_help

Expand Down
23 changes: 11 additions & 12 deletions container_workflow_tool/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from git import Repo
from git.exc import GitCommandError

import container_workflow_tool.utility as u
import container_workflow_tool.utility as utility
from container_workflow_tool.utility import RebuilderError
from container_workflow_tool.dockerfile import DockerfileHandler
from container_workflow_tool.sync import SyncHandler
Expand Down Expand Up @@ -49,7 +49,7 @@ def check_script(self, component, script_path, component_path):
self.logger.info(template.format(name=component, status="Affected"))
err = ret.stderr.decode('utf-8').strip()
if err:
self.logger.error(u._2sp(err))
self.logger.error(utility._2sp(err))
else:
self.logger.info(template.format(name=component, status="OK"))

Expand All @@ -63,7 +63,7 @@ def dist_git_changes(self, images, rebase=False):
rebase (bool, optional): Specify if a rebase should be done instead
"""
try:
for image in (images):
for image in images:
name = image["name"]
component = image["component"]
branch = image["git_branch"]
Expand Down Expand Up @@ -92,18 +92,17 @@ def dist_git_changes(self, images, rebase=False):
ups_name = name.split('-')[0]
# Clone upstream repository
ups_path = os.path.join('upstreams/', ups_name)
self._clone_upstream(url, ups_path, commands=commands)
self.clone_upstream(url, ups_path, commands=commands)
# Save the upstream commit hash
ups_hash = Repo(ups_path).commit().hexsha
self._pull_upstream(component, path, url, repo, ups_name, commands)
self.pull_upstream(component, path, url, repo, ups_name, commands)
self.df_handler.update_dockerfile(
df_path, from_tag, downstream_from=downstream_from
)
repo.git.add("Dockerfile")
# It is possible for the git repository to have no changes
if repo.is_dirty():
commit = self.get_commit_msg(rebase, image, ups_hash
)
commit = self.get_commit_msg(rebase, image, ups_hash)
if commit:
repo.git.commit("-m", commit)
else:
Expand All @@ -122,8 +121,8 @@ def _clone_downstream(self, component, branch):
self.logger.info("Using existing downstream repo: " + component)
repo = Repo(component)
else:
hostname_url = u._get_hostname_url(self.conf)
packager = u._get_packager(self.conf)
hostname_url = utility._get_hostname_url(self.conf)
packager = utility._get_packager(self.conf)
# if packager is fedpkg then namespace is `container` else `containers`
namespace = "container" if packager == "fedpkg" else "containers"
component_path = f"{namespace}/{component}"
Expand Down Expand Up @@ -162,7 +161,7 @@ def push_changes(self, tmp, images):
# commit_msg is set so it is always returned
commit = self.get_commit_msg(None, image)
repo.git.commit("-am", commit)
if self._get_unpushed_commits(repo):
if self.are_unpushed_commits_available(repo):
self.logger.info("Pushing: " + component)

repo.git.push()
Expand All @@ -176,7 +175,7 @@ def push_changes(self, tmp, images):
if failed:
self.logger.error("Failed pushing images:")
for image in failed:
self.logger.error(u._2sp(image["component"]))
self.logger.error(utility._2sp(image["component"]))
self.logger.error("Please check the failures and push the changes manually.")

# TODO: Multiple future branches?
Expand Down Expand Up @@ -204,5 +203,5 @@ def merge_future_branches(self, images):
if failed:
self.logger.error("Failed merging images:")
for image in failed:
self.logger.error(u._2sp(image["component"]))
self.logger.error(utility._2sp(image["component"]))
self.logger.error("Please check the failures and push the changes manually.")
8 changes: 4 additions & 4 deletions container_workflow_tool/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def set_commit_msg(self, msg):
"""
self.commit_msg = msg

def _do_git_reset(self, repo):
def do_git_reset(self, repo):
file_list = ['--', '.gitignore'] + self.conf.ignore_files
repo.git.reset(file_list)
# One file at a time to make sure all files get reset even on error
Expand All @@ -68,7 +68,7 @@ def _do_git_reset(self, repo):
repo.git.clean('-xfd', f)
self.logger.debug("Removing untracked ignored file: " + f)

def _clone_upstream(self, url, ups_path, commands=None):
def clone_upstream(self, url, ups_path, commands=None):
"""
:params: url is URL to repofile from upstream. https://github.com/sclorg
:param: ups_path is path where URL is cloned locally
Expand Down Expand Up @@ -229,7 +229,7 @@ def get_commit_msg(self, rebase, image=None, ups_hash=None):
commit += "\n created from upstream commit: " + ups_hash
return commit

def _pull_upstream(self, component, path, url, repo, ups_name, commands):
def pull_upstream(self, component, path, url, repo, ups_name, commands):
"""Pulls an upstream repo and copies it into downstream"""
ups_path = os.path.join('upstreams/', ups_name)
cp_path = os.path.join(ups_path, path)
Expand Down Expand Up @@ -268,7 +268,7 @@ def _pull_upstream(self, component, path, url, repo, ups_name, commands):
self.update_test_openshift_yaml(test_openshift_yaml_file, path, short_name=ups_name)

repo.git.add("*")
self._do_git_reset(repo)
self.do_git_reset(repo)
# TODO: Configurable?
df_ext = self.df_ext
df_path = os.path.join(component, "Dockerfile")
Expand Down
Loading

0 comments on commit e0a1e03

Please sign in to comment.