From 30a54b0c8092698f23b570cf81560f8e1af3322c Mon Sep 17 00:00:00 2001 From: Johan Lorenzo Date: Mon, 18 Mar 2019 18:55:11 +0100 Subject: [PATCH] Bug 1533791 - Support "assume" scopes --- scriptworker/cot/verify.py | 17 ++++++++++++++--- scriptworker/github.py | 18 ++++++++++++++++++ scriptworker/test/test_cot_verify.py | 14 ++++++++++++++ scriptworker/test/test_github.py | 24 ++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/scriptworker/cot/verify.py b/scriptworker/cot/verify.py index bd7f0cd8..def93c00 100644 --- a/scriptworker/cot/verify.py +++ b/scriptworker/cot/verify.py @@ -30,11 +30,14 @@ from scriptworker.context import Context from scriptworker.ed25519 import ed25519_public_key_from_string, verify_ed25519_signature from scriptworker.exceptions import CoTError, BaseDownloadError, ScriptWorkerEd25519Error, ScriptWorkerGPGException -from scriptworker.github import GitHubRepository +from scriptworker.github import ( + GitHubRepository, + extract_github_repo_owner_and_name, + extract_github_repo_full_name, +) from scriptworker.gpg import get_body, GPG from scriptworker.log import contextual_log_handler from scriptworker.task import ( - extract_github_repo_owner_and_name, get_action_callback_name, get_and_check_project, get_and_check_tasks_for, @@ -1140,6 +1143,7 @@ async def _get_additional_github_releases_jsone_context(decision_link): # This can't be done at the moment because some mobile projects still rely on the # bad value 'clone_url': repo_url, + 'full_name': extract_github_repo_full_name(repo_url), 'html_url': repo_url, }, 'release': { @@ -1177,6 +1181,7 @@ def _get_additional_git_cron_jsone_context(decision_link): # This can't be done at the moment because some mobile projects still rely on the # bad value 'clone_url': repo, + 'full_name': extract_github_repo_full_name(repo), 'html_url': repo, }, 'release': { @@ -1220,6 +1225,11 @@ async def _get_additional_github_pull_request_jsone_context(decision_link): 'html_url': pull_request_data['head']['repo']['html_url'], }, 'pull_request': { + 'base': { + 'repo': { + 'full_name': pull_request_data['base']['repo']['full_name'], + }, + }, 'head': { 'ref': pull_request_data['head']['ref'], 'sha': pull_request_data['head']['sha'], @@ -1230,7 +1240,7 @@ async def _get_additional_github_pull_request_jsone_context(decision_link): # This becomes a problem if a staging release was kicked off and the PR got # updated in the meantime. 'pushed_at': get_push_date_time(task, source_env_prefix), - } + }, }, 'title': pull_request_data['title'], 'number': pull_request_number, @@ -1263,6 +1273,7 @@ async def _get_additional_github_push_jsone_context(decision_link): return { 'event': { 'repository': { + 'full_name': extract_github_repo_full_name(repo_url), 'html_url': repo_url, 'pushed_at': get_push_date_time(task, source_env_prefix), }, diff --git a/scriptworker/github.py b/scriptworker/github.py index 4ec069ce..74f5bdcd 100644 --- a/scriptworker/github.py +++ b/scriptworker/github.py @@ -160,6 +160,24 @@ def extract_github_repo_owner_and_name(url): return repo_owner, _strip_trailing_dot_git(repo_name) +def extract_github_repo_full_name(url): + """Given an URL, return the full name of it. + + The full name is ``RepoOwner/RepoName``. + + Args: + url (str): The URL to the GitHub repository + + Raises: + ValueError: on url that aren't from github + + Returns: + str: the full name. + + """ + return '/'.join(extract_github_repo_owner_and_name(url)) + + def extract_github_repo_and_revision_from_source_url(url): """Given an URL, return the repo name and who owns it. diff --git a/scriptworker/test/test_cot_verify.py b/scriptworker/test/test_cot_verify.py index 6a5af82d..5a7b083e 100644 --- a/scriptworker/test/test_cot_verify.py +++ b/scriptworker/test/test_cot_verify.py @@ -1334,6 +1334,7 @@ async def test_populate_jsone_context_github_release(mocker, mobile_chain, mobil 'event': { 'repository': { 'clone_url': 'https://github.com/mozilla-mobile/focus-android', + 'full_name': 'mozilla-mobile/focus-android', 'html_url': 'https://github.com/mozilla-mobile/focus-android', }, 'release': { @@ -1371,6 +1372,7 @@ async def test_populate_jsone_context_git_cron(mobile_chain, mobile_cron_link, h 'event': { 'repository': { 'clone_url': 'https://github.com/mozilla-mobile/focus-android', + 'full_name': 'mozilla-mobile/focus-android', 'html_url': 'https://github.com/mozilla-mobile/focus-android', }, 'release': { @@ -1413,6 +1415,7 @@ async def test_populate_jsone_context_github_push(mocker, mobile_chain, mobile_g assert context == { 'event': { 'repository': { + 'full_name': 'mozilla-mobile/focus-android', 'html_url': 'https://github.com/mozilla-mobile/focus-android', 'pushed_at': '1549022400', }, @@ -1450,6 +1453,11 @@ async def test_populate_jsone_context_github_pull_request(mocker, mobile_chain_p } github_repo_mock.get_pull_request.return_value = { + 'base': { + 'repo': { + 'full_name': 'mozilla-mobile/focus-android', + }, + }, 'head': { 'ref': 'some-branch', 'repo': { @@ -1489,6 +1497,11 @@ async def test_populate_jsone_context_github_pull_request(mocker, mobile_chain_p 'html_url': 'https://github.com/JohanLorenzo/focus-android', }, 'pull_request': { + 'base': { + 'repo': { + 'full_name': 'mozilla-mobile/focus-android', + }, + }, 'head': { 'ref': 'some-branch', 'sha': 'somerevision', @@ -1515,6 +1528,7 @@ async def test_populate_jsone_context_github_pull_request(mocker, mobile_chain_p 'tasks_for': 'github-pull-request', } + @pytest.mark.asyncio async def test_populate_jsone_context_fail(mobile_chain, mobile_github_release_link): with pytest.raises(CoTError): diff --git a/scriptworker/test/test_github.py b/scriptworker/test/test_github.py index 1ab340c6..b5ac190d 100644 --- a/scriptworker/test/test_github.py +++ b/scriptworker/test/test_github.py @@ -211,6 +211,30 @@ def test_extract_github_repo_owner_and_name(repo_url, expected_user, expected_re assert github.extract_github_repo_owner_and_name(repo_url) == (expected_user, expected_repo_name) +@pytest.mark.parametrize('repo_url, expected, raises', (( + 'https://github.com/mozilla-mobile/android-components', + 'mozilla-mobile/android-components', False +), ( + 'https://github.com/mozilla-mobile/android-components.git', + 'mozilla-mobile/android-components', False +), ( + 'https://github.com/JohanLorenzo/android-components', + 'JohanLorenzo/android-components', False +), ( + 'https://github.com/JohanLorenzo/android-components/raw/0123456789abcdef0123456789abcdef01234567/.taskcluster.yml', + 'JohanLorenzo/android-components', False +), ( + 'https://hg.mozilla.org/mozilla-central', + None, True +))) +def test_extract_github_repo_full_name(repo_url, expected, raises): + if raises: + with pytest.raises(ValueError): + github.extract_github_repo_full_name(repo_url) + else: + assert github.extract_github_repo_full_name(repo_url) == expected + + @pytest.mark.parametrize('repo_url, expected_user, expected_repo_name, raises', (( 'https://github.com/JohanLorenzo/android-components/raw/0123456789abcdef0123456789abcdef01234567/.taskcluster.yml', 'https://github.com/JohanLorenzo/android-components', '0123456789abcdef0123456789abcdef01234567', False,