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

CI: Rewrite check job python file to use gitlab api (no clone anymore)

For projects like the kernel the git checkout needs a log of time and
space. Directly using the gitlab api only loads the infomation needed.
parent e60e50b1
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.
...@@ -3,10 +3,7 @@ import common ...@@ -3,10 +3,7 @@ import common
import argparse import argparse
import sys import sys
import tempfile
import logging import logging
from furl import furl
from git import GitCommandError, Repo
from gitlab import Gitlab, GitlabGetError from gitlab import Gitlab, GitlabGetError
...@@ -16,59 +13,51 @@ def check_if_integration_branch_is_up_to_date( ...@@ -16,59 +13,51 @@ def check_if_integration_branch_is_up_to_date(
project, project,
merge_request, merge_request,
): ):
gitlab = manifest_project.manager.gitlab
with tempfile.TemporaryDirectory() as manifest_dir: integration_branch = None
if merge_request.source_branch.startswith("integrate/gitlab-ci"):
# Construct clone url containing access token
clone_url = furl(manifest_project.http_url_to_repo)
clone_url.username = "gitlab-ci"
clone_url.password = gitlab.private_token
# Checkout manifest
try: try:
manifest_repo = Repo.clone_from( integration_branch = manifest_project.branches.get(
clone_url.url, manifest_dir, depth=1 merge_request.source_branch,
retry_transient_errors=True,
) )
except GitCommandError as e:
sys.exit("ERROR: could not clone manifest repository\n" + str(e))
# Handle gitlab-ci integration except GitlabGetError:
integration_branch = common.find_gitlab_ci_integration_branch( # Branch not found
manifest_repo, pass
merge_request.source_branch if integration_branch is None:
integration_branch_name = common.integration_branch_name(
project.name, merge_request.source_branch
) )
if integration_branch is None: try:
integration_branch = common.integration_branch_name( integration_branch = manifest_project.branches.get(
project.name, merge_request.source_branch integration_branch_name,
retry_transient_errors=True,
) )
except GitlabGetError:
sys.exit("ERROR: could not find integration branch\n")
logging.debug("Checking integration branch: %s", integration_branch) try:
manifest_repo.git.checkout('-b', integration_branch, "origin/{}".format(integration_branch)) integration_base_branch = manifest_project.branches.get(
integration_base, retry_transient_errors=True
# Get branches )
try: except GitlabGetError:
integration_branch = manifest_repo.heads[integration_branch] sys.exit("ERROR: could not find integration base branch\n")
except IndexError:
sys.exit("ERROR: branch '%s' not found" % integration_branch) integration_base_id = integration_base_branch.commit["id"]
try:
integration_base = manifest_repo.remote().refs[integration_base] parent = integration_branch.commit
except IndexError: parent = manifest_project.commits.get(parent["id"], retry_transient_errors=True)
sys.exit("ERROR: branch '%s' not found" % integration_base) # Loop over the commits until the integration_branch head id is found
while True:
logging.debug("Checking integration base: %s", integration_base) # The integration branch is up to date if its parent is the integration base
parent = integration_branch.commit.parents[0] logging.debug(parent.id)
while True: if parent.id == integration_base_id:
logging.debug("Parent of integration branch: %s", parent) return True
# The integration branch is up to date if its parent is the integration base if len(parent.parent_ids) == 0:
if parent == integration_base.commit: break
logging.debug("Found match") parent_id = parent.parent_ids[0] # Assume linear history
return True parent = manifest_project.commits.get(parent_id, retry_transient_errors=True)
if len(parent.parents) > 0:
parent = parent.parents[0]
else:
return False
return False return False
...@@ -132,12 +121,9 @@ def main(): ...@@ -132,12 +121,9 @@ def main():
logging.debug(args) logging.debug(args)
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)
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)
)
if check_if_integration_branch_is_up_to_date( if check_if_integration_branch_is_up_to_date(
manifest_project=manifest_project, manifest_project=manifest_project,
...@@ -147,9 +133,9 @@ def main(): ...@@ -147,9 +133,9 @@ def main():
): ):
print("Integration branch is up to date.") print("Integration branch is up to date.")
else: else:
mr_url=merge_request.web_url + "/pipelines" mr_url = merge_request.web_url + "/pipelines"
if args.parent_merge_request is not None: if args.parent_merge_request is not None:
mr_url=args.parent_merge_request + "/pipelines" mr_url = args.parent_merge_request + "/pipelines"
sys.exit( sys.exit(
"Integration branch is not up to date. Please re-run the MR pipeline:\n" "Integration branch is not up to date. Please re-run the MR pipeline:\n"
......
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