Repository: ansible-collections/community.general

Issue:
mas - remove signed-in check from Mac OS 12.0+
## Context
Unfortunately, as of Mac 12.0 and onwards, the `mas account` command isn't functioning correctly.
Issues have been already raised regarding this:

https://github.com/mas-cli/mas/issues/417

## Problem
The `mas account` is used as a check to see if the user is signed in before installing or upgrading an app using mas via Ansible.

As of Mac 12.0 and onwards, we cannot install or upgrade an app via mas via Ansible

## Solution
Detailed in the `Additional Information` section

### Issue Type

Feature Idea

### Component Name

mas

### Additional Information

## Solution
In the `check_signin` function,

https://github.com/ansible-collections/community.general/blob/f833982d6f686063288b9be2c3721aac8d35d521/plugins/modules/mas.py#L157 

 We should do the following:

1. Get the version of Mac OS running on your computer
```python
import platform
mac_version = platform.mac_ver()
```

2. Check if the version is greater than 12.0
```python
def is_version_greater(version, reference_version):
    version_parts = [int(part) for part in version.split('.')]
    reference_parts = [int(part) for part in reference_version.split('.')]
    
    if version_parts > reference_parts:
        return True
    else:
        return False
```

3. If it is greater, skip the logic defined in `check_signin` function and also print a user-friendly message to sign in via the Mac App Store GUI. Unfortunately, for the time being, this is the workaround mentioned in the issues.
```python
def check_signin(self):
    ''' Verifies that the user is signed in to the Mac App Store '''
    
    # Only check this once per execution
    if self._checked_signin:
        return
    
    if self.is_version_greater(self._mac_version, WORKING_MAC_VERSION_MAS_ACCOUNT):
        print('WARNING: You must be signed in via the Mac App Store GUI else error will occur')
    else:
        rc, out, err = self.run(['account'])
        if out.split("\n", 1)[0].rstrip() == 'Not signed in':
            self.module.fail_json(msg='You must be signed in to the Mac App Store')

    self._checked_signin = True
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/mas.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/mas.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @lukasbestle @mheap
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/mas.py b/plugins/modules/mas.py
--- a/plugins/modules/mas.py
+++ b/plugins/modules/mas.py
@@ -53,6 +53,8 @@
     - macOS 10.11+
     - "mas-cli (U(https://github.com/mas-cli/mas)) 1.5.0+ available as C(mas) in the bin path"
     - The Apple ID to use already needs to be signed in to the Mac App Store (check with C(mas account)).
+    - The feature of "checking if user is signed in" is disabled for anyone using macOS 12.0+.
+    - Users need to sign in via the Mac App Store GUI beforehand for anyone using macOS 12.0+ due to U(https://github.com/mas-cli/mas/issues/417).
 '''
 
 EXAMPLES = '''
@@ -106,6 +108,9 @@
 
 from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
 
+import platform
+NOT_WORKING_MAC_VERSION_MAS_ACCOUNT = '12.0'
+
 
 class Mas(object):
 
@@ -115,6 +120,7 @@ def __init__(self, module):
         # Initialize data properties
         self.mas_path = self.module.get_bin_path('mas')
         self._checked_signin = False
+        self._mac_version = platform.mac_ver()[0] or '0.0'
         self._installed = None  # Populated only if needed
         self._outdated = None   # Populated only if needed
         self.count_install = 0
@@ -156,14 +162,16 @@ def check_mas_tool(self):
 
     def check_signin(self):
         ''' Verifies that the user is signed in to the Mac App Store '''
-
         # Only check this once per execution
         if self._checked_signin:
             return
-
-        rc, out, err = self.run(['account'])
-        if out.split("\n", 1)[0].rstrip() == 'Not signed in':
-            self.module.fail_json(msg='You must be signed in to the Mac App Store')
+        if LooseVersion(self._mac_version) >= LooseVersion(NOT_WORKING_MAC_VERSION_MAS_ACCOUNT):
+            # Checking if user is signed-in is disabled due to https://github.com/mas-cli/mas/issues/417
+            self.module.log('WARNING: You must be signed in via the Mac App Store GUI beforehand else error will occur')
+        else:
+            rc, out, err = self.run(['account'])
+            if out.split("\n", 1)[0].rstrip() == 'Not signed in':
+                self.module.fail_json(msg='You must be signed in to the Mac App Store')
 
         self._checked_signin = True
 


Test patch: