Skip to content

Commit

Permalink
Add set_active_docs.py to set what versions are visible
Browse files Browse the repository at this point in the history
(cherry picked from commit 2a92aef95af7079f70fd8c4812fa86e9c814d352)
  • Loading branch information
LilSpazJoekp committed Jun 18, 2021
1 parent bfcc7f0 commit e28a228
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/set_active_versions.yml
@@ -0,0 +1,19 @@
jobs:
set_active_docs:
name: Set Active Docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: pip install packaging requests
- env:
READTHEDOCS_TOKEN: ${{ secrets.READTHEDOCS_TOKEN }}
name: Set Active Docs
run: python tools/set_active_docs.py
name: Set Active Docs
on:
release:
types: [published]
89 changes: 89 additions & 0 deletions tools/set_active_docs.py
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
import os
import re
import sys
import time

import packaging.version
import requests

PROJECT = "asyncpraw"
HEADERS = {"Authorization": f"token {os.environ.get('READTHEDOCS_TOKEN')}"}


def fetch_versions():
response = requests.get(
f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions?active=true",
headers=HEADERS,
)
versions = None
if response.status_code == 200:
active_versions = response.json()
versions = [
packaging.version.parse(slug["slug"].strip("v"))
for slug in active_versions["results"]
if not slug["hidden"] and not slug["slug"] in ["current", "latest"]
]
if versions is None:
sys.stderr.write("Failed to get current active versions\n")
return versions


def main():
with open(f"{PROJECT}/const.py") as fp:
current_version = packaging.version.parse(
re.search('__version__ = "([^"]+)"', fp.read()).group(1)
)

versions = fetch_versions()
if versions is None:
return 1
max_retry_count = 5
retry_count = 0
while current_version not in versions:
versions = fetch_versions()
if versions is None:
return 1
if current_version in versions:
break
else:
if retry_count >= max_retry_count:
sys.stderr.write("Current version failed to build\n")
return 1
sys.stdout.write("Waiting 30 seconds for build to finish\n")
retry_count += 1
time.sleep(30)
aggregated_versions = {}
for version in versions:
aggregated_versions.setdefault(version.major, [])
aggregated_versions[version.major].append(version)

latest_major_versions = [
max(aggregated_versions[major]) for major in aggregated_versions
]
major_versions = [version.major for version in versions]
is_new_major = major_versions.count(current_version.major) == 1

for version in versions:
if (is_new_major and version not in latest_major_versions) or (
(version.major, version.minor)
== (
current_version.major,
current_version.minor,
)
and version.micro != current_version.micro
):
response = requests.patch(
f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions/v{version}/",
json={"active": True, "hidden": True},
headers=HEADERS,
)
if response.status_code == 204:
sys.stderr.write(f"Version {version!s} was hidden successfully\n")
else:
sys.stderr.write(f"Failed to hide version {version}\n")
return 1


if __name__ == "__main__":
sys.exit(main())

0 comments on commit e28a228

Please sign in to comment.