From d1dbc86a2eb8995eab31f0527b5df8f1d5a73870 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sat, 10 Feb 2024 20:24:57 +0000 Subject: [PATCH] Writes a script to automate insertion of checklist --- .github/workflows/insert-checklist.yml | 50 ++++++++++++++++++++ lib/generate.py | 65 ++++++++++++++++++++++++++ lib/requirements.txt | 2 + 3 files changed, 117 insertions(+) create mode 100644 .github/workflows/insert-checklist.yml create mode 100644 lib/requirements.txt diff --git a/.github/workflows/insert-checklist.yml b/.github/workflows/insert-checklist.yml new file mode 100644 index 0000000..e329d04 --- /dev/null +++ b/.github/workflows/insert-checklist.yml @@ -0,0 +1,50 @@ +name: โ˜‘๏ธ Generate and insert markdown from YAML + +on: + workflow_dispatch: + push: + branches: [ main ] + paths: ['personal-security-checklist.yml'] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository ๐Ÿ›Ž๏ธ + uses: actions/checkout@v2 + + # Get current date-time (used for commit message) + - name: Get Date ๐Ÿ“… + id: date + run: echo "::set-output name=date::$(date +'%d-%b-%Y')" + + # Downloads + installs Python (used for running gen scripts) + - name: Set up Python ๐Ÿ + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + # Install contents of requirements.txt + - name: Install dependencies ๐Ÿ“ฅ + run: | + python -m pip install --upgrade pip + cd lib && pip install -r requirements.txt + + # The make command triggers all the Python scripts, generates output + - name: Run make command ๐Ÿ”จ + run: python lib/generate.py + + # Commit and push the outputed JSON files + - name: Commit and push generated files โคด๏ธ + run: | + git config --global user.name "Liss-Bot" + git config --global user.email "alicia-gh-bot@mail.as93.net" + git pull origin main + git add CHECKLIST.md + if git diff --staged --quiet; then + echo "Nothin new added, so nothing to commit, exiting..." + else + git commit -m "Updates checklist (auto-generated, on ${{ steps.date.outputs.date }})" + git push + fi diff --git a/lib/generate.py b/lib/generate.py index e69de29..b345a06 100644 --- a/lib/generate.py +++ b/lib/generate.py @@ -0,0 +1,65 @@ +import os +import yaml +import logging + +# Configure Logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +yaml_file_path = os.path.join(project_root, 'personal-security-checklist.yml') +markdown_file_path = os.path.join(project_root, 'CHECKLIST.md') + + +def read_yaml(file_path): + logger.info(f"Reading YAML file from {file_path}...") + with open(file_path, 'r') as file: + return yaml.safe_load(file) + +def generate_markdown_section(section): + markdown = f"## {section['title']}\n\n" + markdown += f"{section['intro']}\n\n" + markdown += "**Security** | **Priority** | **Details and Hints**\n" + markdown += "--- | --- | ---\n" + for item in section['checklist']: + markdown += f"**{item['point']}** | {item['priority']} | {item['details']}\n" + + if 'softwareLinks' in section: + software_links = [software for software in section['softwareLinks'] if 'title' in software and 'url' in software] + if software_links: + markdown += "\n### Recommended Software\n" + for software in software_links: + markdown += f"- [{software['title']}]({software['url']})\n" + + return markdown + +def insert_markdown_content(md_file_path, new_content): + start_marker, end_marker = "", "" + logger.info(f"Inserting generated markdown into {md_file_path} between markers...") + + with open(md_file_path, 'r') as file: + content = file.read() + + start_index = content.find(start_marker) + end_index = content.find(end_marker, start_index) + + if start_index == -1 or end_index == -1: + logger.error("Markers not found in the markdown file.") + return + + updated_content = content[:start_index + len(start_marker)] + "\n" + new_content + "\n" + content[end_index:] + + with open(md_file_path, 'w') as file: + file.write(updated_content) + logger.info("Markdown content successfully inserted.") + +def main(): + yaml_data = read_yaml(yaml_file_path) + markdown_content = "" + for section in yaml_data: + markdown_content += generate_markdown_section(section) + "\n\n" + insert_markdown_content(markdown_file_path, markdown_content) + logger.info("Script completed successfully!") + +if __name__ == "__main__": + main() diff --git a/lib/requirements.txt b/lib/requirements.txt new file mode 100644 index 0000000..b369901 --- /dev/null +++ b/lib/requirements.txt @@ -0,0 +1,2 @@ +PyYAML==6.0.1 +requests==2.31.0