Commit ec982967 authored by FulcrandG's avatar FulcrandG
Browse files

Using jinja templates instead of wrappers in builder

parent 7a86ea74
Loading
Loading
Loading
Loading
+62 −53
Original line number Diff line number Diff line
@@ -30,95 +30,104 @@ job_metadata_file = "job.yml"

gitlab_api_url = "https://gitlab.com/api/v4/"

# Index variables
# Templates variables
builder_dir = "builder"
template_dir = "templates"
template_index = "index.md.j2"
template_changelog = "changelog.md.j2"
template_license = "license.md.j2"
template_doc = "job_documentation.md.j2"
template_placeholder = "placeholder.md.j2"
template_code_owner = "code_owner.md.j2"
index_file = "index.md"
index = {"static_tests": [], "build": [], "dynamic_tests": [], "review": [], "deployment": []}
index = {
  "static_tests": [],
  "build": [],
  "dynamic_tests": [],
  "review": [],
  "deployment": []
}

def get_conf(job_path, job_name):
  # Load yaml file
  with open(job_path + "/" + job_metadata_file) as file:
    return full_load(file)

def add_description(job_path, job_name, mkdocs_job_content):
def get_description(job_path, job_name):
  # Concatenate description to final file
  with open(job_path + "/" + job_description_file) as file:
    mkdocs_job_content += file.read()
  return mkdocs_job_content

def add_changelog(job_path, job_name, mkdocs_job_content):
  # For now, we are just getting the latest release, but we will be adding a full link to the release later
  latest_release = listdir(job_path + "/" + job_changelog_dir)[-1][0:-3]
  env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
  template = env.get_template(template_changelog)
  mkdocs_job_content += template.render(latest_release=latest_release)

  for release in listdir(job_path + "/" + job_changelog_dir)[::-1]:
    with open(job_path + "/" + job_changelog_dir + "/" + release) as file:
      mkdocs_job_content += file.read()
      # TODO replace <TAG_URL> by the link to the release
  
  # Adding a new line for consistency
  mkdocs_job_content += "\n"
  return mkdocs_job_content

def add_license(job_path, job_name, mkdocs_job_content):
  env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
  template = env.get_template(template_license)
  mkdocs_job_content += template.render()

  # Concatenate license to final file
    return file.read()

def get_changelogs(job_path, job_name):
  changelogs = {}

  latest_version = listdir(job_path + "/" + job_changelog_dir)[-1][0:-3]
  ### TODO add the url of the tag in here
  latest_url = "<TAG_URL>"
  version_url = "<TAG_URL>"
  ###

  changelogs["latest"] = {
    "url": latest_url,
    "version": latest_version
  }

  for version in listdir(job_path + "/" + job_changelog_dir)[::-1]:
    with open(job_path + "/" + job_changelog_dir + "/" + version) as file:
      changelogs[version[0:-3]] = {
        "url": version_url,
        "changelog": file.read()
      }
  return changelogs

def get_license(job_path, job_name):
  with open(job_path + "/" + job_license_file) as file:
    for line in file.readlines():
      mkdocs_job_content += "    " + line
  return mkdocs_job_content
    return file.readlines()

def add_code_owner(job_path, job, mkdocs_job_content, code_owner):
def get_user(job_path, job, code_owner):
  url = gitlab_api_url + "users?username=" + code_owner

  response = requests.request("GET", url)

  if response.status_code == 200:
    user = response.json()[0]

    env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
    template = env.get_template(template_code_owner)
    mkdocs_job_content += template.render(gitlab_image=user["avatar_url"], code_owner_name=user["name"], code_owner=code_owner, code_owner_url=user["web_url"])
  return mkdocs_job_content
    return response.json()[0]
  return None

def create_job_doc(job):
  job_path = jobs_dir + "/" + job
  mkdocs_job_content = ""

  # Getting conf for indexing  
  conf = get_conf(job_path, job)
  code_owner = conf.get("code-owner")
  index[conf["default_stage"]].append(conf)

  mkdocs_file_path = mkdocs_dir + "/" + jobs_dir + "/" + conf["default_stage"] + "/" + job + ".md"

  mkdocs_job_content = add_description(job_path, job, mkdocs_job_content)
  mkdocs_job_content = add_changelog(job_path, job, mkdocs_job_content)
  mkdocs_job_content = add_license(job_path, job, mkdocs_job_content)
  code_owner = conf.get("code-owner")
  if code_owner:
    mkdocs_job_content = add_code_owner(job_path, job, mkdocs_job_content, conf["code-owner"])
  # Get variables for jinja
  description = get_description(job_path, job)
  changelogs = get_changelogs(job_path, job)
  license = get_license(job_path, job)
  user = get_user(job_path, job, conf["code-owner"])

  ### TO REMOVE
  from pprint import pprint
  pprint(changelogs)
  ###

  # Write final file
  with open(mkdocs_file_path, 'w+') as file:
    file.write(mkdocs_job_content)
    env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
    template = env.get_template(template_doc)
    file.write(template.render(
      readme = description,
      license = license,
      changelogs = changelogs,
      gitlab_image = user["avatar_url"],
      code_owner_name = user["name"],
      code_owner = code_owner,
      code_owner_url = user["web_url"]
    ))

def add_placeholder():
  # Verify that there is a .md file for every stage, or mkdocs will break
  stages = listdir(mkdocs_dir + "/" + jobs_dir)
  stages.remove(".pages")

  for stage in stages:
  for stage in index.keys():
    if len(listdir(mkdocs_dir + "/" + jobs_dir + "/" + stage)) == 1:
      # There is only the .pages file, so mkdocs will break
      with open(mkdocs_dir + "/" + jobs_dir + "/" + stage + "/" + mkdocs_placeholder_file, "w+") as file:

builder/templates/changelog.md.j2

deleted100644 → 0
+0 −2
Original line number Diff line number Diff line
# Changelog
* **[latest]**(current -> `{{ latest_release }}`) : `<TAG_URL>`
+17 −0
Original line number Diff line number Diff line
{{ readme }}

??? License\n
{% for line in license %}
    {{ line }}
{% endfor %}

# Changelog
* **[latest]({{ changelogs.latest.version }})**(current -> `{{ changelogs.latest.url }}`)
{% for version in changelogs %}
* [{{ version }}]({{ version.url }})
  {{ version.changelog }}
{% endfor %}

{% if code_owner %}
-- 🔨 Maintainer: <img src='{{ gitlab_image }}' alt='avatar' style='width: 20px; height: 20px; border-radius: 50%'> [{{ code_owner_name }}]({{ code_owner_url }}) @{{ code_owner }}
{% endif %}
 No newline at end of file

builder/templates/license.md.j2

deleted100644 → 0
+0 −1
Original line number Diff line number Diff line
??? License\n
 No newline at end of file