Skip to content

Commit

Permalink
feat: report fetch-service rejections
Browse files Browse the repository at this point in the history
When creating the Craft manifest the FetchService will now also print a report
of those dependencies that were rejected by the fetch-service. This includes
the dependencies' urls and the list of rejection reasons.

Fixes #3539
  • Loading branch information
tigarmo committed Oct 11, 2024
1 parent 5d31349 commit 480dee1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
21 changes: 20 additions & 1 deletion craft_application/services/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import pathlib
import subprocess
import typing
from functools import partial

import craft_providers
from craft_cli import emit
from typing_extensions import override

from craft_application import fetch, models, services
from craft_application import fetch, models, services, util
from craft_application.models.manifest import CraftManifest, ProjectManifest

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -173,3 +174,21 @@ def _create_craft_manifest(

with manifest_path.open("w") as f:
json.dump(data, f, ensure_ascii=False, indent=2)

deps = craft_manifest.dependencies
rejections = [dep for dep in deps if dep.rejected]

if rejections:
display = partial(emit.progress, permanent=True)
items: list[dict[str, typing.Any]] = []
for rejection in rejections:
url = rejection.url[0] if len(rejection.url) == 1 else rejection.url
items.append({"url": url, "reasons": rejection.rejection_reasons})
text = util.dump_yaml(items)

display(
"The following artifacts were marked as rejected by the fetch-service:"
)
for line in text.splitlines():
display(line)
display("This build will fail on 'strict' fetch-service sessions.")
30 changes: 29 additions & 1 deletion tests/unit/services/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import contextlib
import json
import re
import textwrap
from datetime import datetime
from unittest import mock
from unittest.mock import MagicMock, call
Expand Down Expand Up @@ -108,7 +109,13 @@ def test_create_project_manifest_not_managed(fetch_service, tmp_path, monkeypatc


def test_teardown_session_create_manifest(
fetch_service, tmp_path, mocker, manifest_data_dir, monkeypatch, fake_project
fetch_service,
tmp_path,
mocker,
manifest_data_dir,
monkeypatch,
fake_project,
emitter,
):
monkeypatch.chdir(tmp_path)

Expand Down Expand Up @@ -142,6 +149,27 @@ def temporarily_pull_file(*, source, missing_ok):

assert obtained_file.read_text() + "\n" == expected_file.read_text()

expected_output = textwrap.dedent(
"""\
The following artifacts were marked as rejected by the fetch-service:
- url: https://github.com:443/canonical/sphinx-docs-starter-pack.git/git-upload-pack
reasons:
- fetch is allowed only on a single ref
- fetch is only allowed with depth 1
- git repository does not contain a go.mod file
- url: https://proxy.golang.org:443/github.com/go-mmap/mmap/@v/v0.7.0.mod
reasons:
- the artefact format is unknown
- the request was not recognized by any format inspector
This build will fail on 'strict' fetch-service sessions.
"""
)
for line in expected_output.splitlines():
emitter.assert_progress(
line,
permanent=True,
)


@pytest.mark.parametrize("run_on_host", [True, False])
def test_warning_experimental(mocker, fetch_service, run_on_host, emitter):
Expand Down

0 comments on commit 480dee1

Please sign in to comment.