diff --git a/.github/scripts/validate_markdown.py b/.github/scripts/validate_markdown.py new file mode 100644 index 000000000..8ce277af0 --- /dev/null +++ b/.github/scripts/validate_markdown.py @@ -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.") \ No newline at end of file diff --git a/.github/workflows/validate_markdown.yml b/.github/workflows/validate_markdown.yml new file mode 100644 index 000000000..b2bde7498 --- /dev/null +++ b/.github/workflows/validate_markdown.yml @@ -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