Skip to content

Commit

Permalink
Ugly hack to workaround the yaml parsing issue with on: keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
jboursier-mwb committed Sep 27, 2022
1 parent d0136b8 commit 451f467
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/ghas_cli/utils/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,31 @@ def get_languages(
return list(lang)


def load_codeql_base64_template(language) -> tuple:
def load_codeql_base64_template(language: str, default_branch: str = "main") -> tuple:
language = language.lower()
try:
with open(f"./templates/codeql-analysis-{language.lower()}.yml", "r") as f:
template = f.read()
# Ugly af but `yaml` transforms `on:` to `True:` which is obviously annoying to parse Github Actions files..
template = f.readlines()
template_new = ""
for l in template:
if l == ' branches: ["main"]\n':
template_new += f" branches: ['{default_branch}']\n"
else:
template_new += l
except Exception as e:
with open(f"./templates/codeql-analysis-default.yml", "r") as f:
template = f.read()
language = "default"
return language, str(base64.b64encode(template.encode(encoding="utf-8")), "utf-8")
template = f.readlines()
template_new = ""
for l in template:
if l == ' branches: ["main"]\n':
template_new += f" branches: ['{default_branch}']\n"
else:
template_new += l
return language, str(
base64.b64encode(template_new.encode(encoding="utf-8")), "utf-8"
)


def create_codeql_pr(
Expand Down Expand Up @@ -424,7 +439,7 @@ def create_codeql_pr(
)

for language in languages:
lang, template = load_codeql_base64_template(language)
lang, template = load_codeql_base64_template(language, default_branch)
payload = {
"message": f"Enable CodeQL analysis for {language}",
"content": template,
Expand Down

0 comments on commit 451f467

Please sign in to comment.