2022-02-04 04:28:07 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-08-31 01:15:12 -07:00
|
|
|
# Copyright 2023 Google LLC
|
2022-02-04 04:28:07 -08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
'''Recursively check link destination validity in Markdown files.
|
|
|
|
|
|
|
|
This tool recursively checks that local links in Markdown files point to valid
|
|
|
|
destinations. Its main use is in CI pipelines triggered by pull requests.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import collections
|
|
|
|
import pathlib
|
2022-03-28 09:53:32 -07:00
|
|
|
import requests
|
2022-02-04 04:28:07 -08:00
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
import click
|
|
|
|
import marko
|
|
|
|
|
|
|
|
BASEDIR = pathlib.Path(__file__).resolve().parents[1]
|
|
|
|
DOC = collections.namedtuple('DOC', 'path relpath links')
|
|
|
|
LINK = collections.namedtuple('LINK', 'dest valid')
|
2022-03-28 00:43:00 -07:00
|
|
|
|
|
|
|
|
2022-03-28 09:53:32 -07:00
|
|
|
def check_link(link, readme_path, external):
|
2022-03-28 00:43:00 -07:00
|
|
|
'Checks if a link element has a valid destination.'
|
|
|
|
link_valid = None
|
|
|
|
url = urllib.parse.urlparse(link.dest)
|
2022-03-28 09:53:32 -07:00
|
|
|
# If the link is public, say the link is anyway valid
|
|
|
|
# if --external is not set; check the link otherwise
|
2022-03-28 00:43:00 -07:00
|
|
|
if url.scheme:
|
|
|
|
link_valid = True
|
2022-03-28 09:53:32 -07:00
|
|
|
if external:
|
|
|
|
try:
|
2022-03-28 11:36:51 -07:00
|
|
|
response = requests.get(link.dest)
|
|
|
|
link_valid = response.ok
|
|
|
|
except requests.exceptions.RequestException:
|
2022-03-28 09:53:32 -07:00
|
|
|
link_valid = False
|
|
|
|
# The link is private
|
2022-03-28 00:43:00 -07:00
|
|
|
else:
|
|
|
|
link_valid = (readme_path.parent / url.path).exists()
|
|
|
|
return LINK(link.dest, link_valid)
|
|
|
|
|
|
|
|
|
2022-03-28 11:33:16 -07:00
|
|
|
def check_docs(dir_name, external=False):
|
2022-03-28 00:43:00 -07:00
|
|
|
'Traverses dir_name and checks for all Markdown files.'
|
2022-02-04 04:28:07 -08:00
|
|
|
dir_path = BASEDIR / dir_name
|
2022-03-28 07:54:06 -07:00
|
|
|
parser = marko.parser.Parser()
|
2022-02-04 04:28:07 -08:00
|
|
|
for readme_path in sorted(dir_path.glob('**/*.md')):
|
|
|
|
if '.terraform' in str(readme_path) or '.pytest' in str(readme_path):
|
|
|
|
continue
|
2022-03-28 07:54:06 -07:00
|
|
|
|
|
|
|
root = parser.parse(readme_path.read_text())
|
|
|
|
elements = collections.deque([root])
|
|
|
|
links = []
|
|
|
|
while elements:
|
|
|
|
el = elements.popleft()
|
|
|
|
if isinstance(el, marko.inline.Link):
|
2022-03-28 09:53:32 -07:00
|
|
|
links.append(check_link(el, readme_path, external))
|
2022-03-28 07:54:06 -07:00
|
|
|
elif hasattr(el, 'children'):
|
|
|
|
elements.extend(el.children)
|
|
|
|
|
2022-02-04 04:28:07 -08:00
|
|
|
yield DOC(readme_path, str(readme_path.relative_to(dir_path)), links)
|
|
|
|
|
|
|
|
|
2022-02-14 03:13:42 -08:00
|
|
|
@click.command()
|
|
|
|
@click.argument('dirs', type=str, nargs=-1)
|
2022-03-28 09:53:32 -07:00
|
|
|
@click.option('-e', '--external', is_flag=True, default=False,
|
|
|
|
help='Whether to test external links.')
|
2023-08-31 01:15:12 -07:00
|
|
|
@click.option('--show-summary/--no-show-summary', default=True)
|
|
|
|
def main(dirs, external, show_summary=True):
|
2022-03-28 00:43:00 -07:00
|
|
|
'Checks links in Markdown files contained in dirs.'
|
2022-04-11 23:17:27 -07:00
|
|
|
errors = []
|
2022-02-04 04:28:07 -08:00
|
|
|
for dir_name in dirs:
|
2023-08-31 01:15:12 -07:00
|
|
|
if show_summary:
|
|
|
|
print(f'----- {dir_name} -----')
|
2022-03-28 09:53:32 -07:00
|
|
|
for doc in check_docs(dir_name, external):
|
2022-02-04 04:28:07 -08:00
|
|
|
state = '✓' if all(l.valid for l in doc.links) else '✗'
|
2023-08-31 01:15:12 -07:00
|
|
|
if show_summary:
|
|
|
|
print(f'[{state}] {doc.relpath} ({len(doc.links)})')
|
2022-02-04 04:28:07 -08:00
|
|
|
if state == '✗':
|
FAST multitenant bootstrap and resource management, rename org-level FAST stages (#1052)
* rename stages
* remove support for external org billing, rename output files
* resman: make groups optional, align on new billing account variable
* bootstrap: multitenant outputs
* tenant bootstrap stage, untested
* fix folder name
* fix stage 0 output names
* optional creation for tag keys in organization module
* single tenant bootstrap minus tag
* rename output files, add tenant tag key
* fix organization module tag values output
* test skipping creation for tags in organization module
* single tenant bootstrap plan working
* multitenant bootstrap
* tfdoc
* fix check links error messages
* fix links
* tfdoc
* fix links
* rename fast tests, fix bootstrap tests
* multitenant stages have their own folder, simplify stage numbering
* stage renumbering
* wip
* rename tests
* exclude fast providers in fixture
* stage 0 tests
* stage 1 tests
* network stages tests
* stage tests
* tfdoc
* fix links
* tfdoc
* multitenant tests
* remove local files
* stage links command
* fix links script, TODO
* wip
* wip single tenant bootstrap
* working tenant bootstrap
* update gitignore
* remove local files
* tfdoc
* remove local files
* allow tests for tenant bootstrap stage
* tenant bootstrap proxies stage 1 tfvars
* stage 2 and 3 service accounts and IAM in tenant bootstrap
* wip
* wip
* wip
* drop multitenant bootstrap
* tfdoc
* add missing stage 2 SAs, fix org-level IAM condition
* wip
* wip
* optional tag value creation in organization module
* stage 1 working
* linting
* linting
* READMEs
* wip
* Make stage-links script work in old macos bash
* stage links command help
* fix output file names
* diagrams
* fix svg
* stage 0 skeleton and diagram
* test svg
* test svg
* test diagram
* diagram
* readme
* fix stage links script
* stage 0 readme
* README changes
* stage readmes
* fix outputs order
* fix link
* fix tests
* stage 1 test
* skip stage example
* boilerplate
* fix tftest skip
* default bootstrap stage log sinks to log buckets
* add logging to tenant bootstrap
* move iam variables out of tenant config
* fix cicd, reintroduce missing variable
* use optional in stage 1 cicd variable
* rename extras stage
* rename and move identity providers local, use optional for cicd variable
* tfdoc
* add support for wif pool and providers, ci/cd
* tfdoc
* fix links
* better handling of modules repository
* add missing role on logging project
* fix cicd pools in locals, test cicd
* fix workflow extension
* fix module source replacement
* allow tenant bootstrap cicd sa to impersonate resman sa
* tenant workflow templates fix for no providers file
* fix output files, push github workflow template to new repository
* remove try from outpout files
* align stage 1 cicd internals to stage 0
* tfdoc
* tests
* fix tests
* tests
* improve variable descriptions
* use optional in fast features
* actually create tenant log sinks, and allow the resman sa to do it
* test
* tests
* aaaand tests again
* fast features tenant override
* fast features tenant override
* fix wording
* add missing comment
* configure pf service accounts
* add missing comment
* tfdoc
* tests
* IAM docs
* update copyright
---------
Co-authored-by: Julio Castillo <jccb@google.com>
2023-02-04 06:00:45 -08:00
|
|
|
error = [f'{dir_name}/{doc.relpath}']
|
2022-02-04 04:28:07 -08:00
|
|
|
for l in doc.links:
|
|
|
|
if not l.valid:
|
2022-04-11 23:17:27 -07:00
|
|
|
error.append(f' - {l.dest}')
|
2023-08-31 01:15:12 -07:00
|
|
|
if show_summary:
|
|
|
|
print(f' {l.dest}')
|
2022-04-11 23:17:27 -07:00
|
|
|
errors.append('\n'.join(error))
|
2022-02-04 04:28:07 -08:00
|
|
|
if errors:
|
2022-04-11 23:17:27 -07:00
|
|
|
raise SystemExit('Errors found:\n{}'.format('\n'.join(errors)))
|
2022-02-04 04:28:07 -08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|