Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Feb 27, 2024
1 parent cae939e commit 76b2765
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Validation

on:
push:
branches: [main]
branches:
- '*'
pull_request:
types:
- opened
Expand All @@ -14,32 +15,33 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Install pre-commit
run: pip install pre-commit==3.2.2

- name: Run pre-commit
run: pre-commit run -a

pytest:
runs-on: ubuntu-22.04
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install package
run: pip install .[dev]

- name: Pytest
run: pytest tests -rfs -vv

md-dead-link-check:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: AlexanderDokuchaev/md-dead-link-check@gha
with:
config: pyproject.toml1
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ File: tests/test_md_files/fail.md:13 • Link: a.md#fail • Error: Not found fr

## Usage

### From pip
### Github action

```bash
pip install md-dead-link-check
md-dead-link-check
```yaml
jobs:
md-dead-link-check:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: AlexanderDokuchaev/md-dead-link-check@main
```
### Pre-commit hook
Expand All @@ -35,12 +39,19 @@ Adding to your .pre-commit-config.yaml
```yaml
- repo: https://github.com/AlexanderDokuchaev/md-dead-link-check
rev: v0.1.0
rev: main
hooks:
- id: md-dead-link-check
```
### From github repo
### Install rom pip
```bash
pip install md-dead-link-check
md-dead-link-check
```

### Install github repo

```bash
git clone https://github.com/AlexanderDokuchaev/md-dead-link-check
Expand Down
27 changes: 27 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'Markdown dead link checker GitHub Action'
description: 'A GitHub Action to help you keep your Markdown files free of broken links!'
branding:
icon: 'external-link'
color: 'blue'

inputs:
config:
description: 'Config toml file'
required: false
default: 'pyproject.toml'

runs:
using: 'composite'
steps:
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install md-dead-link-check
run: pip install md-dead-link-check==0.1
shell: bash

- name: Fetch the number's square
run: md-dead-link-check --config=${{ inputs.config }}
shell: bash
3 changes: 2 additions & 1 deletion md_dead_link_check/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_config(root_dir: Path, config_path: Optional[Path]) -> Config:
setattr(config, key, value)
else:
raise ConnectionError(
f"Unexpected config key `{key}` in pyproject.toml. Available keys: [{', '.join(config.__annotations__)}]"
f"Unexpected config key `{key}` in {config_path.name}. "
f"Available keys: [{', '.join(config.__annotations__)}]"
)
return config
18 changes: 14 additions & 4 deletions md_dead_link_check/link_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ def __lt__(self, other: StatusInfo) -> bool:


def get_proxies(env: Mapping[str, Any]) -> Dict[str, Optional[str]]:
"""
Find proxies in environment.
"""
return {
"http": env.get("http_proxy", env.get("HTTP_PROXY")),
"https": env.get("https_proxy", env.get("HTTPS_PROXY")),
}


def select_proxy(url: str, proxies: Dict[str, Optional[str]]) -> Optional[str]:
"""
Select proxy setting by type by suffix of url.
"""
if url.startswith("https://"):
proxy = proxies.get("https")
else:
Expand All @@ -48,6 +54,11 @@ def select_proxy(url: str, proxies: Dict[str, Optional[str]]) -> Optional[str]:
async def process_link(
link_info: LinkInfo, session: ClientSession, proxies: Dict[str, Optional[str]], timeout: int
) -> StatusInfo:
"""
Asynchronously processes a link to check its status and gather information.
Timeout is not interpolated as error, because timeout often occur due to temporary server issues and
retrying the request might be more appropriate than treating it as an immediate failure.
"""
try:
proxy = select_proxy(link_info.link, proxies)
headers = {"User-Agent": "Mozilla/5.0"}
Expand All @@ -72,7 +83,7 @@ async def async_check_links(links: list[LinkInfo], config: Config) -> List[Statu
proxies = get_proxies(os.environ)
async with ClientSession() as session:
ret = await asyncio.gather(*[process_link(li, session, proxies, config.timeout) for li in links])
return sorted(ret)
return ret


def check_web_links(md_data: Dict[str, MarkdownInfo], config: Config, files: List[str]) -> List[StatusInfo]:
Expand Down Expand Up @@ -125,15 +136,14 @@ def check_path_links(md_data: Dict[str, MarkdownInfo], root_dir: Path, config: C

if res_path.as_posix() not in md_data:
if not abs_path.exists():
# check not md file
ret.append(StatusInfo(md_link, "Path does not exist"))
continue

if fragment and fragment not in md_data[res_path.as_posix()].fragments:
ret.append(StatusInfo(md_link, "Not found fragment"))
continue
ret.append(StatusInfo(md_link, None))
return sorted(ret)
return ret


def check_all_links(
Expand All @@ -143,4 +153,4 @@ def check_all_links(
if config.check_web_links:
status_list.extend(check_web_links(md_data, config, files))
status_list.extend(check_path_links(md_data, root_dir, config))
return status_list
return sorted(status_list)

0 comments on commit 76b2765

Please sign in to comment.