diff --git a/source_control/gitea_organization.py b/source_control/gitea_organization.py index c7bb490..95a27c0 100644 --- a/source_control/gitea_organization.py +++ b/source_control/gitea_organization.py @@ -26,13 +26,36 @@ options: default: present required: no name: - description: - - The full name for your organization account / group - required: false - username: description: - The username for your organization account / group required: true + full_name: + description: + - The full name for your organization account / group + required: false + visibility: + description: + - Visibility of the organization, either private, public, or limited (visible to all logged in users) + choices: ['private', 'public', 'limited'] + default: private + required: no + description: + description: + - The organizations description + required: no + location: + description: + - The organizations location + required: no + website: + description: + - The organizations website + required: no + repo_admin_change_team_access + description + - Wether repository admins can add and remove access for teams + choices: ['yes', 'no'] + default: no auth_token: description: - Authentification token for your gitea account @@ -50,29 +73,28 @@ EXAMPLES = ''' # Create an organization - name: Create organization gitea_organization: - name: My Org - username: myorg + full_name: My Org + name: myorg auth_token: 1234...6789 gitea_url: https://git.example.com # Update organization - name: Change organization gitea_organization: - name: My better Org - username: myorg + full_name: My better Org + name: myorg auth_token: 1234...6789 gitea_url: https://git.example.com ''' RETURN = ''' -original_message: - description: The original name param that was passed in - type: str - returned: always -message: - description: The output message that the test module generates - type: str +return_code: + description: The HTTP return code from the Gitea API + type: int returned: always +gitea_respone: + description: The JSON output message that Gitea returns + type: dict ''' from ansible.module_utils.basic import AnsibleModule @@ -83,14 +105,14 @@ def run_module(): auth_token=dict(type='str', required=True), gitea_url=dict(type='str', required=True), - username=dict(type='str', required=True), - name=dict(type='bool', required=False, default=False) + name=dict(type='str', required=True), + full_name=dict(type='str', default="", required=False), + visibility=dict(type='str', default="private", choices=['private', 'public', 'limited']), - # TODO support "description": "string", - # TODO support "location": "string", - # TODO support "repo_admin_change_team_access": true, - # TODO support "visibility": "public", - # TODO support "website": "string" + repo_admin_change_team_access=dict(type="bool", required=False, default=False), + description=dict(type='str', default="", required=False), + location=dict(type='str', default="", required=False), + website=dict(type='str', default="", required=False), ) result = dict( @@ -109,7 +131,7 @@ def run_module(): } gitea_url = module.params['gitea_url'] gitea_token = module.params['auth_token'] - username = module.params['username'] + username = module.params['name'] req_org = requests.get( gitea_url + '/api/v1/orgs/' + username + '?access_token=' + gitea_token, @@ -119,26 +141,50 @@ def run_module(): if module.check_mode: module.exit_json(**result) + # Prepare org data + new_data = { + "username": username, + "full_name": module.params['full_name'], + "visibility": module.params['visibility'], + "repo_admin_change_team_access": module.params['repo_admin_change_team_access'], + "description": module.params['description'], + "location": module.params['location'], + "website": module.params['website'], + } + if module.params['state'] == 'present': + # org is requested and already exists if req_org.status_code == 200: - ##TODO support modify modifyable params result['return_code'] = 200 result['gitea_response'] = req_org.json() + old_data = req_org.json() + # check if org needs to be patched + if ( + new_data['full_name'] != old_data['full_name'] or + new_data['description'] != old_data['description'] or + new_data['location'] != old_data['location'] or + new_data['repo_admin_change_team_access'] != + old_data['repo_admin_change_team_access'] or + new_data['visibility'] != old_data['visibility'] or + new_data['website'] != old_data['website'] + ): + new_data.pop('username') + req_patch = requests.patch( + gitea_url + '/api/v1/orgs/' + username + '?access_token=' + gitea_token, + headers=headers, + data=json.dumps(new_data), + ) + result['return_code'] = req_patch.status_code + result['gitea_response'] = req_patch.json() + result['changed'] = True + # all is fine result['state'] = 'present' if req_org.status_code == 404: - data = { - #TODO "description": "string", - "full_name": module.params['name'], - #TODO "location": "string", - #TODO "repo_admin_change_team_access": true, - "username": username, - #TODO "visibility": "public", - #TODO "website": "string" - } + # org is requesed and does not yet exist, create create_req = requests.post( gitea_url + '/api/v1/orgs' + '?access_token=' + gitea_token, headers=headers, - data=json.dumps(data), + data=json.dumps(new_data), ) result['return_code'] = create_req.status_code result['gitea_response'] = create_req.json() @@ -149,10 +195,12 @@ def run_module(): if module.params['state'] == 'absent': if req_org.status_code == 404: + # org should be absent and does not exist result['return_code'] = 404 result['gitea_response'] = req_org.json() result['state'] = 'absent' if req_org.status_code == 200: + # org should be abenst and needs to be deleted delete_req = requests.delete( gitea_url + '/api/v1/orgs/' + username + '?access_token=' + gitea_token, @@ -160,6 +208,7 @@ def run_module(): ) result['return_code'] = delete_req.status_code if delete_req.status_code != 204: + # deletion failed result['gitea_response'] = create_req.json() module.fail_json(msg="Deletion failed", **result) result['changed'] = True @@ -171,4 +220,4 @@ def main(): run_module() if __name__ == '__main__': - main() \ No newline at end of file + main()