Skip to content

Commit

Permalink
Get exclude hostnames as user input
Browse files Browse the repository at this point in the history
Handle an edge case scenario in subprocess errors
Update README.md
  • Loading branch information
dormant-user committed Oct 25, 2023
1 parent 71986ee commit e069505
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ jobs:
Use the options below to configure debug and fail state when broken links are found in the repository/wiki pages.
| option | requirement | description |
|---------|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `owner` | optional | Owner/Organization of the repository - Defaults to current owner/org name |
| `repo` | optional | Name of the repository - Defaults to current repository's name |
| `fail` | optional | If `true` (Default) the build is failed if broken links are found.<br/>If `false` the build completes successfully and warnings are provided in the logs |
| `debug` | optional | If `true` (Default is `false`) debug level logging is enabled |
| option | requirement | description |
|--------------------|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `owner` | optional | Owner/Organization of the repository - Defaults to current owner/org name |
| `repo` | optional | Name of the repository - Defaults to current repository's name |
| `fail` | optional | If `true` (Default) the build is failed if broken links are found.<br/>If `false` the build completes successfully and warnings are provided in the logs |
| `debug` | optional | If `true` (Default is `false`) debug level logging is enabled |
| `excludeHostnames` | optional | Comma separated list of hostnames to ignore when failed |

> `excludeHostnames` will perform a regex like lookup, so wildcards (*) are not required and spaces don't matter<br>

> To exclude any URL with `amazon`/`amzn` in it simply specify,<br>`excludeHostnames: "amazon,amzn"`

## [Release Notes][release-notes]
**Requirement**
Expand Down
21 changes: 13 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
name: 'none-shall-pass'
description: 'Run validations against hyperlinks in all markdown files (including Wiki pages)'
name: none-shall-pass
description: Run validations against hyperlinks in all markdown files (including Wiki pages)
inputs:
owner:
description: "Owner/Organization of the repository"
description: Owner/Organization of the repository
required: false
default: ${{ github.repository_owner }}
repo:
description: "Name of the repository"
description: Name of the repository
required: false
default: ${{ github.event.repository.name }}
fail:
description: "Flag to fail"
description: Flag to fail
required: false
default: "true"
debug:
description: "Debug flag"
description: Debug flag
required: false
default: "false"
excludeHostnames:
description: Hostnames for which the failure should be ignored
required: false
default: ""
outputs: {}
runs:
using: 'docker'
image: 'Dockerfile'
using: docker
image: Dockerfile
args:
- ${{ inputs.owner }}
- ${{ inputs.repo }}
- ${{ inputs.fail }}
- ${{ inputs.debug }}
- ${{ inputs.excludeHostnames }}
branding:
icon: check-square
color: gray-dark
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Placeholder for the version number of the package."""
version = "1.0.8"
version = "2.0a"
8 changes: 5 additions & 3 deletions src/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

from logger import LOGGER

exclusions = ["localhost", "127.0.0.1", socket.gethostbyname('localhost')]


def verify_url(hyperlink: Tuple[str, str], timeout: Tuple[int, int] = (3, 3)) -> None:
"""Make a GET request to the hyperlink for validation using requests module.
Expand All @@ -22,7 +24,7 @@ def verify_url(hyperlink: Tuple[str, str], timeout: Tuple[int, int] = (3, 3)) ->
timeout: A tuple of connect timeout and read timeout.
See Also:
- Ignores amazon and localhost URLs.
- Ignores localhost URLs.
- Ignores URLs that return 429 status code.
- Retries once with a longer connect and read timeout in case of a timeout error.
Expand All @@ -43,7 +45,7 @@ def verify_url(hyperlink: Tuple[str, str], timeout: Tuple[int, int] = (3, 3)) ->
return verify_url(hyperlink, (10, 10))
except requests.RequestException as error:
LOGGER.debug(error)
if any(map(lambda keyword: keyword in url, ("amazon", "localhost", socket.gethostbyname('localhost')))):
LOGGER.warning("[%s] - '%s' - '%s' is broken", current_process().name, text, url)
if any(map(lambda keyword: keyword in url, exclusions)):
LOGGER.warning("[%s] - '%s' - '%s' is broken but excluded", current_process().name, text, url)
else:
raise ValueError(f"[{current_process().name}] - {text!r} - {url!r} is broken")
2 changes: 1 addition & 1 deletion src/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run_command(cmd: str) -> bool:
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True, check=True)
return True
except (subprocess.CalledProcessError, subprocess.SubprocessError, Exception) as error:
if isinstance(error, subprocess.CalledProcessError):
if hasattr(error, 'output') and error.output:
result = error.output.decode(encoding='UTF-8').strip()
LOGGER.warning("[%d]: %s", error.returncode, result or error.__str__())
else:
Expand Down
6 changes: 6 additions & 0 deletions src/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
REPO = sys.argv[2]
FAIL = sys.argv[3]
DEBUG = sys.argv[4]
EXCLUDE_HOSTNAMES = sys.argv[5]

connection.exclusions.extend(list(filter(None, map(str.strip, EXCLUDE_HOSTNAMES.split(',')))))

if DEBUG == "true":
LOGGER.setLevel(level=logging.DEBUG)
else:
LOGGER.setLevel(level=logging.INFO)

LOGGER.debug(connection.exclusions)


def verify_hyperlinks_in_md(filename: str) -> None:
"""Get all hyperlinks in a markdown file and validate them in a pool of threads.
Expand Down

0 comments on commit e069505

Please sign in to comment.