Skip to content

Commit

Permalink
Add linting/checks against using "feature branch" references #48
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Nov 21, 2024
1 parent 43cf608 commit 09cbc88
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion benchmark_scenarios/max_ndvi_composite.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"process_graph": {
"maxndvi1": {
"process_id": "max_ndvi_composite",
"namespace": "https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/max_ndvi_composite/openeo_udp/examples/max_ndvi_composite/max_ndvi_composite.json",
"namespace": "https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/refs/heads/main/openeo_udp/examples/max_ndvi_composite/max_ndvi_composite.json",
"arguments": {
"spatial_extent": {
"west": 5.07,
Expand Down
32 changes: 32 additions & 0 deletions qa/tools/apex_algorithm_qa_tools/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import re
from pathlib import Path
from typing import Iterator

Expand Down Expand Up @@ -33,3 +34,34 @@ def candidates() -> Iterator[Path]:
return candidate

raise RuntimeError("Could not determine project root directory.")


def assert_no_github_feature_branch_refs(href: str) -> None:
"""
Check that GitHub links do not point to (ephemeral) feature branches.
"""
# TODO: automatically suggest commit hash based fix?
allowed_branches = {"main", "master"}

# Check for feature branches with explicit "refs/heads" prefix in the URL
if match := re.search("//raw.githubusercontent.com/.*/refs/heads/(.*?)/", href):
if match.group(1) not in allowed_branches:
# TODO: automatically suggest commit hash based fix?
raise ValueError(
f"Links should not point to ephemeral feature branches: found {match.group(1)!r} in {href!r}"
)

# Also check shorthand URLs without "refs/heads"
if match := re.search(
"//raw.githubusercontent.com/ESA-APEx/apex_algorithms/(.*?)/", href
):
ref = match.group(1)
# Only, allow main/master and commit hashes
if (
not re.fullmatch("[0-9a-f]+", ref)
and ref != "refs"
and ref not in allowed_branches
):
raise ValueError(
f"Links should not point to ephemeral feature branches: found {match.group(1)!r} in {href!r}"
)
6 changes: 5 additions & 1 deletion qa/tools/apex_algorithm_qa_tools/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import jsonschema
import requests
from apex_algorithm_qa_tools.common import get_project_root
from apex_algorithm_qa_tools.common import (
assert_no_github_feature_branch_refs,
get_project_root,
)
from openeo.util import TimingLogger

_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -93,6 +96,7 @@ def lint_benchmark_scenario(scenario: BenchmarkScenario):
raise ValueError(
f"Invalid github.com based namespace {namespace!r}: should be a raw URL"
)
assert_no_github_feature_branch_refs(namespace)
# Inspect openEO process definition URL
resp = requests.get(namespace)
resp.raise_for_status()
Expand Down
9 changes: 8 additions & 1 deletion qa/unittests/tests/test_algorithm_catalog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json

import pytest
from apex_algorithm_qa_tools.common import get_project_root
from apex_algorithm_qa_tools.common import (
assert_no_github_feature_branch_refs,
get_project_root,
)
from esa_apex_toolbox.algorithms import Algorithm


Expand All @@ -23,6 +26,10 @@ def test_lint_algorithm_catalog_json_file(path):
assert data["properties"]["type"] == "apex_algorithm"

assert "openeo-process" in {k["rel"] for k in data["links"]}

for link in data["links"]:
assert_no_github_feature_branch_refs(link.get("href"))

# TODO #17 more checks

algo = Algorithm.from_ogc_api_record(path)
Expand Down
37 changes: 36 additions & 1 deletion qa/unittests/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
from apex_algorithm_qa_tools.common import get_project_root
import pytest
from apex_algorithm_qa_tools.common import (
assert_no_github_feature_branch_refs,
get_project_root,
)


def test_get_project_root():
assert get_project_root().is_dir()


@pytest.mark.parametrize(
"href",
[
"https://example.com/foo/bar",
"https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/refs/heads/main/openeo_udp/examples/max_ndvi_composite/max_ndvi_composite.json",
"https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/main/openeo_udp/examples/max_ndvi_composite/max_ndvi_composite.json"
"https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/3b5a011a90f4a3050ff8fdf69ca5bc2fd1535881/openeo_udp/biopar/biopar.json",
],
)
def test_assert_no_github_feature_branch_refs_ok(href):
assert_no_github_feature_branch_refs(href)


@pytest.mark.parametrize(
["href", "expected_error"],
[
(
"https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/refs/heads/footurebranch/openeo_udp/examples/max_ndvi_composite/max_ndvi_composite.json",
"should not point to ephemeral feature branches: found 'footurebranch'",
),
(
"https://raw.githubusercontent.com/ESA-APEx/apex_algorithms/footurebranch/openeo_udp/examples/max_ndvi_composite/max_ndvi_composite.json",
"should not point to ephemeral feature branches: found 'footurebranch'",
),
],
)
def test_assert_no_github_feature_branch_refs_not_ok(href, expected_error):
with pytest.raises(ValueError, match=expected_error):
assert_no_github_feature_branch_refs(href)
9 changes: 8 additions & 1 deletion qa/unittests/tests/test_openeo_udp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json

import pytest
from apex_algorithm_qa_tools.common import get_project_root
from apex_algorithm_qa_tools.common import (
assert_no_github_feature_branch_refs,
get_project_root,
)


@pytest.mark.parametrize(
Expand All @@ -19,5 +22,9 @@ def test_lint_openeo_udp_json_file(path):
assert "description" in data
assert "parameters" in data
assert "process_graph" in data

for link in data.get("links", []):
assert_no_github_feature_branch_refs(link.get("href"))

# TODO #17 more checks
# TODO require a standardized openEO "type"? https://github.com/Open-EO/openeo-api/issues/539

0 comments on commit 09cbc88

Please sign in to comment.