from bs4 import BeautifulSoup
import re


def sub_releases(page):
    """
    Take a specific branch release page and extract all the releases

    Parameters
    ----------
    page : str
        The HTML page to analyze. Should be a specific branch page of releases,
        such as https://www.elastic.co/guide/en/apm/agent/java/current/release-notes-1.x.html  # noqa E501

    Returns
    -------
    list
        A list containing the releases where each release is a string.

    Raises
    ------
    Exception
        A generic exception is raised if the page cannot be parsed or is not a release page.
    """
    versions = list()
    soup = BeautifulSoup(page, "html.parser")
    release_candidates = soup.find_all(
        "a", id=re.compile(r"release-notes-\d+\.\d+\.\d+"))
    for candidate in release_candidates:
        versions.append(candidate["id"].split("-").pop().upper())
    return versions