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 679a48a
Show file tree
Hide file tree
Showing 2 changed files with 95 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://github.com/gitapi/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)
23 changes: 23 additions & 0 deletions .github/workflows/documentation_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Documentation Check

on:
pull_request:

jobs:
docs-required:
name: 'Documentation Required'
runs-on: ubuntu-latest

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 679a48a

Please sign in to comment.