Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[marketplace contributions] - fix issue with support labels #27600

Merged
merged 16 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest


Expand Down Expand Up @@ -28,13 +29,69 @@
def test_get_highest_support_label(support_levels, expected_support_level):
"""
Given:
a list of support levels for packs that were changed
- a list of support levels for packs that were changed

When:
running get_highest_support_label function
- running get_highest_support_label function

Then:
make sure the highest support level is always returned
- make sure the highest support level is always returned
"""
from Utils.github_workflow_scripts.handle_external_pr import get_highest_support_label
assert get_highest_support_label(support_levels) == expected_support_level


@pytest.mark.parametrize(
'fork_owner, expected_fork_owner', [
('test', 'test'),
('xsoar-bot', 'xsoar-contrib')
]
)
def test_get_packs_support_level_label(mocker, fork_owner, expected_fork_owner):
"""
Given:
- a pack and a fork owner

When:
- running get_packs_support_level_label function

Then:
- make sure correct support label is returned.
- fork owner that is being delivered to the Checkout branch is correct.
"""
from Utils.github_workflow_scripts.handle_external_pr import get_packs_support_level_label, Checkout
from Utils.github_workflow_scripts.utils import ChangeCWD

checkout_mocker = mocker.patch.object(Checkout, '__init__', return_value=None)
mocker.patch.object(Checkout, '__enter__', return_value=None)
mocker.patch.object(Checkout, '__exit__', return_value=None)
mocker.patch.object(os, 'getenv', return_value=fork_owner)

with ChangeCWD('Utils/github_workflow_scripts/github_workflow_scripts_tests/test_files'):
assert get_packs_support_level_label(
file_paths=['Packs/Pack1/pack_metadata.json'], external_pr_branch='test'
) == 'Xsoar Support Level'

assert checkout_mocker.call_args.kwargs['fork_owner'] == expected_fork_owner


def test_get_packs_support_level_label_checkout_failed(mocker):
"""
Given:
- a pack

When:
- running get_packs_support_level_label function when Checkout fails.

Then:
- make sure correct support label is still returned.
"""
from Utils.github_workflow_scripts.handle_external_pr import get_packs_support_level_label, Checkout
from Utils.github_workflow_scripts.utils import ChangeCWD

mocker.patch.object(Checkout, '__init__', return_value=Exception('Error'))

with ChangeCWD('Utils/github_workflow_scripts/github_workflow_scripts_tests/test_files'):
assert get_packs_support_level_label(
file_paths=['Packs/Pack1/pack_metadata.json'], external_pr_branch='test'
) == 'Xsoar Support Level'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"support": "xsoar"
}
11 changes: 8 additions & 3 deletions Utils/github_workflow_scripts/handle_external_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,20 @@ def get_packs_support_level_label(file_paths: List[str], external_pr_branch: str
f'to retrieve support level of {pack_dirs_to_check_support_levels_labels}'
)
try:
fork_owner = os.getenv('GITHUB_ACTOR')
with Checkout(
repo=Repo(Path().cwd(), search_parent_directories=True),
branch_to_checkout=external_pr_branch,
fork_owner=os.getenv('GITHUB_ACTOR')
# in marketplace contributions the name of the owner should be xsoar-contrib
fork_owner=fork_owner if fork_owner != 'xsoar-bot' else 'xsoar-contrib'
):
packs_support_levels = get_packs_support_levels(pack_dirs_to_check_support_levels_labels)
except Exception as error:
packs_support_levels = set()
print(f'Received error when trying to checkout to {external_pr_branch} forked content repo\n{error=}')
# in case we were not able to checkout correctly, fallback to the files in the master branch to retrieve support labels
# in case those files exist.
print(f'Received error when trying to checkout to {external_pr_branch} \n{error=}')
print('Trying to retrieve support levels from the master branch')
packs_support_levels = get_packs_support_levels(pack_dirs_to_check_support_levels_labels)

print(f'{packs_support_levels=}')
return get_highest_support_label(packs_support_levels) if packs_support_levels else ''
Expand Down
19 changes: 19 additions & 0 deletions Utils/github_workflow_scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
from datetime import datetime
from typing import Any, Generator, Iterable, Optional, Tuple, Union
from pathlib import Path

from git import Repo

Expand Down Expand Up @@ -108,6 +109,7 @@ def __init__(self, repo: Repo, branch_to_checkout: str, fork_owner: Optional[str
url = f"https://github.com/{fork_owner}/{repo_name}"
try:
self.repo.create_remote(name=forked_remote_name, url=url)
print(f'Successfully created remote {forked_remote_name} for repo {url}')
except Exception as error:
print(f'could not create remote from {url}, {error=}')
# handle the case where the name of the forked repo is not content
Expand Down Expand Up @@ -145,3 +147,20 @@ def __exit__(self, *args):
"""Checks out the previous branch"""
self.repo.git.checkout(self._original_branch)
print(f"Checked out to original branch {self._original_branch}")


class ChangeCWD:
"""
Temporary changes the cwd to the given dir and then reverts it.
Use with 'with' statement.
"""

def __init__(self, directory):
self.current = Path().cwd()
self.directory = directory

def __enter__(self):
os.chdir(self.directory)

def __exit__(self, *args):
os.chdir(self.current)