Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/aap_eda/services/project/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
from __future__ import annotations

import contextlib
import io
import logging
import os
import re
import shutil
import subprocess
import tempfile
Expand Down Expand Up @@ -365,27 +363,21 @@ def __call__(
os.makedirs(env_dir)
safe_yaml.dump(os.path.join(env_dir, "extravars"), extra_vars)

outputs = io.StringIO()
with contextlib.redirect_stdout(outputs):
runner = ansible_runner.run(
private_data_dir=data_dir,
playbook=PLAYBOOK,
envvars=env_vars,
)
runner = ansible_runner.run(
private_data_dir=data_dir,
playbook=PLAYBOOK,
envvars=env_vars,
quiet=True,
)

events = list(runner.events)

if runner.rc == 0:
match = re.search(
r'"msg": "Repository Version ([0-9a-fA-F]+)"',
outputs.getvalue(),
)
if match:
return match.group(1)
match = re.search(
r'fatal: \[localhost\]: FAILED! => \{.+"msg": (.+)\}',
outputs.getvalue(),
)
if match:
err_msg = match.group(1)
git_hash = self._extract_git_hash(events)
if git_hash:
return git_hash
err_msg = self._extract_error_msg(events)
if err_msg:
if "Authentication failed" in err_msg:
raise ScmAuthenticationError("Authentication failed")
if (
Expand All @@ -395,7 +387,29 @@ def __call__(
err_msg = "Credentials not provided or incorrect"
raise ScmAuthenticationError(err_msg)
raise ScmError(f"{self.ERROR_PREFIX} {err_msg}")
raise ScmError(f"{self.ERROR_PREFIX} {outputs.getvalue().strip()}")
raise ScmError(self.ERROR_PREFIX)

@staticmethod
def _extract_git_hash(events):
for event in events:
if event.get("event") != "runner_on_ok":
continue
res = event.get("event_data", {}).get("res", {})
scm_version = res.get("ansible_facts", {}).get("scm_version")
if scm_version:
return scm_version
return None

@staticmethod
def _extract_error_msg(events):
for event in events:
if event.get("event") != "runner_on_failed":
continue
res = event.get("event_data", {}).get("res", {})
msg = res.get("msg")
if msg:
return msg
return None


def is_refspec_valid(refspec: str, is_branch: bool) -> bool:
Expand Down
Loading