0

I am trying to set ReadTheDocs.com (i.e. the commercial end of ReadTheDocs) versions' active state programatically.

This idea being that a branch, when created, has documentation built for it, and when the branch ends we delete the version documentation (or at least stop building for it).

The latter is, obviously, only cleanup and not that important (want, not need). But we'd strongly like to avoid having to use the project management interface to set each branch/version to active.

I've been trying to use the v2 REST API provided by RTD. I can extract version data from "GET https://readthedocs.com/api/v2/version/" and find the version I want to mess with, but I am unable to either send data back, or find something that lets me set Version.active=True for a given version id in their API.

I'm not hugely up on how to play with these APIs so any help would be much appreciated.

I am using python and the requests library.

Daniel
  • 11
  • 3

1 Answers1

0

I searched a solution for this, because I had the same problem at automating the build process of the documentation in connection with my Git server.

At the end I found two different ways to change the project versions and set them to active with a script. Both scripts emulate the http requests which are sent to the read-the-docs server. I have a local running instance with http (without https) and it works, but I don“t know if it works for https too. Maybe it is necessary to capture the packets via Wireshark and adapt the script.

First script (using Python):

def set_version_active_on_rtd():

    server_addr =  "http://192.168.1.100:8000"
    project_slug = "myProject"
    rtd_user = 'mylogin'
    rtd_password = 'mypassword'

    with requests.session() as s:
        url = server_addr + "/accounts/login/"
        # fetch the login page
        s.get(url)
        if 'csrftoken' in s.cookies:
            # Django 1.6 and up
            csrftoken = s.cookies['csrftoken']
        else:
            # older versions
            csrftoken = s.cookies['csrf']


        login_data = dict(login=rtd_user, password=rtd_password, csrfmiddlewaretoken=csrftoken, next='/')
        r = s.post(url, data=login_data, headers=dict(Referer=url))
        url = server_addr+"/dashboard/"+project_slug+"/versions/"
        if 'csrftoken' in s.cookies:
            # Django 1.6 and up
            csrftoken = s.cookies['csrftoken']
        else:
            # older versions
            csrftoken = s.cookies['csrf']

        '''
        These settings which are saved in version_data, are configured normally with help of the webinterface.

        To set a version active, it must be configured with 
                        'version-<version_number>':'on'
        and its privacy must be set like 
                        'privacy-<version_number>':'public'

        To disable a version, its privacy has to be set and no other entry with 'on' has to be supplied
        '''
        version_data = {'default-version': 'latest', 'version-latest': 'on', 'privacy-latest' : 'public', 'privacy-master':'public','csrfmiddlewaretoken': csrftoken}
        r = s.post(url, data = version_data, headers=dict(Referer=url))

Second script (bash and cUrl):

#!/bin/bash
RTD_SERVER='http://192.168.1.100:8000'
RTD_LOGIN='mylogin'
RTD_PASSWORD='mypassword'
RTD_SLUG='myProject'

#fetch login page and save first cookie
curl -c cookie1.txt "$RTD_SERVER"/accounts/login/ > /dev/null

#extract token from first cookie
TOKEN1=$(tail -n1 cookie1.txt | awk 'NF>1{print $NF}')

#login and send first cookie and save second cookie
curl -b cookie1.txt -c cookie2.txt -X POST -d 
"csrfmiddlewaretoken=$TOKEN1&login=$RTD_LOGIN&\
password=$RTD_PASSWORD&next=/dashboard/"$RTD_SLUG"/versions/" 
"$RTD_SERVER"/accounts/login/ > /dev/null

#extract token from second cookie
TOKEN2=$(tail -n3 cookie2.txt | awk 'NF>1{print $NF}' | head -n1)

# send data for the versions to the rtd server using the second cookie
curl -b cookie2.txt -X POST -d "csrfmiddlewaretoken=$TOKEN2&default- 
version=latest&version-master=on&privacy-master=public&\
version-latest=on&privacy-latest=public" 
$RTD_SERVER"/dashboard/"$RTD_SLUG"/versions/ > /dev/null

#delete the cookies
rm cookie1.txt cookie2.txt

To set a default-version, it can be necessary to run the script twice, if the version was not set to active. At the first run for activating the version, and at the second run to set it as default-version.

Hope it helps

A. L
  • 131
  • 2
  • 12