Files
doczyai-pipelines/git_diff_changes.py
T
2024-06-21 15:30:57 +02:00

91 lines
3.2 KiB
Python

import os
import subprocess
import requests
BITBUCKET_REPO_OWNER = "aarete"
BITBUCKET_REPO_SLUG = "doczy.ai"
BB_PIPELINE_BRANCH = os.getenv("BB_PIPELINE_BRANCH", "DEV")
BITBUCKET_AUTH_HEADER = os.getenv("BITBUCKET_AUTH_HEADER")
from terminal import run_command, prepare_logger, decorate_warn
LOGGER = prepare_logger()
def check_bitbucket_auth_header():
if os.getenv("BITBUCKET_CI") == "true" and not BITBUCKET_AUTH_HEADER:
raise EnvironmentError("Error: BITBUCKET_AUTH_HEADER is not set.")
def get_last_successful_commit(module):
if os.getenv("BITBUCKET_CI") != "true":
rc, result = run_command("git rev-parse HEAD", log_output=True, log_cmd=True, log_prefix=f"[{module}]")
return result
for commit in subprocess.check_output(["git", "log", "--format=%h", "-n", "30"]).decode().split():
url = f"https://api.bitbucket.org/2.0/repositories/{BITBUCKET_REPO_OWNER}/{BITBUCKET_REPO_SLUG}/commit/{commit}/statuses/"
LOGGER.info(f"commit={commit}, url={url}")
basic_url_headers = {
BITBUCKET_AUTH_HEADER.split(": ")[0]: BITBUCKET_AUTH_HEADER.split(": ")[1]
}
response = requests.get(url, headers=basic_url_headers).json()
LOGGER.info(f"response={response}")
for item in response.get('values', []):
name = item['name']
if "security/snyk" in name or "iacbot" in name:
continue
commit_state = item['state']
ref_name = item['refname']
updated_on = item['updated_on']
LOGGER.info(
f"COMMIT={commit}, COMMIT_STATE={commit_state}, REF_NAME={ref_name}, NAME={name}, UPDATED_ON={updated_on}")
if commit_state == "SUCCESSFUL" and ref_name.startswith(BB_PIPELINE_BRANCH):
return commit
return commit
def get_changes_output(module, last_successful_commit):
if os.getenv("BITBUCKET_CI") == "true":
_, result = run_command(f"git diff --dirstat=files,0 {last_successful_commit} | sed -E 's/^[ 0-9.]+% //g'",
log_output=True, log_cmd=True, log_prefix=f"[{module}]")
return result
else:
_, result = run_command("git diff --dirstat=files,0 HEAD~1 | sed -E 's/^[ 0-9.]+% //g'",
log_output=True, log_cmd=True, log_prefix=f"[{module}]")
return result
def is_deploy_module(module, dir_prefix):
check_bitbucket_auth_header()
if os.getenv("IS_DEPLOY_ALL") == "true":
return True
if os.getenv("SKIP_DEPLOY"):
return False
if module == os.getenv("MODULE_NAME"):
return True
elif os.getenv("MODULE_NAME"):
return False
changes_output = get_changes_output(module, get_last_successful_commit(module))
LOGGER.info(f"changes_output={changes_output}")
special_checks = [
("textract-pipeline/terraform", "textract-pipeline/src"),
]
for mod, pattern in special_checks:
pattern_found = any(pattern in change for change in changes_output)
if module == mod and pattern_found:
LOGGER.info(decorate_warn(f"Marking {module} as changed based on the special pattern={pattern}"))
return True
if f"{dir_prefix}{module}/" in changes_output:
return True
return False