Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add Validate CI/CD #2

Merged
merged 13 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/scripts/aggregate_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import os
import sys
import yaml

from collections import defaultdict, OrderedDict


def main(input_path: str, output_path: str):
shared_prefix_traits: dict[str, dict] = {}

iter_ = os.walk(input_path)
root, dirs, files = iter_.__next__()
for d in dirs:
if (file := f"{d}.yaml") in files:
with open(os.path.join(root, file), 'r') as f:
shared_prefix_traits[d] = yaml.safe_load(f)
else:
print(f"Missing {d}.yaml")
exit(1)

for root, _, files in iter_:
signal_ids: dict[str, OrderedDict] = defaultdict(OrderedDict)
prefix = root.split('/')[-1]
for file in sorted(files):
with open(os.path.join(root, file), 'r') as f:
signal_id = file.split('.')[0]
prefixed_signal_id = f"{prefix}:{signal_id}"

signal_ids[prefixed_signal_id].update(yaml.safe_load(f))
signal_ids[prefixed_signal_id].update(shared_prefix_traits[prefix])

if not os.path.exists(output_path):
os.makedirs(output_path)

with open(f"{output_path}/registry.json", 'w') as f:
json.dump(signal_ids, f, indent=4)


if __name__ == '__main__':
main(*sys.argv[1:])
31 changes: 31 additions & 0 deletions .github/scripts/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! serde_json = "1.0.128"
//! bothan-core = "0.0.1-alpha.3"
//! ```

use std::env;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use bothan_core::registry::{Invalid, Registry};

fn main() {
let args = env::args().collect::<Vec<String>>();
if args.len() != 2 {
eprintln!("unexpected arguments: {:?}", args);
std::process::exit(1);
}

let path = Path::new(args[1].as_str());
let registry: Registry<Invalid> = {
let file = File::open(path).expect("Failed to open registry file");
let reader = BufReader::new(file);
serde_json::from_reader(reader).expect("Failed to parse registry file")
};
registry.validate().expect("Registry is invalid");

println!("Registry is valid");
}
50 changes: 50 additions & 0 deletions .github/workflows/aggregate_signals.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Aggregate Signals

on:
push:

jobs:
aggregate-signal:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.11.2'

- name: Install Python Dependencies
run: |
python -m pip install --upgrade pip
pip install pyyaml

- name: Aggregate Signals
run: python .github/scripts/aggregate_signals.py ./signals ./registry

- name: Cache Rust-Script
uses: actions/cache@v4
with:
path: ~/.cache/rust-script
key: ${{ runner.os }}-rust-script-${{ hashFiles('.github/scripts/validate.rs') }}
restore-keys: |
${{ runner.os }}-rust-script-${{ hashFiles('.github/scripts/validate.rs') }}

- name: Setup Rust Toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Install Rust Script
run: cargo install rust-script

- name: Install Dependencies
run: sudo apt update && sudo apt-get install clang -y

- name: Validate Aggregated Signals
run: .github/scripts/validate.rs registry/registry.json

- name: Commit
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: aggregated signals
RogerKSI marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 0 additions & 4 deletions CS.yaml

This file was deleted.

Loading
Loading