-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2542 from 0xldr/executive-markdown-checker
Add sanity check Github Action for Executive Votes
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |