-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#50) fix: do not prepend full build urls with jenkins base
- Loading branch information
Showing
3 changed files
with
86 additions
and
25 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
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,36 @@ | ||
from typing import cast | ||
from urllib.parse import urlparse | ||
|
||
from meiga import Result, Error | ||
|
||
def validate_jenkins_base(jenkins_base: str) -> Result[str, Error]: | ||
base = urlparse(jenkins_base) | ||
if not base.scheme: | ||
return Result(failure=Error('jenkins base should have scheme')) | ||
if not base.netloc: | ||
return Result(failure=Error('jenkins base should have domain')) | ||
return Result(success=jenkins_base) | ||
|
||
|
||
def normalize_build_url(url: str, jenkins_base: str) -> Result[str, Error]: | ||
parsed = urlparse(url) | ||
if parsed.scheme: | ||
# got full http://other.jenkins/x, possibly on different domain than `jenkins_base`` | ||
url = parsed.geturl() | ||
else: | ||
validation = validate_jenkins_base(jenkins_base) | ||
if validation.is_failure: | ||
return validation | ||
base = cast(str, validation.unwrap()) | ||
|
||
if not base.endswith('/'): | ||
base += '/' | ||
url = base + url | ||
# TODO: strip all suffixes with regexp or something... | ||
# or make sure that jenkins can handle build/1337/junit/api/json and drop this altogether | ||
url = (url | ||
.rstrip('/consoleFull') | ||
.rstrip('/console') | ||
.rstrip('/') | ||
) | ||
return Result(success=url) |
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 |
---|---|---|
@@ -1,10 +1,42 @@ | ||
from jenot import normalize_build_url | ||
import pytest | ||
|
||
def test__normalize_build_url() -> None: | ||
JENKINS = 'http://jenkins' | ||
from jenot.url import normalize_build_url | ||
|
||
@pytest.mark.parametrize('host', ['http://jenkins.local']) | ||
@pytest.mark.parametrize('port', ['', ':8080']) | ||
@pytest.mark.parametrize('prefix', ['', '/jenkins']) | ||
def test__normalize_build_url__concat(host: str, port: str, prefix: str) -> None: | ||
JENKINS = f'{host}{port}{prefix}' | ||
BUILD = 'job/kek/123' | ||
RESULT = f'{JENKINS}/{BUILD}' | ||
assert normalize_build_url(f'{BUILD}', JENKINS).unwrap() == RESULT | ||
assert normalize_build_url(f'{BUILD}/', JENKINS).unwrap() == RESULT | ||
assert normalize_build_url(f'{BUILD}/console', JENKINS).unwrap() == RESULT | ||
|
||
|
||
@pytest.mark.parametrize('host', ['http://jenkins.local']) | ||
@pytest.mark.parametrize('port', ['', ':8080']) | ||
@pytest.mark.parametrize('prefix', ['', '/jenkins']) | ||
def test__normalize_build_url__same_domain(host: str, port: str, prefix: str) -> None: | ||
JENKINS = f'{host}{port}{prefix}' | ||
BUILD = 'job/kek/123' | ||
RESULT = f'{JENKINS}/{BUILD}' | ||
assert normalize_build_url(f'{JENKINS}/{BUILD}', JENKINS).unwrap() == RESULT | ||
assert normalize_build_url(f'{JENKINS}/{BUILD}/', JENKINS).unwrap() == RESULT | ||
assert normalize_build_url(f'{JENKINS}/{BUILD}/console', JENKINS).unwrap() == RESULT | ||
|
||
|
||
@pytest.mark.parametrize('host', ['http://jenkins.local']) | ||
@pytest.mark.parametrize('port', ['', ':8080']) | ||
@pytest.mark.parametrize('prefix', ['', '/jenkins']) | ||
@pytest.mark.parametrize('other_host', ['http://company']) | ||
@pytest.mark.parametrize('other_port', ['', ':8081']) | ||
@pytest.mark.parametrize('other_prefix', ['', '/other']) | ||
def test__normalize_build_url__different_domain(host: str, port: str, prefix: str, other_host: str, other_port: str, other_prefix: str) -> None: | ||
JENKINS = f'{host}{port}{prefix}' | ||
OTHER = f'{other_host}{other_port}{other_prefix}' | ||
BUILD = 'job/kek/123' | ||
assert normalize_build_url(f'{BUILD}', JENKINS) == f'{JENKINS}/{BUILD}' | ||
assert normalize_build_url(f'{BUILD}/consoleFull', JENKINS) == f'{JENKINS}/{BUILD}' | ||
assert normalize_build_url(f'{BUILD}/console', JENKINS) == f'{JENKINS}/{BUILD}' | ||
assert normalize_build_url(f'{JENKINS}/{BUILD}/console', JENKINS) == f'{JENKINS}/{BUILD}' | ||
assert normalize_build_url(f'{JENKINS}/{BUILD}', JENKINS) == f'{JENKINS}/{BUILD}' | ||
RESULT = f'{OTHER}/{BUILD}' | ||
assert normalize_build_url(f'{OTHER}/{BUILD}', JENKINS).unwrap() == RESULT | ||
assert normalize_build_url(f'{OTHER}/{BUILD}/', JENKINS).unwrap() == RESULT | ||
assert normalize_build_url(f'{OTHER}/{BUILD}/console', JENKINS).unwrap() == RESULT |