Skip to content
Open
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
173 changes: 173 additions & 0 deletions .github/workflows/validate-templates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
name: Validate Templates

on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'templates/**'
- 'scripts/validate-templates.py'
- '.github/workflows/validate-templates.yml'
push:
branches:
- main
- master
paths:
- 'templates/**'
- 'scripts/validate-templates.py'
workflow_dispatch:
inputs:
template:
description: 'Specific template to validate (leave empty for all)'
required: false
default: ''

jobs:
validate:
name: Validate Agent Templates
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

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

- name: Run template validation
id: validate
run: |
chmod +x scripts/validate-templates.py
if [ -n "${{ github.event.inputs.template }}" ]; then
python scripts/validate-templates.py "templates/${{ github.event.inputs.template }}"
else
python scripts/validate-templates.py
fi

- name: Post validation results to PR
if: github.event_name == 'pull_request' && failure()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');

// Create a comment with validation failure notice
const body = `## Template Validation Failed

The template validation check has failed. Please review the workflow logs for details.

### Common Issues
- **metadata.json**: Ensure \`name\` is lowercase with spaces only (e.g., \`code review\`)
- **metadata.json**: Ensure \`version\` follows semver format (e.g., \`1.0.0\`)
- **pipeline.yaml**: Ensure \`version: 1\` is present at top level
- **pipeline.yaml**: Ensure all stages have unique names

[View workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})

---
*This comment was automatically generated by the template validation workflow.*`;

// Check if we already commented
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});

const existingComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Template Validation Failed')
);

if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});
}

# Job to detect which templates changed (for targeted validation in PRs)
detect-changes:
name: Detect Changed Templates
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
outputs:
templates: ${{ steps.changes.outputs.templates }}
has_changes: ${{ steps.changes.outputs.has_changes }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get changed templates
id: changes
run: |
# Get list of changed files in templates directory
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- 'templates/' 2>/dev/null || echo "")

if [ -z "$CHANGED_FILES" ]; then
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "templates=[]" >> $GITHUB_OUTPUT
exit 0
fi

# Extract unique template directories
TEMPLATES=$(echo "$CHANGED_FILES" | grep -oP 'templates/[^/]+' | sort -u | sed 's/templates\///' | jq -R -s -c 'split("\n") | map(select(length > 0))')

echo "Changed templates: $TEMPLATES"
echo "templates=$TEMPLATES" >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT

# Validate only changed templates (more efficient for large repos)
validate-changed:
name: Validate Changed Templates
needs: detect-changes
if: needs.detect-changes.outputs.has_changes == 'true'
runs-on: ubuntu-latest

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

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

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

- name: Validate changed templates
run: |
echo "Changed templates: ${{ needs.detect-changes.outputs.templates }}"
TEMPLATES='${{ needs.detect-changes.outputs.templates }}'

# Convert JSON array to space-separated paths
TEMPLATE_PATHS=$(echo "$TEMPLATES" | jq -r '.[] | "templates/" + .')

if [ -n "$TEMPLATE_PATHS" ]; then
python scripts/validate-templates.py $TEMPLATE_PATHS
else
echo "No templates to validate"
fi
Loading
Loading