def update_navigator_gists()

in detection_rules/devtools.py [0:0]


def update_navigator_gists(directory: Path, token: str, gist_id: str, print_markdown: bool,
                           update_coverage: bool) -> list:
    """Update the gists with new navigator files."""
    assert directory.exists(), f'{directory} does not exist'

    def raw_permalink(raw_link):
        # Gist file URLs change with each revision, but can be permalinked to the latest by removing the hash after raw
        prefix, _, suffix = raw_link.rsplit('/', 2)
        return '/'.join([prefix, suffix])

    file_map = {f: f.read_text() for f in directory.glob('*.json')}
    try:
        response = update_gist(token,
                               file_map,
                               description='ATT&CK Navigator layer files.',
                               gist_id=gist_id,
                               pre_purge=True)
    except requests.exceptions.HTTPError as exc:
        if exc.response.status_code == requests.status_codes.codes.not_found:
            raise client_error('Gist not found: verify the gist_id exists and the token has access to it', exc=exc)
        else:
            raise

    response_data = response.json()
    raw_urls = {name: raw_permalink(data['raw_url']) for name, data in response_data['files'].items()}

    base_url = 'https://mitre-attack.github.io/attack-navigator/#layerURL={}&leave_site_dialog=false&tabs=false'

    # pull out full and platform coverage to print on top of markdown table
    all_url = base_url.format(urllib.parse.quote_plus(raw_urls.pop('Elastic-detection-rules-all.json')))
    platforms_url = base_url.format(urllib.parse.quote_plus(raw_urls.pop('Elastic-detection-rules-platforms.json')))

    generated_urls = [all_url, platforms_url]
    markdown_links = []
    for name, gist_url in raw_urls.items():
        query = urllib.parse.quote_plus(gist_url)
        url = f'https://mitre-attack.github.io/attack-navigator/#layerURL={query}&leave_site_dialog=false&tabs=false'
        generated_urls.append(url)
        link_name = name.split('.')[0]
        markdown_links.append(f'|[{link_name}]({url})|')

    markdown = [
        f'**Full coverage**: {NAVIGATOR_BADGE}',
        '\n',
        f'**Coverage by platform**: [navigator]({platforms_url})',
        '\n',
        '| other navigator links by rule attributes |',
        '|------------------------------------------|',
    ] + markdown_links

    if print_markdown:
        click.echo('\n'.join(markdown) + '\n')

    if update_coverage:
        coverage_file_path = get_path(REPO_DOCS_DIR, 'ATT&CK-coverage.md')
        header_lines = textwrap.dedent("""# Rule coverage