Commit 38f5585e authored by Andres Gomez's avatar Andres Gomez
Browse files

ci-fairy: do not import git from the top



The GitPython module depends in the git command.

Installing git pulls quite some dependencies: eg. in a Debian Buster
system it increases the used space in ~100MB.

We may want to use some of the ci-fairy commands in systems with
constrained space. Since git is used only in some of the commands,
let's import only when it's really needed.

v2:
  - Remove the get_git() helper and patch the git module at
    sys.modules in the tests (Peter).
Signed-off-by: Andres Gomez's avatarAndres Gomez <agomez@igalia.com>
Reviewed-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
parent 97556201
Pipeline #231847 passed with stages
in 22 seconds
......@@ -130,7 +130,7 @@ ci-fairy images:
.ci-fairy-tag:
variables:
FDO_DISTRIBUTION_TAG: sha256-3cb21c3654a7ab78cb20e216c9ef46c4f357df22b27d243cc6751a39a6d96fb6
FDO_DISTRIBUTION_TAG: sha256-47e810651deea12fcf77fe9b1731dae47aa8cb8613af37af8e59ed4c8b88031d
.ci-fairy-local-image:
extends:
......@@ -268,7 +268,7 @@ test published images:
script:
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:buildah-aarch64-2020-10-30.1
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:buildah-x86_64-2020-10-30.1
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-3cb21c3654a7ab78cb20e216c9ef46c4f357df22b27d243cc6751a39a6d96fb6
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-47e810651deea12fcf77fe9b1731dae47aa8cb8613af37af8e59ed4c8b88031d
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:qemu-base-x86_64-2020-10-30.1
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:qemu-mkosi-base-x86_64-2020-10-30.1
rules:
......
......@@ -19,7 +19,7 @@ ci-fairy-base-image:
.ci-fairy-tag:
variables:
FDO_DISTRIBUTION_TAG: sha256-3cb21c3654a7ab78cb20e216c9ef46c4f357df22b27d243cc6751a39a6d96fb6
FDO_DISTRIBUTION_TAG: sha256-47e810651deea12fcf77fe9b1731dae47aa8cb8613af37af8e59ed4c8b88031d
# The actual ci-fairy image with ci-fairy installed
# This image uses the sha of the ci-fairy script itself as tag.
......
......@@ -31,6 +31,6 @@
# Variables provided by this template should be considered read-only.
#
.fdo.ci-fairy:
image: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-3cb21c3654a7ab78cb20e216c9ef46c4f357df22b27d243cc6751a39a6d96fb6
image: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-47e810651deea12fcf77fe9b1731dae47aa8cb8613af37af8e59ed4c8b88031d
variables:
FDO_DISTRIBUTION_IMAGE: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-3cb21c3654a7ab78cb20e216c9ef46c4f357df22b27d243cc6751a39a6d96fb6
\ No newline at end of file
FDO_DISTRIBUTION_IMAGE: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-47e810651deea12fcf77fe9b1731dae47aa8cb8613af37af8e59ed4c8b88031d
\ No newline at end of file
......@@ -5,7 +5,6 @@ import botocore
import click
import colored
import functools
import git
import gitdb
import gitlab
from gitlab import Gitlab
......@@ -601,6 +600,8 @@ def ci_fairy(ctx, verbose, gitlab_url, authfile):
if gitlab_url is None:
gitlab_url = os.getenv('CI_SERVER_URL')
if not gitlab_url:
import git
try:
from urllib.parse import urlparse
......@@ -1009,6 +1010,7 @@ def check_commits(ctx, commit_range, branch, signed_off_by, textwidth, junit_xml
CI environment, the FDO_UPSTREAM_REPO is added and the commit range
defaults to "upstream-repo/branchname..HEAD".
'''
import git
try:
repo = git.Repo('.', search_parent_directories=True)
......@@ -1130,6 +1132,8 @@ def check_merge_request(ctx, project, merge_request_iid, junit_xml, require_allo
logger.debug('This is not a merge pipeline, searching for MR')
sha = os.getenv('CI_COMMIT_SHA')
if not sha:
import git
try:
repo = git.Repo('.')
sha = repo.commit('HEAD').hexsha
......@@ -1215,6 +1219,8 @@ def wait_for_pipeline(ctx, project, sha, latest, interval):
if latest:
pipelines = p.pipelines.list(per_page=1)
else:
import git
try:
# If called from within a git repostory, try to resolve the
# symbolic ref correctly
......
......@@ -969,91 +969,94 @@ def test_merge_request_already_merged(gitlab, caplog, gitlab_default_env):
# fun fact: @patch is a stack, so the order of arguments matters
@patch('ci_fairy.Path.cwd', MagicMock(return_value=Path(GITLAB_TEST_PROJECT_PATH)))
@patch('ci_fairy.git')
@patch('ci_fairy.Gitlab')
def test_wait_for_pipeline(gitlab, git, caplog, gitlab_default_env):
def test_wait_for_pipeline(gitlab, caplog, gitlab_default_env):
args = ['-vv', 'wait-for-pipeline']
env = gitlab_default_env
env['USER'] = 'project12'
# a minimal git repository, we need this for the sha lookup
def mock_gitrepo(git, sha):
repo = MagicMock()
repo.rev_parse = MagicMock(return_value=sha)
git.Repo = MagicMock(return_value=repo)
git = MagicMock()
# See mock pipelines setup, they're in order
# running/failed/success for each
mock_gitrepo(git, 'dead00000000')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
with patch.dict('sys.modules', git=git):
# a minimal git repository, we need this for the sha lookup
def mock_gitrepo(git, sha):
repo = MagicMock()
repo.rev_parse = MagicMock(return_value=sha)
git.Repo = MagicMock(return_value=repo)
args = ['-vv', 'wait-for-pipeline', '--sha=dead00000001']
mock_gitrepo(git, 'dead00000001')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 1
assert f'status: {"failed":9s}' in result.output
args = ['-vv', 'wait-for-pipeline', '--sha=dead00000002']
mock_gitrepo(git, 'dead00000002')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"success":9s}' in result.output
# See mock pipelines setup, they're in order
# running/failed/success for each
mock_gitrepo(git, 'dead00000000')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
args = ['-vv', 'wait-for-pipeline', '--latest']
mock_gitrepo(git, 'dead00000003')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
args = ['-vv', 'wait-for-pipeline', '--sha=dead00000001']
mock_gitrepo(git, 'dead00000001')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 1
assert f'status: {"failed":9s}' in result.output
# Basically just an argparse check - because we don't set up the jobs in
# the pipeline we never actually invoke the interval. Something for
# later maybe
args = ['-vv', 'wait-for-pipeline', '--interval=5']
mock_gitrepo(git, 'dead00000003')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
args = ['-vv', 'wait-for-pipeline', '--sha=dead00000002']
mock_gitrepo(git, 'dead00000002')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"success":9s}' in result.output
# Fallback to GITLAB_USER_ID
# we can't remove USER because it's in the os.env, but setting it to the
# empty string is enough for it to be False
env['USER'] = ''
env['GITLAB_USER_ID'] = 'project12'
args = ['-vv', 'wait-for-pipeline']
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
args = ['-vv', 'wait-for-pipeline', '--latest']
mock_gitrepo(git, 'dead00000003')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
# Basically just an argparse check - because we don't set up
# the jobs in the pipeline we never actually invoke the
# interval. Something for later maybe
args = ['-vv', 'wait-for-pipeline', '--interval=5']
mock_gitrepo(git, 'dead00000003')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
# Fallback to GITLAB_USER_ID
# we can't remove USER because it's in the os.env, but setting it to the
# empty string is enough for it to be False
env['USER'] = ''
env['GITLAB_USER_ID'] = 'project12'
args = ['-vv', 'wait-for-pipeline']
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
# we can't remove GITLAB_USER_ID because it'll be set in the pipeline,
# but setting it to the empty string is enough for it to be False
env['USER'] = ''
env['GITLAB_USER_ID'] = ''
args = ['-vv', 'wait-for-pipeline']
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 2
assert 'Unable to find or identify project' in result.stdout
# we can't remove GITLAB_USER_ID because it'll be set in the
# pipeline, but setting it to the empty string is enough for
# it to be False
env['USER'] = ''
env['GITLAB_USER_ID'] = ''
args = ['-vv', 'wait-for-pipeline']
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 2
assert 'Unable to find or identify project' in result.stdout
args = ['-vv', 'wait-for-pipeline', f'--project={GITLAB_TEST_PROJECT_PATH}']
mock_gitrepo(git, 'dead00000003')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
args = ['-vv', 'wait-for-pipeline', f'--project={GITLAB_TEST_PROJECT_PATH}']
mock_gitrepo(git, 'dead00000003')
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert f'status: {"running":9s}' in result.output
def mock_s3_session(session):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment