Skip to content

Instantly share code, notes, and snippets.

@jtprogru
Last active September 20, 2023 10:26
Show Gist options
  • Save jtprogru/8f6b3a85542ba09aee73b64253044e52 to your computer and use it in GitHub Desktop.
Save jtprogru/8f6b3a85542ba09aee73b64253044e52 to your computer and use it in GitHub Desktop.
Запуск salt-lint по коммиту в репозиторий
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Michael Savin <mail@jtprog.ru>
# WWW: https://jtprog.ru
import logging
import os
import subprocess
import sys
from saltlint.cli import run
REPO_BRANCH: str = os.getenv("CI_COMMIT_BRANCH", "develop")
COMMIT_SHA: str = os.getenv("CI_COMMIT_SHA", None)
DEBUG_LOG: bool = os.getenv("DEBUG_LOG", False)
root = logging.getLogger(__name__)
root.setLevel(logging.DEBUG)
if COMMIT_SHA in [None, '']:
msg = f"Не указан коммит который надо проверить \n{COMMIT_SHA=}"
root.error("[*] ERROR: {}".format(msg))
sys.exit(1)
def get_file_type(file_name: str) -> bool:
root.debug("[*] Проверка расширения файла")
extension = os.path.splitext(file_name)[1].lower()
# All file extensions is [".sls", ".jinja", ".jinja2", ".j2"]
# but I'm used only SaltStack extensions [".sls"]
if extension in [".sls"]:
root.debug("[*] Расширение файла валидно")
return True
root.debug("[*] Расширение файла не валидно")
return False
def get_list_files(commit_sha: str) -> list:
output = subprocess.check_output(['git', 'diff-tree', '--no-commit-id', '--name-only', '-r', commit_sha])
return output.decode().split()
def main() -> None:
all_file_names = get_list_files(commit_sha=COMMIT_SHA)
file_names = [name for name in all_file_names if get_file_type(name)]
files = set(file_names)
if len(files) == 0:
root.debug("[*] Проверять нечего - иди отдыхай")
sys.exit(0)
root.debug("[*] Количество файлов для проверки: {}".format(len(files)))
sys.exit(run(files))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment