Commit 98b63e42 authored by Aurelien's avatar Aurelien
Browse files

Merge branch '292-update-job_customs-py-to-detect-when-job-no-have-any-label' into 'latest'

Resolve "Update job_customs.py to detect when job no have any label"

Closes #292

See merge request r2devops/hub!150
parents f1bb96d1 646b29c4
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -152,19 +152,19 @@ links_checker:
  allow_failure: true

# See https://docs.gitlab.com/ee/api/labels.html
job_labels:
job_gitlab_labels:
  image: python:3.9.1-alpine
  stage: project_setup
  variables:
    PIPENV_PIPFILE: tools/job_labels/Pipfile
    JOB_LOGFILE: "job_labels.log"
    PIPENV_PIPFILE: tools/job_gitlab_labels/Pipfile
    JOB_LOGFILE: "job_gitlab_labels.log"
  before_script:
    - pip install --ignore-installed distlib pipenv
    - pipenv install
  script:
    - pipenv run python3 tools/job_labels/job_labels.py
    - pipenv run python3 tools/job_gitlab_labels/job_gitlab_labels.py
  artifacts:
    expose_as: "job_labels"
    expose_as: "job_gitlab_labels"
    paths:
      - ${JOB_LOGFILE}
    expire_in: 30 days
+59 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import os
import logging
import sys
import yaml
from yaml import full_load, YAMLError

# Job variables
JOBS_DIR = "jobs"
@@ -15,6 +16,63 @@ LOGFILE_NAME = os.getenv("JOB_LOGFILE")
EXIT_SUCCESS = 0
EXIT_FAILURE = 1

# List of available labels for jobs
labels_list = ["GitLab","Build","Container","Docker","PHP", "Testing","Utilities","Yarn", "Dependency scan", "Security","Python","API", "Documentation","Quality","SAST","Linter","Helm","DAST","Kubernetes","NPM"]
set_labels_list = set(labels_list)

def get_conf(job_path):
    """Parse the YAML config of the job

    Args:
        job_path (string): Path to job.yml

    Returns:
        (dict): Object of parsed YAML
    """
    try:
        with open(job_path + "/" + JOB_YAML) as conf_file:
            return full_load(conf_file)
    except YAMLError as error:
        logging.error("Failed to parse job config '%s/%s", job_path,
                      JOB_YAML )
        logging.error(error)
        sys.exit(1)
    except OSError as error:
        logging.error("Failed to open and read job config '%s/%s",
                      job_path, JOB_YAML )
        logging.error(error)
        sys.exit(1)


def check_job_labels(job):
    """Check and logging if job labels are not in the knonw label set_labels_list

    Parameters:
    -----------
    job : str
        The name of the job
    Returns:
    --------
    """

    ret = EXIT_SUCCESS
    # Getting conf for indexing
    conf = get_conf(JOBS_DIR+"/"+job)
    job_labels = conf.get("labels")


    # If job has no label
    if job_labels is None:
        logging.warning(' 🚫 🏷  Missing label(s) for job Job label: "%s"',
             job)
    # Check if job lable are weel knonw
    else:
        difference_labels = [label for label in job_labels if label not in set_labels_list]
        if difference_labels != []:
            logging.warning(' ⚠️  🏷  Label(s) unknown: "%s"',difference_labels)



def check_job_yaml(job):
    """Verify the content of job.yaml for every job

@@ -130,4 +188,5 @@ if __name__ == "__main__":
            ret = EXIT_FAILURE
        elif check_job_yaml(job) != EXIT_SUCCESS:
            ret = EXIT_FAILURE
        check_job_labels(job)
    sys.exit(ret)