Skip to content

Commit

Permalink
ci: check documentation for feature merge requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarvora committed Dec 11, 2023
1 parent d456b7c commit 15bc215
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/helper/documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sys
import requests
from urllib.parse import urlparse


WEBSITE_REPOS = frozenset(("india-compliance-docs",))
DOCUMENTATION_DOMAINS = frozenset(("docs.indiacompliance.app",))


def is_valid_url(url: str) -> bool:
parts = urlparse(url)
return all((parts.scheme, parts.netloc, parts.path))


def is_documentation_link(word: str) -> bool:
if not word.startswith("http") or not is_valid_url(word):
return False

parsed_url = urlparse(word)
if parsed_url.netloc in DOCUMENTATION_DOMAINS:
return True

if parsed_url.netloc == "github.com":
parts = parsed_url.path.split("/")
if (
len(parts) >= 5
and parts[1] == "resilient-tech"
and parts[2] in WEBSITE_REPOS
):
return True

return False


def contains_documentation_link(body: str) -> bool:
return any(
is_documentation_link(word)
for line in body.splitlines()
for word in line.split()
)


def check_pull_request(number: str) -> "tuple[int, str]":
response = requests.get(
f"https://api.github.com/repos/resilient-tech/india-compliance/pulls/{number}"
)
if not response.ok:
return 1, "Pull Request Not Found! ⚠️"

payload = response.json()
title = (payload.get("title") or "").lower().strip()
head_sha = (payload.get("head") or {}).get("sha")
body = (payload.get("body") or "").lower()

if (
not title.startswith("feat")
or not head_sha
or "no-docs" in body
or "backport" in body
):
return 0, "Skipping documentation checks... 🏃"

if contains_documentation_link(body):
return 0, "Documentation Link Found. You're Awesome! 🎉"

return 1, "Documentation Link Not Found! ⚠️"


if __name__ == "__main__":
exit_code, message = check_pull_request(sys.argv[1])
print(message)
sys.exit(exit_code)
24 changes: 24 additions & 0 deletions .github/workflows/docs-required.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Documentation Required

on:
pull_request:
types: [ opened, synchronize, reopened, edited ]

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: 'Setup Environment'
uses: actions/setup-python@v4
with:
python-version: '3.11'
- uses: actions/checkout@v4

- name: Validate Docs
env:
PR_NUMBER: ${{ github.event.number }}
run: |
pip install requests --quiet
python $GITHUB_WORKSPACE/.github/helper/documentation.py $PR_NUMBER

0 comments on commit 15bc215

Please sign in to comment.