Skip to content
Snippets Groups Projects
Commit e081a8d8 authored by Jonas Höppner's avatar Jonas Höppner
Browse files

CI: Create integration branch optionally also when no submodule change is detected

parent 4452c5cb
No related branches found
No related tags found
1 merge request!101CI: Create pipeline to deploy changes in gitlab-ci to all subprojects, include all CI implementations from there.
...@@ -36,6 +36,13 @@ def check_if_integration_branch_is_up_to_date( ...@@ -36,6 +36,13 @@ def check_if_integration_branch_is_up_to_date(
except GitCommandError as e: except GitCommandError as e:
sys.exit("ERROR: could not clone manifest repository\n" + str(e)) sys.exit("ERROR: could not clone manifest repository\n" + str(e))
branch = common.find_gitlab_ci_integration_branch(
manifest_repo,
merge_request.source_branch
)
if branch is not None:
integration_branch = branch
# Get branches # Get branches
try: try:
integration_branch = manifest_repo.heads[integration_branch] integration_branch = manifest_repo.heads[integration_branch]
......
...@@ -21,6 +21,33 @@ def integration_branch_name(project_name, branch_name): ...@@ -21,6 +21,33 @@ def integration_branch_name(project_name, branch_name):
return "integrate/" + project_name.lower() + "/" + branch_name return "integrate/" + project_name.lower() + "/" + branch_name
def find_gitlab_ci_integration_branch(repo: Repo, branch_name):
"""
# Special handling for the gitlab-ci integration
# When the branch 'merge_request.source_branch' already starts with
# integrate/gitlab-ci we add our new commit to this branch
# Otherwise (normal behaviour) a new integration branch is created
"""
if not branch_name.startswith("integrate/gitlab-ci"):
return None
logging.debug("Integration of gitlab-ci: %s", branch_name)
for ref in repo.references:
refname = ref.name
logging.debug("Found ref: %s", refname)
if not refname.startswith("origin/"):
continue
# remove 'origin/' from the ref before compare
refname = ref.name.split("/", 1)[1]
logging.debug("Splitted refname: %s", refname)
if branch_name == refname:
integration_branch = refname
logging.debug(
"Found integration branch for gitlab-ci: %s", integration_branch
)
return integration_branch
def get_project(gitlab, project_name): def get_project(gitlab, project_name):
"""Get a GitLab project by its name (including or excluding namespace)""" """Get a GitLab project by its name (including or excluding namespace)"""
project = None project = None
...@@ -126,7 +153,7 @@ def commit_and_push(project: Project, repo: Repo, branch, message, name, email): ...@@ -126,7 +153,7 @@ def commit_and_push(project: Project, repo: Repo, branch, message, name, email):
# Push commit # Push commit
try: try:
origin = repo.remote("origin") origin = repo.remote("origin")
logging.debug("Push branch %s to %s",branch, origin) logging.debug("Push branch %s to %s", branch, origin)
origin.push(branch, force=True) origin.push(branch, force=True)
except GitCommandError as e: except GitCommandError as e:
sys.exit("ERROR: could not commit changes\n" + str(e)) sys.exit("ERROR: could not commit changes\n" + str(e))
...@@ -135,8 +162,7 @@ def commit_and_push(project: Project, repo: Repo, branch, message, name, email): ...@@ -135,8 +162,7 @@ def commit_and_push(project: Project, repo: Repo, branch, message, name, email):
revision = repo.head.commit.hexsha revision = repo.head.commit.hexsha
print("Pushed new commit:") print("Pushed new commit:")
print(project.web_url + "/-/commit/" + revision) print(project.web_url + "/-/commit/" + revision)
print(repo.git.log("--oneline", "-n", "5")) print(repo.git.show("--summary", "--decorate"))
#print(repo.git.show("--summary", "--decorate"))
return revision return revision
...@@ -163,8 +189,9 @@ def extract_message_body(msg): ...@@ -163,8 +189,9 @@ def extract_message_body(msg):
return msg return msg
def get_merge_request( project: Project, merge_request ):
""" Return a gitlab mergereqest specified either by id or by link """ def get_merge_request(project: Project, merge_request):
"""Return a gitlab mergereqest specified either by id or by link"""
# MR may also be specified as # MR may also be specified as
# SECO-Northern-Europe/yocto/infrastructure/ci-test/minimal-bar!115 # SECO-Northern-Europe/yocto/infrastructure/ci-test/minimal-bar!115
......
...@@ -9,7 +9,7 @@ import re ...@@ -9,7 +9,7 @@ import re
from pathlib import Path from pathlib import Path
from furl import furl from furl import furl
from git import GitCommandError, Repo from git import GitCommandError, Repo
from gitlab import Gitlab, GitlabGetError from gitlab import Gitlab
from lxml import etree from lxml import etree
...@@ -47,33 +47,18 @@ def integrate_into_manifest( ...@@ -47,33 +47,18 @@ def integrate_into_manifest(
# When the branch 'merge_request.source_branch' already starts with # When the branch 'merge_request.source_branch' already starts with
# integrate/gitlab-ci we add our new commit to this branch # integrate/gitlab-ci we add our new commit to this branch
# Otherwise (normal behaviour) a new integration branch is created # Otherwise (normal behaviour) a new integration branch is created
integration_branch = None integration_branch = common.find_gitlab_ci_integration_branch(
if merge_request.source_branch.startswith("integrate/gitlab-ci"): manifest_repo, merge_request.source_branch
logging.debug("Integration of gitlab-ci: %s", merge_request.source_branch) )
for ref in manifest_repo.references:
refname = ref.name
logging.debug("Found ref: %s", refname)
if not refname.startswith("origin/"):
continue
# remove 'origin/' from the ref before compare
refname = ref.name.split("/", 1)[1]
logging.debug("Splitted refname: %s", refname)
if merge_request.source_branch == refname:
logging.debug("Found integration branch for gitlab-ci")
integration_branch = refname
logging.debug("Integration branch: %s", integration_branch)
manifest_repo.git.checkout('-b', integration_branch, ref.name)
#origin = manifest_repo.remote("origin")
#fetch = origin.fetch(integration_branch)
#logging.debug("Fetch:%s", fetch)
logging.debug("Heads: %s", manifest_repo.heads)
manifest_repo.heads[integration_branch].checkout()
print(manifest_repo.git.log("--oneline", "-n", "5"))
break
if integration_branch is None: if integration_branch is not None:
manifest_repo.git.checkout(
"-b", integration_branch, "origin/{}".format(integration_branch)
)
logging.debug("Heads: %s", manifest_repo.heads)
manifest_repo.heads[integration_branch].checkout()
print(manifest_repo.git.log("--oneline", "-n", "5"))
else:
# Create integration branch (delete former one if already exists) # Create integration branch (delete former one if already exists)
integration_branch = common.integration_branch_name( integration_branch = common.integration_branch_name(
project.name, merge_request.source_branch project.name, merge_request.source_branch
...@@ -87,7 +72,6 @@ def integrate_into_manifest( ...@@ -87,7 +72,6 @@ def integrate_into_manifest(
manifest_repo.create_head(integration_branch) manifest_repo.create_head(integration_branch)
) )
# Parse manifest file # Parse manifest file
try: try:
manifest = etree.parse(manifest_filepath.as_posix()) manifest = etree.parse(manifest_filepath.as_posix())
...@@ -254,15 +238,12 @@ def main(): ...@@ -254,15 +238,12 @@ def main():
manifest_project = common.get_project(gitlab, args.manifest_project) manifest_project = common.get_project(gitlab, args.manifest_project)
project = common.get_project(gitlab, args.project) project = common.get_project(gitlab, args.project)
logging.debug("Project: %s", project.name ) logging.debug("Project: %s", project.name)
logging.debug("Merge Request: %s", args.merge_request) logging.debug("Merge Request: %s", args.merge_request)
merge_request = common.get_merge_request( project, args.merge_request) merge_request = common.get_merge_request(project, args.merge_request)
if merge_request is None: if merge_request is None:
sys.exit( sys.exit("ERROR: could not get %s %s" % (project.name, args.merge_request))
"ERROR: could not get %s %s"
% (project.name, args.merge_request)
)
manifest_revision = integrate_into_manifest( manifest_revision = integrate_into_manifest(
manifest_project=manifest_project, manifest_project=manifest_project,
......
...@@ -63,7 +63,9 @@ def update_submodule( ...@@ -63,7 +63,9 @@ def update_submodule(
# Check if revisions are different # Check if revisions are different
if submodule.hexsha == submodule_revision: if submodule.hexsha == submodule_revision:
print("Submodule is already at %s" % submodule_revision) print("Submodule is already at %s" % submodule_revision)
return (None, None, None) if not replace_existing_branch:
# TODO test this
return (None, None, None)
# Check for relative path # Check for relative path
if not submodule.url.startswith(".."): if not submodule.url.startswith(".."):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment