Skip to content

Commit

Permalink
Merge pull request #2542 from 0xldr/executive-markdown-checker
Browse files Browse the repository at this point in the history
Add sanity check Github Action for Executive Votes
  • Loading branch information
0xldr authored Feb 5, 2024
2 parents a4d32c7 + 392b691 commit c7a11d6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/scripts/validate_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
from github import Github
import frontmatter
from web3 import Web3

# Environment variables
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
REPO_NAME = os.getenv('GITHUB_REPOSITORY')
PR_NUMBER_STR = os.getenv('PR_NUMBER')

# Convert PR_NUMBER from string to integer
PR_NUMBER = int(PR_NUMBER_STR)

# Initialize GitHub client
g = Github(GITHUB_TOKEN)
repo = g.get_repo(REPO_NAME)
pr = repo.get_pull(PR_NUMBER)

# Function to validate markdown files
def validate_markdown(file_path):
with open(file_path, 'r') as file:
content = file.read()
post = frontmatter.loads(content)
metadata, markdown = post.metadata, post.content

errors = []
if 'title' not in metadata:
errors.append('missing title')
if 'summary' not in metadata:
errors.append('missing summary')
if 'date' not in metadata:
errors.append('missing date')
if 'address' not in metadata:
errors.append('missing mainnet address')
elif metadata['address'] != "$spell_address": # Check if it's not the placeholder
if not Web3.is_address(metadata['address']):
errors.append('invalid address format')
elif not Web3.is_checksum_address(metadata['address']):
errors.append('address is not checksummed')
# Note: If the address is "$spell_address", it's considered a valid placeholder and skipped

return errors

# Function to create a comment on the PR
def create_pr_comment(message):
pr.create_issue_comment(message)

# Collect all errors
all_errors = []

# Iterate over modified files in the PR
for file in pr.get_files():
if file.filename.startswith('governance/votes/') and file.filename.endswith('.md'):
errors = validate_markdown(file.filename)
if errors:
error_message = f"Validation errors in {file.filename}:\n" + "\n".join(f"- {error}" for error in errors)
all_errors.append(error_message)

# Create a PR comment based on the validation results
if all_errors:
comment_body = "\n\n".join(all_errors)
create_pr_comment(comment_body)
else:
create_pr_comment("All markdown files passed validation. No errors found.")
29 changes: 29 additions & 0 deletions .github/workflows/validate_markdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Validate Markdown

on:
pull_request:
paths:
- 'governance/votes/*.md'

jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
pip install PyGithub python-frontmatter requests web3
- name: Validate Markdown Files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: python .github/scripts/validate_markdown.py

0 comments on commit c7a11d6

Please sign in to comment.