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

Add sanity check Github Action for Executive Votes #2542

Merged
merged 4 commits into from
Feb 5, 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
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