forked from packit/packit-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial functionality for OpenScanHub prototype
Fixes packit#2454
- Loading branch information
1 parent
9e2e27d
commit 632aea8
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,21 @@ | |
# SPDX-License-Identifier: MIT | ||
|
||
import logging | ||
import os | ||
from datetime import datetime, timezone | ||
from io import StringIO | ||
from logging import StreamHandler | ||
from pathlib import Path | ||
from re import search | ||
from typing import List, Tuple, Optional | ||
|
||
import requests | ||
|
||
from ogr.abstract import PullRequest | ||
from packit.config import JobConfig, PackageConfig | ||
from packit.schema import JobConfigSchema, PackageConfigSchema | ||
from packit.utils import PackitFormatter | ||
from packit_service import __version__ as ps_version | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -253,3 +258,37 @@ def pr_labels_match_configuration( | |
not configured_labels_absent | ||
or all(label not in pr_labels for label in configured_labels_absent) | ||
) | ||
|
||
|
||
def download_file(url: str, path: Path): | ||
""" | ||
Download a file from given url to the given path. | ||
Returns: | ||
True if the download was successful, False otherwise | ||
""" | ||
# TODO: use a library to make the downloads more robust (e.g. pycurl), | ||
# unify with packit code: | ||
# https://github.com/packit/packit/blob/2e75e6ff4c0cadb55da1c8daf9315e4b0a69e4a8/packit/base_git.py#L566-L583 | ||
user_agent = ( | ||
os.getenv("PACKIT_USER_AGENT") | ||
or f"packit-service/{ps_version} ([email protected])" | ||
) | ||
try: | ||
with requests.get( | ||
url, | ||
headers={"User-Agent": user_agent}, | ||
# connection and read timout | ||
timeout=(10, 30), | ||
stream=True, | ||
) as response: | ||
response.raise_for_status() | ||
with open(path, "wb") as f: | ||
for chunk in response.iter_content(chunk_size=8192): | ||
f.write(chunk) | ||
except requests.exceptions.RequestException as e: | ||
msg = f"Failed to download file from {url}" | ||
logger.debug(f"{msg}: {e!r}") | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from packit.api import PackitAPI | ||
from packit_service.models import CoprBuildTargetModel | ||
from packit_service.worker.handlers import CoprBuildEndHandler | ||
from packit_service.worker.handlers import copr | ||
from packit_service.worker.events import AbstractCoprBuildEvent | ||
from flexmock import flexmock | ||
|
||
|
||
def test_handle_scan(): | ||
srpm_mock = flexmock(url="https://some-url/my-srpm.src.rpm") | ||
flexmock(CoprBuildTargetModel).should_receive("get_by_build_id").and_return( | ||
flexmock( | ||
get_srpm_build=lambda: srpm_mock, | ||
target="fedora-rawhide-x86_64", | ||
get_project_event_model=lambda: None, | ||
) | ||
) | ||
flexmock(AbstractCoprBuildEvent).should_receive("from_event_dict").and_return( | ||
flexmock(chroot="fedora-rawhide-x86_64", build_id="123") | ||
) | ||
|
||
flexmock(copr).should_receive("download_file").once().and_return(True) | ||
flexmock(PackitAPI).should_receive("run_osh_build").once() | ||
|
||
CoprBuildEndHandler( | ||
package_config=flexmock(), | ||
job_config=flexmock(osh_diff_scan_after_copr_build=True), | ||
event={}, | ||
).handle_scan() |