Skip to content

Commit

Permalink
Merge pull request #256 from codemagic-ci-cd/improvement/disallow-emp…
Browse files Browse the repository at this point in the history
…ty-device-list

Improvement: Show error message if devices are not provided on development or ad-hoc profile creation
  • Loading branch information
VeArnold authored Aug 29, 2022
2 parents 3f8be16 + 31c068a commit 58a7836
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 0.31.3
-------------

**Bugfixes**
- Show error message when no matching test device is found in the Apple Developer Portal when creating an Ad Hoc or development provisioning profile. [PR #256](https://github.com/codemagic-ci-cd/cli-tools/pull/256)

Version 0.31.2
-------------

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "codemagic-cli-tools"
version = "0.31.2"
version = "0.31.3"
description = "CLI tools used in Codemagic builds"
readme = "README.md"
authors = [
Expand Down
16 changes: 16 additions & 0 deletions src/codemagic/apple/app_store_connect/provisioning/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ def create(self,
"""
if profile_type.devices_not_allowed() and devices:
raise ValueError(f'Cannot assign devices to profile with type {profile_type}')
elif profile_type.devices_required() and not devices:
if profile_type.is_tvos_profile:
device_type = 'tvOS'
elif profile_type.is_macos_profile:
device_type = 'macOS'
elif profile_type.is_ios_profile:
device_type = 'iOS'
else:
raise ValueError(f'Device type for profile type {profile_type} is unknown')

raise ValueError(
f'Cannot create profile: the request does not include any {device_type} testing devices '
f'while they are required for creating a {profile_type} profile. If the profile creation is automatic, '
'ensure that at least one suitable testing device is registered on the Apple Developer Portal.',
)

if devices is None:
devices = []
attributes = {
Expand Down
17 changes: 16 additions & 1 deletion src/codemagic/apple/resources/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from typing import Tuple

from codemagic.cli import Colors
from codemagic.models.enums import ResourceEnum
from codemagic.utilities import log


class AppStoreState(ResourceEnum):
Expand Down Expand Up @@ -205,6 +207,10 @@ def is_ad_hoc_type(self) -> bool:
def is_development_type(self) -> bool:
return self.value.endswith('_DEVELOPMENT')

@property
def is_ios_profile(self) -> bool:
return self.value.startswith('IOS_')

@property
def is_macos_profile(self) -> bool:
return self.value.startswith('MAC_')
Expand All @@ -214,9 +220,18 @@ def is_tvos_profile(self) -> bool:
return self.value.startswith('TVOS_')

def devices_not_allowed(self) -> bool:
return not self.devices_allowed()
return not self.devices_required()

def devices_allowed(self) -> bool:
warning = (
'Deprecation warning! Method '
'"devices_allowed" is deprecated in favor of "devices_required" in version 0.31.3 '
'and is subject for removal in future releases.'
)
log.get_logger(self.__class__).warning(Colors.YELLOW(warning))
return self.devices_required()

def devices_required(self) -> bool:
return self.is_development_type or self.is_ad_hoc_type


Expand Down
2 changes: 1 addition & 1 deletion src/codemagic/tools/app_store_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def create_profile(
devices=[],
omit_keys=['devices'],
)
if profile_type.devices_allowed():
if profile_type.devices_required():
create_params['devices'] = device_resource_ids
profile = self._create_resource(self.api_client.profiles, should_print, **create_params)

Expand Down
25 changes: 25 additions & 0 deletions tests/apple/app_store_connect/provisioning/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ def test_create_profile_success_with_devices(profile_type, profile_response, app
app_store_api_client.session.post.assert_called_once()


@pytest.mark.parametrize('profile_type, device_type', [
(ProfileType.IOS_APP_DEVELOPMENT, 'iOS'),
(ProfileType.IOS_APP_ADHOC, 'iOS'),
(ProfileType.MAC_APP_DEVELOPMENT, 'macOS'),
(ProfileType.MAC_CATALYST_APP_DEVELOPMENT, 'macOS'),
(ProfileType.TVOS_APP_DEVELOPMENT, 'tvOS'),
(ProfileType.TVOS_APP_ADHOC, 'tvOS'),
])
def test_create_profile_failure_without_devices(profile_type, device_type, app_store_api_client):
with pytest.raises(ValueError) as error_info:
app_store_api_client.profiles.create(
name='test profile',
profile_type=profile_type,
bundle_id=ResourceId('bundle_id_resource_id'),
certificates=[ResourceId('certificate_resource_id')],
devices=None,
)
expected_error_msg = (
f'Cannot create profile: the request does not include any {device_type} testing devices '
f'while they are required for creating a {profile_type} profile. If the profile creation is automatic, '
'ensure that at least one suitable testing device is registered on the Apple Developer Portal.'
)
assert str(error_info.value) == expected_error_msg


@pytest.mark.skipif(not os.environ.get('RUN_LIVE_API_TESTS'), reason='Live App Store Connect API access')
class ProfilesTest(ResourceManagerTestsBase):

Expand Down
6 changes: 3 additions & 3 deletions tests/apple/resources/enums/test_profile_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from codemagic.apple.resources import ProfileType


@pytest.mark.parametrize('profile_type, should_be_allowed', [
@pytest.mark.parametrize('profile_type, should_be_required', [
(ProfileType.IOS_APP_ADHOC, True),
(ProfileType.IOS_APP_DEVELOPMENT, True),
(ProfileType.IOS_APP_INHOUSE, False),
Expand All @@ -19,5 +19,5 @@
(ProfileType.TVOS_APP_INHOUSE, False),
(ProfileType.TVOS_APP_STORE, False),
])
def test_devices_allowed(profile_type, should_be_allowed):
assert profile_type.devices_allowed() is should_be_allowed
def test_devices_required(profile_type, should_be_required):
assert profile_type.devices_required() is should_be_required

0 comments on commit 58a7836

Please sign in to comment.