Add --additional-github-domain to check-vcs-permalinks
This commit is contained in:
parent
2323ac0558
commit
1f8151aeeb
|
@ -66,6 +66,10 @@ Attempts to load all TOML files to verify syntax.
|
|||
|
||||
#### `check-vcs-permalinks`
|
||||
Ensures that links to vcs websites are permalinks.
|
||||
- `--additional-github-domain DOMAIN` - Add check for specified domain.
|
||||
Can be repeated multiple times. for example, if your company uses
|
||||
GitHub Enterprise you may use something like
|
||||
`--additional-github-domain github.example.com`
|
||||
|
||||
#### `check-xml`
|
||||
Attempts to load all xml files to verify syntax.
|
||||
|
|
|
@ -1,35 +1,50 @@
|
|||
import argparse
|
||||
import re
|
||||
import sys
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Pattern
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
GITHUB_NON_PERMALINK = re.compile(
|
||||
br'https://github.com/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+',
|
||||
)
|
||||
def _get_pattern(domain: str) -> Pattern[bytes]:
|
||||
regex = rf'https://{domain}/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+'
|
||||
return re.compile(regex.encode())
|
||||
|
||||
|
||||
def _check_filename(filename: str) -> int:
|
||||
def _check_filename(filename: str, patterns: List[Pattern[bytes]]) -> int:
|
||||
retv = 0
|
||||
with open(filename, 'rb') as f:
|
||||
for i, line in enumerate(f, 1):
|
||||
if GITHUB_NON_PERMALINK.search(line):
|
||||
sys.stdout.write(f'{filename}:{i}:')
|
||||
sys.stdout.flush()
|
||||
sys.stdout.buffer.write(line)
|
||||
retv = 1
|
||||
for pattern in patterns:
|
||||
if pattern.search(line):
|
||||
sys.stdout.write(f'{filename}:{i}:')
|
||||
sys.stdout.flush()
|
||||
sys.stdout.buffer.write(line)
|
||||
retv = 1
|
||||
return retv
|
||||
|
||||
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
parser.add_argument(
|
||||
'--additional-github-domain',
|
||||
dest='additional_github_domains',
|
||||
action='append',
|
||||
default=['github.com'],
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
patterns = [
|
||||
_get_pattern(domain)
|
||||
for domain in args.additional_github_domains
|
||||
]
|
||||
|
||||
retv = 0
|
||||
|
||||
for filename in args.filenames:
|
||||
retv |= _check_filename(filename)
|
||||
retv |= _check_filename(filename, patterns)
|
||||
|
||||
if retv:
|
||||
print()
|
||||
|
|
|
@ -22,13 +22,15 @@ def test_passing(tmpdir):
|
|||
def test_failing(tmpdir, capsys):
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('f.txt').write_binary(
|
||||
b'https://github.com/asottile/test/blob/master/foo#L1\n',
|
||||
b'https://github.com/asottile/test/blob/master/foo#L1\n'
|
||||
b'https://example.com/asottile/test/blob/master/foo#L1\n',
|
||||
)
|
||||
|
||||
assert main(('f.txt',))
|
||||
assert main(('f.txt', '--additional-github-domain', 'example.com'))
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == (
|
||||
'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n'
|
||||
'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n'
|
||||
'\n'
|
||||
'Non-permanent github link detected.\n'
|
||||
'On any page on github press [y] to load a permalink.\n'
|
||||
|
|
Loading…
Reference in New Issue