Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting: handle request errors more gracefully for actions validation #2959

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Include test for presence of versions in snapshot ([#2888](https://github.com/nf-core/tools/pull/2888))
- Components: set correct sha before running component lint tests ([#2952](https://github.com/nf-core/tools/pull/2952))
- Less strict logo comparison ([#2956](https://github.com/nf-core/tools/pull/2956))
- Handle request errors more gracefully for actions validation ([#2959](https://github.com/nf-core/tools/pull/2959))

### Download

Expand Down
20 changes: 14 additions & 6 deletions nf_core/lint/actions_schema_validation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import glob
import logging
import os
from typing import Any, Dict, List

import jsonschema
import requests
import yaml


def actions_schema_validation(self):
def actions_schema_validation(self) -> Dict[str, List[str]]:
"""Checks that the GitHub Action workflow yml/yaml files adhere to the correct schema

nf-core pipelines use GitHub actions workflows to run CI tests, check formatting and also linting, among others.
Expand All @@ -17,8 +18,9 @@ def actions_schema_validation(self):
To pass this test, make sure that all your workflows contain the required properties ``on`` and ``jobs`` and that
all other properties are of the correct type, as specified in the schema (link above).
"""
passed = []
failed = []
passed: List[str] = []
failed: List[str] = []
warned: List[str] = []

# Only show error messages from schema
logging.getLogger("nf_core.schema").setLevel(logging.ERROR)
Expand All @@ -28,7 +30,13 @@ def actions_schema_validation(self):

# Load the GitHub workflow schema
r = requests.get("https://json.schemastore.org/github-workflow", allow_redirects=True)
schema = r.json()
# handle "Service Unavailable" error
if r.status_code not in [200, 301]:
warned.append(
f"Failed to fetch schema: Response code for `https://json.schemastore.org/github-workflow` was {r.status_code}"
)
return {"passed": passed, "failed": failed, "warned": warned}
schema: Dict[str, Any] = r.json()

# Validate all workflows against the schema
for wf_path in action_workflows:
Expand All @@ -46,7 +54,7 @@ def actions_schema_validation(self):
try:
wf_json["on"] = wf_json.pop(True)
except Exception:
failed.append("Missing 'on' keyword in {}.format(wf)")
failed.append(f"Missing 'on' keyword in {wf}")

# Validate the workflow
try:
Expand All @@ -55,4 +63,4 @@ def actions_schema_validation(self):
except Exception as e:
failed.append(f"Workflow validation failed for {wf}: {e}")

return {"passed": passed, "failed": failed}
return {"passed": passed, "failed": failed, "warned": warned}
Loading