We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Can't create github repo
### Summary

When I try to create a github repo with community general 2.4.0 github_repo module, I got an assertion error related to port.

### Issue Type

Bug Report

### Component Name

github_repo

### Ansible Version

ansible 2.10.7
community.general 2.4.0
PyGithub 1.54.1

### Configuration

_No response_

### OS / Environment

MacOS Mojave 10.14.6

### Steps to Reproduce

This is the task I've used
<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
- name: Create a Github repository
      github_repo:
        access_token: "{{ lookup('env', 'GITHUB_ACESS_TOKEN') }}"
        organization: xxx
        name: xxx
        description: "xxx"
        private: yes
```


### Expected Results

it should create a new Github Repo without error

### Actual Results

```console (paste below)
The full traceback is:
  File "/var/folders/kx/5h602b9s6fq3r5h7lxpxy8sr0000gn/T/ansible_github_repo_payload_8ng_7p6h/ansible_github_repo_payload.zip/ansible_collections/community/general/plugins/modules/github_repo.py", line 233, in main
  File "/var/folders/kx/5h602b9s6fq3r5h7lxpxy8sr0000gn/T/ansible_github_repo_payload_8ng_7p6h/ansible_github_repo_payload.zip/ansible_collections/community/general/plugins/modules/github_repo.py", line 197, in run_module
  File "/var/folders/kx/5h602b9s6fq3r5h7lxpxy8sr0000gn/T/ansible_github_repo_payload_8ng_7p6h/ansible_github_repo_payload.zip/ansible_collections/community/general/plugins/modules/github_repo.py", line 144, in create_repo
  File "/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Organization.py", line 578, in create_repo
    headers, data = self._requester.requestJsonAndCheck(
  File "/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py", line 316, in requestJsonAndCheck
    *self.requestJson(
  File "/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py", line 408, in requestJson
    return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
  File "/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py", line 475, in __requestEncode
    url = self.__makeAbsoluteUrl(url)
  File "/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py", line 557, in __makeAbsoluteUrl
    assert o.port == self.__port
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "access_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "description": "xxx",
            "name": "xxx",
            "organization": "xxx",
            "password": null,
            "private": true,
            "state": "present",
            "username": null
        }
    },
    "msg": "Unexpected error. AssertionError()"
}
```


### Code of Conduct

I agree to follow the Ansible Code of Conduct

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `plugins/modules/source_control/github/github_repo.py`
Content:
```
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 # Copyright: (c) 2021, Álvaro Torres Cogollo
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9 
10 DOCUMENTATION = '''
11 ---
12 module: github_repo
13 short_description: Manage your repositories on Github
14 version_added: 2.2.0
15 description:
16 - Manages Github repositories using PyGithub library.
17 - Authentication can be done with I(access_token) or with I(username) and I(password).
18 options:
19   username:
20     description:
21     - Username used for authentication.
22     - This is only needed when not using I(access_token).
23     type: str
24     required: false
25   password:
26     description:
27     - Password used for authentication.
28     - This is only needed when not using I(access_token).
29     type: str
30     required: false
31   access_token:
32     description:
33     - Token parameter for authentication.
34     - This is only needed when not using I(username) and I(password).
35     type: str
36     required: false
37   name:
38     description:
39     - Repository name.
40     type: str
41     required: true
42   description:
43     description:
44     - Description for the repository.
45     - This is only used when I(state) is C(present).
46     type: str
47     default: ''
48     required: false
49   private:
50     description:
51     - Whether the new repository should be private or not.
52     - This is only used when I(state) is C(present).
53     type: bool
54     default: no
55     required: false
56   state:
57     description:
58     - Whether the repository should exist or not.
59     type: str
60     default: present
61     choices: [ absent, present ]
62     required: false
63   organization:
64     description:
65     - Organization for the repository.
66     - When I(state) is C(present), the repository will be created in the current user profile.
67     type: str
68     required: false
69 requirements:
70 - PyGithub>=1.54
71 notes:
72 - For Python 3, PyGithub>=1.54 should be used.
73 - "For Python 3.5, PyGithub==1.54 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-54-november-30-2020)."
74 - "For Python 2.7, PyGithub==1.45 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-45-december-29-2019)."
75 - Supports C(check_mode).
76 author:
77 - Álvaro Torres Cogollo (@atorrescogollo)
78 '''
79 
80 EXAMPLES = '''
81 - name: Create a Github repository
82   community.general.github_repo:
83     access_token: mytoken
84     organization: MyOrganization
85     name: myrepo
86     description: "Just for fun"
87     private: yes
88     state: present
89   register: result
90 
91 - name: Delete the repository
92   community.general.github_repo:
93     username: octocat
94     password: password
95     organization: MyOrganization
96     name: myrepo
97     state: absent
98   register: result
99 '''
100 
101 RETURN = '''
102 repo:
103   description: Repository information as JSON. See U(https://docs.github.com/en/rest/reference/repos#get-a-repository).
104   returned: success and I(state) is C(present)
105   type: dict
106 '''
107 
108 import traceback
109 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
110 import sys
111 
112 GITHUB_IMP_ERR = None
113 try:
114     from github import Github, GithubException
115     from github.GithubException import UnknownObjectException
116     HAS_GITHUB_PACKAGE = True
117 except Exception:
118     GITHUB_IMP_ERR = traceback.format_exc()
119     HAS_GITHUB_PACKAGE = False
120 
121 
122 def authenticate(username=None, password=None, access_token=None):
123     if access_token:
124         return Github(base_url="https://api.github.com:443", login_or_token=access_token)
125     else:
126         return Github(base_url="https://api.github.com:443", login_or_token=username, password=password)
127 
128 
129 def create_repo(gh, name, organization=None, private=False, description='', check_mode=False):
130     result = dict(
131         changed=False,
132         repo=dict())
133     if organization:
134         target = gh.get_organization(organization)
135     else:
136         target = gh.get_user()
137 
138     repo = None
139     try:
140         repo = target.get_repo(name=name)
141         result['repo'] = repo.raw_data
142     except UnknownObjectException:
143         if not check_mode:
144             repo = target.create_repo(
145                 name=name, private=private, description=description)
146             result['repo'] = repo.raw_data
147 
148         result['changed'] = True
149 
150     changes = {}
151     if repo is None or repo.raw_data['private'] != private:
152         changes['private'] = private
153     if repo is None or repo.raw_data['description'] != description:
154         changes['description'] = description
155 
156     if changes:
157         if not check_mode:
158             repo.edit(**changes)
159 
160         result['repo'].update({
161             'private': repo._private.value if not check_mode else private,
162             'description': repo._description.value if not check_mode else description,
163         })
164         result['changed'] = True
165 
166     return result
167 
168 
169 def delete_repo(gh, name, organization=None, check_mode=False):
170     result = dict(changed=False)
171     if organization:
172         target = gh.get_organization(organization)
173     else:
174         target = gh.get_user()
175     try:
176         repo = target.get_repo(name=name)
177         if not check_mode:
178             repo.delete()
179         result['changed'] = True
180     except UnknownObjectException:
181         pass
182 
183     return result
184 
185 
186 def run_module(params, check_mode=False):
187     gh = authenticate(
188         username=params['username'], password=params['password'], access_token=params['access_token'])
189     if params['state'] == "absent":
190         return delete_repo(
191             gh=gh,
192             name=params['name'],
193             organization=params['organization'],
194             check_mode=check_mode
195         )
196     else:
197         return create_repo(
198             gh=gh,
199             name=params['name'],
200             organization=params['organization'],
201             private=params['private'],
202             description=params['description'],
203             check_mode=check_mode
204         )
205 
206 
207 def main():
208     module_args = dict(
209         username=dict(type='str', required=False, default=None),
210         password=dict(type='str', required=False, default=None, no_log=True),
211         access_token=dict(type='str', required=False,
212                           default=None, no_log=True),
213         name=dict(type='str', required=True),
214         state=dict(type='str', required=False, default="present",
215                    choices=["present", "absent"]),
216         organization=dict(type='str', required=False, default=None),
217         private=dict(type='bool', required=False, default=False),
218         description=dict(type='str', required=False, default=''),
219     )
220     module = AnsibleModule(
221         argument_spec=module_args,
222         supports_check_mode=True,
223         required_together=[('username', 'password')],
224         required_one_of=[('username', 'access_token')],
225         mutually_exclusive=[('username', 'access_token')]
226     )
227 
228     if not HAS_GITHUB_PACKAGE:
229         module.fail_json(msg=missing_required_lib(
230             "PyGithub"), exception=GITHUB_IMP_ERR)
231 
232     try:
233         result = run_module(module.params, module.check_mode)
234         module.exit_json(**result)
235     except GithubException as e:
236         module.fail_json(msg="Github error. {0}".format(repr(e)))
237     except Exception as e:
238         module.fail_json(msg="Unexpected error. {0}".format(repr(e)))
239 
240 
241 if __name__ == '__main__':
242     main()
243
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/plugins/modules/source_control/github/github_repo.py b/plugins/modules/source_control/github/github_repo.py
--- a/plugins/modules/source_control/github/github_repo.py
+++ b/plugins/modules/source_control/github/github_repo.py
@@ -121,9 +121,9 @@
 
 def authenticate(username=None, password=None, access_token=None):
     if access_token:
-        return Github(base_url="https://api.github.com:443", login_or_token=access_token)
+        return Github(base_url="https://api.github.com", login_or_token=access_token)
     else:
-        return Github(base_url="https://api.github.com:443", login_or_token=username, password=password)
+        return Github(base_url="https://api.github.com", login_or_token=username, password=password)
 
 
 def create_repo(gh, name, organization=None, private=False, description='', check_mode=False):

{"golden_diff": "diff --git a/plugins/modules/source_control/github/github_repo.py b/plugins/modules/source_control/github/github_repo.py\n--- a/plugins/modules/source_control/github/github_repo.py\n+++ b/plugins/modules/source_control/github/github_repo.py\n@@ -121,9 +121,9 @@\n \n def authenticate(username=None, password=None, access_token=None):\n     if access_token:\n-        return Github(base_url=\"https://api.github.com:443\", login_or_token=access_token)\n+        return Github(base_url=\"https://api.github.com\", login_or_token=access_token)\n     else:\n-        return Github(base_url=\"https://api.github.com:443\", login_or_token=username, password=password)\n+        return Github(base_url=\"https://api.github.com\", login_or_token=username, password=password)\n \n \n def create_repo(gh, name, organization=None, private=False, description='', check_mode=False):\n", "issue": "Can't create github repo\n### Summary\r\n\r\nWhen I try to create a github repo with community general 2.4.0 github_repo module, I got an assertion error related to port.\r\n\r\n### Issue Type\r\n\r\nBug Report\r\n\r\n### Component Name\r\n\r\ngithub_repo\r\n\r\n### Ansible Version\r\n\r\nansible 2.10.7\r\ncommunity.general 2.4.0\r\nPyGithub 1.54.1\r\n\r\n### Configuration\r\n\r\n_No response_\r\n\r\n### OS / Environment\r\n\r\nMacOS Mojave 10.14.6\r\n\r\n### Steps to Reproduce\r\n\r\nThis is the task I've used\r\n<!--- Paste example playbooks or commands between quotes below -->\r\n```yaml (paste below)\r\n- name: Create a Github repository\r\n      github_repo:\r\n        access_token: \"{{ lookup('env', 'GITHUB_ACESS_TOKEN') }}\"\r\n        organization: xxx\r\n        name: xxx\r\n        description: \"xxx\"\r\n        private: yes\r\n```\r\n\r\n\r\n### Expected Results\r\n\r\nit should create a new Github Repo without error\r\n\r\n### Actual Results\r\n\r\n```console (paste below)\r\nThe full traceback is:\r\n  File \"/var/folders/kx/5h602b9s6fq3r5h7lxpxy8sr0000gn/T/ansible_github_repo_payload_8ng_7p6h/ansible_github_repo_payload.zip/ansible_collections/community/general/plugins/modules/github_repo.py\", line 233, in main\r\n  File \"/var/folders/kx/5h602b9s6fq3r5h7lxpxy8sr0000gn/T/ansible_github_repo_payload_8ng_7p6h/ansible_github_repo_payload.zip/ansible_collections/community/general/plugins/modules/github_repo.py\", line 197, in run_module\r\n  File \"/var/folders/kx/5h602b9s6fq3r5h7lxpxy8sr0000gn/T/ansible_github_repo_payload_8ng_7p6h/ansible_github_repo_payload.zip/ansible_collections/community/general/plugins/modules/github_repo.py\", line 144, in create_repo\r\n  File \"/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Organization.py\", line 578, in create_repo\r\n    headers, data = self._requester.requestJsonAndCheck(\r\n  File \"/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py\", line 316, in requestJsonAndCheck\r\n    *self.requestJson(\r\n  File \"/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py\", line 408, in requestJson\r\n    return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)\r\n  File \"/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py\", line 475, in __requestEncode\r\n    url = self.__makeAbsoluteUrl(url)\r\n  File \"/Users/kakarukeys/.local/share/virtualenvs/infra_setup-AVEq_0GX/lib/python3.9/site-packages/github/Requester.py\", line 557, in __makeAbsoluteUrl\r\n    assert o.port == self.__port\r\nfatal: [localhost]: FAILED! => {\r\n    \"changed\": false,\r\n    \"invocation\": {\r\n        \"module_args\": {\r\n            \"access_token\": \"VALUE_SPECIFIED_IN_NO_LOG_PARAMETER\",\r\n            \"description\": \"xxx\",\r\n            \"name\": \"xxx\",\r\n            \"organization\": \"xxx\",\r\n            \"password\": null,\r\n            \"private\": true,\r\n            \"state\": \"present\",\r\n            \"username\": null\r\n        }\r\n    },\r\n    \"msg\": \"Unexpected error. AssertionError()\"\r\n}\r\n```\r\n\r\n\r\n### Code of Conduct\r\n\r\nI agree to follow the Ansible Code of Conduct\n", "before_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n# Copyright: (c) 2021, \u00c1lvaro Torres Cogollo\n# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)\n\nfrom __future__ import absolute_import, division, print_function\n__metaclass__ = type\n\nDOCUMENTATION = '''\n---\nmodule: github_repo\nshort_description: Manage your repositories on Github\nversion_added: 2.2.0\ndescription:\n- Manages Github repositories using PyGithub library.\n- Authentication can be done with I(access_token) or with I(username) and I(password).\noptions:\n  username:\n    description:\n    - Username used for authentication.\n    - This is only needed when not using I(access_token).\n    type: str\n    required: false\n  password:\n    description:\n    - Password used for authentication.\n    - This is only needed when not using I(access_token).\n    type: str\n    required: false\n  access_token:\n    description:\n    - Token parameter for authentication.\n    - This is only needed when not using I(username) and I(password).\n    type: str\n    required: false\n  name:\n    description:\n    - Repository name.\n    type: str\n    required: true\n  description:\n    description:\n    - Description for the repository.\n    - This is only used when I(state) is C(present).\n    type: str\n    default: ''\n    required: false\n  private:\n    description:\n    - Whether the new repository should be private or not.\n    - This is only used when I(state) is C(present).\n    type: bool\n    default: no\n    required: false\n  state:\n    description:\n    - Whether the repository should exist or not.\n    type: str\n    default: present\n    choices: [ absent, present ]\n    required: false\n  organization:\n    description:\n    - Organization for the repository.\n    - When I(state) is C(present), the repository will be created in the current user profile.\n    type: str\n    required: false\nrequirements:\n- PyGithub>=1.54\nnotes:\n- For Python 3, PyGithub>=1.54 should be used.\n- \"For Python 3.5, PyGithub==1.54 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-54-november-30-2020).\"\n- \"For Python 2.7, PyGithub==1.45 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-45-december-29-2019).\"\n- Supports C(check_mode).\nauthor:\n- \u00c1lvaro Torres Cogollo (@atorrescogollo)\n'''\n\nEXAMPLES = '''\n- name: Create a Github repository\n  community.general.github_repo:\n    access_token: mytoken\n    organization: MyOrganization\n    name: myrepo\n    description: \"Just for fun\"\n    private: yes\n    state: present\n  register: result\n\n- name: Delete the repository\n  community.general.github_repo:\n    username: octocat\n    password: password\n    organization: MyOrganization\n    name: myrepo\n    state: absent\n  register: result\n'''\n\nRETURN = '''\nrepo:\n  description: Repository information as JSON. See U(https://docs.github.com/en/rest/reference/repos#get-a-repository).\n  returned: success and I(state) is C(present)\n  type: dict\n'''\n\nimport traceback\nfrom ansible.module_utils.basic import AnsibleModule, missing_required_lib\nimport sys\n\nGITHUB_IMP_ERR = None\ntry:\n    from github import Github, GithubException\n    from github.GithubException import UnknownObjectException\n    HAS_GITHUB_PACKAGE = True\nexcept Exception:\n    GITHUB_IMP_ERR = traceback.format_exc()\n    HAS_GITHUB_PACKAGE = False\n\n\ndef authenticate(username=None, password=None, access_token=None):\n    if access_token:\n        return Github(base_url=\"https://api.github.com:443\", login_or_token=access_token)\n    else:\n        return Github(base_url=\"https://api.github.com:443\", login_or_token=username, password=password)\n\n\ndef create_repo(gh, name, organization=None, private=False, description='', check_mode=False):\n    result = dict(\n        changed=False,\n        repo=dict())\n    if organization:\n        target = gh.get_organization(organization)\n    else:\n        target = gh.get_user()\n\n    repo = None\n    try:\n        repo = target.get_repo(name=name)\n        result['repo'] = repo.raw_data\n    except UnknownObjectException:\n        if not check_mode:\n            repo = target.create_repo(\n                name=name, private=private, description=description)\n            result['repo'] = repo.raw_data\n\n        result['changed'] = True\n\n    changes = {}\n    if repo is None or repo.raw_data['private'] != private:\n        changes['private'] = private\n    if repo is None or repo.raw_data['description'] != description:\n        changes['description'] = description\n\n    if changes:\n        if not check_mode:\n            repo.edit(**changes)\n\n        result['repo'].update({\n            'private': repo._private.value if not check_mode else private,\n            'description': repo._description.value if not check_mode else description,\n        })\n        result['changed'] = True\n\n    return result\n\n\ndef delete_repo(gh, name, organization=None, check_mode=False):\n    result = dict(changed=False)\n    if organization:\n        target = gh.get_organization(organization)\n    else:\n        target = gh.get_user()\n    try:\n        repo = target.get_repo(name=name)\n        if not check_mode:\n            repo.delete()\n        result['changed'] = True\n    except UnknownObjectException:\n        pass\n\n    return result\n\n\ndef run_module(params, check_mode=False):\n    gh = authenticate(\n        username=params['username'], password=params['password'], access_token=params['access_token'])\n    if params['state'] == \"absent\":\n        return delete_repo(\n            gh=gh,\n            name=params['name'],\n            organization=params['organization'],\n            check_mode=check_mode\n        )\n    else:\n        return create_repo(\n            gh=gh,\n            name=params['name'],\n            organization=params['organization'],\n            private=params['private'],\n            description=params['description'],\n            check_mode=check_mode\n        )\n\n\ndef main():\n    module_args = dict(\n        username=dict(type='str', required=False, default=None),\n        password=dict(type='str', required=False, default=None, no_log=True),\n        access_token=dict(type='str', required=False,\n                          default=None, no_log=True),\n        name=dict(type='str', required=True),\n        state=dict(type='str', required=False, default=\"present\",\n                   choices=[\"present\", \"absent\"]),\n        organization=dict(type='str', required=False, default=None),\n        private=dict(type='bool', required=False, default=False),\n        description=dict(type='str', required=False, default=''),\n    )\n    module = AnsibleModule(\n        argument_spec=module_args,\n        supports_check_mode=True,\n        required_together=[('username', 'password')],\n        required_one_of=[('username', 'access_token')],\n        mutually_exclusive=[('username', 'access_token')]\n    )\n\n    if not HAS_GITHUB_PACKAGE:\n        module.fail_json(msg=missing_required_lib(\n            \"PyGithub\"), exception=GITHUB_IMP_ERR)\n\n    try:\n        result = run_module(module.params, module.check_mode)\n        module.exit_json(**result)\n    except GithubException as e:\n        module.fail_json(msg=\"Github error. {0}\".format(repr(e)))\n    except Exception as e:\n        module.fail_json(msg=\"Unexpected error. {0}\".format(repr(e)))\n\n\nif __name__ == '__main__':\n    main()\n", "path": "plugins/modules/source_control/github/github_repo.py"}], "after_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\n# Copyright: (c) 2021, \u00c1lvaro Torres Cogollo\n# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)\n\nfrom __future__ import absolute_import, division, print_function\n__metaclass__ = type\n\nDOCUMENTATION = '''\n---\nmodule: github_repo\nshort_description: Manage your repositories on Github\nversion_added: 2.2.0\ndescription:\n- Manages Github repositories using PyGithub library.\n- Authentication can be done with I(access_token) or with I(username) and I(password).\noptions:\n  username:\n    description:\n    - Username used for authentication.\n    - This is only needed when not using I(access_token).\n    type: str\n    required: false\n  password:\n    description:\n    - Password used for authentication.\n    - This is only needed when not using I(access_token).\n    type: str\n    required: false\n  access_token:\n    description:\n    - Token parameter for authentication.\n    - This is only needed when not using I(username) and I(password).\n    type: str\n    required: false\n  name:\n    description:\n    - Repository name.\n    type: str\n    required: true\n  description:\n    description:\n    - Description for the repository.\n    - This is only used when I(state) is C(present).\n    type: str\n    default: ''\n    required: false\n  private:\n    description:\n    - Whether the new repository should be private or not.\n    - This is only used when I(state) is C(present).\n    type: bool\n    default: no\n    required: false\n  state:\n    description:\n    - Whether the repository should exist or not.\n    type: str\n    default: present\n    choices: [ absent, present ]\n    required: false\n  organization:\n    description:\n    - Organization for the repository.\n    - When I(state) is C(present), the repository will be created in the current user profile.\n    type: str\n    required: false\nrequirements:\n- PyGithub>=1.54\nnotes:\n- For Python 3, PyGithub>=1.54 should be used.\n- \"For Python 3.5, PyGithub==1.54 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-54-november-30-2020).\"\n- \"For Python 2.7, PyGithub==1.45 should be used. More information: U(https://pygithub.readthedocs.io/en/latest/changes.html#version-1-45-december-29-2019).\"\n- Supports C(check_mode).\nauthor:\n- \u00c1lvaro Torres Cogollo (@atorrescogollo)\n'''\n\nEXAMPLES = '''\n- name: Create a Github repository\n  community.general.github_repo:\n    access_token: mytoken\n    organization: MyOrganization\n    name: myrepo\n    description: \"Just for fun\"\n    private: yes\n    state: present\n  register: result\n\n- name: Delete the repository\n  community.general.github_repo:\n    username: octocat\n    password: password\n    organization: MyOrganization\n    name: myrepo\n    state: absent\n  register: result\n'''\n\nRETURN = '''\nrepo:\n  description: Repository information as JSON. See U(https://docs.github.com/en/rest/reference/repos#get-a-repository).\n  returned: success and I(state) is C(present)\n  type: dict\n'''\n\nimport traceback\nfrom ansible.module_utils.basic import AnsibleModule, missing_required_lib\nimport sys\n\nGITHUB_IMP_ERR = None\ntry:\n    from github import Github, GithubException\n    from github.GithubException import UnknownObjectException\n    HAS_GITHUB_PACKAGE = True\nexcept Exception:\n    GITHUB_IMP_ERR = traceback.format_exc()\n    HAS_GITHUB_PACKAGE = False\n\n\ndef authenticate(username=None, password=None, access_token=None):\n    if access_token:\n        return Github(base_url=\"https://api.github.com\", login_or_token=access_token)\n    else:\n        return Github(base_url=\"https://api.github.com\", login_or_token=username, password=password)\n\n\ndef create_repo(gh, name, organization=None, private=False, description='', check_mode=False):\n    result = dict(\n        changed=False,\n        repo=dict())\n    if organization:\n        target = gh.get_organization(organization)\n    else:\n        target = gh.get_user()\n\n    repo = None\n    try:\n        repo = target.get_repo(name=name)\n        result['repo'] = repo.raw_data\n    except UnknownObjectException:\n        if not check_mode:\n            repo = target.create_repo(\n                name=name, private=private, description=description)\n            result['repo'] = repo.raw_data\n\n        result['changed'] = True\n\n    changes = {}\n    if repo is None or repo.raw_data['private'] != private:\n        changes['private'] = private\n    if repo is None or repo.raw_data['description'] != description:\n        changes['description'] = description\n\n    if changes:\n        if not check_mode:\n            repo.edit(**changes)\n\n        result['repo'].update({\n            'private': repo._private.value if not check_mode else private,\n            'description': repo._description.value if not check_mode else description,\n        })\n        result['changed'] = True\n\n    return result\n\n\ndef delete_repo(gh, name, organization=None, check_mode=False):\n    result = dict(changed=False)\n    if organization:\n        target = gh.get_organization(organization)\n    else:\n        target = gh.get_user()\n    try:\n        repo = target.get_repo(name=name)\n        if not check_mode:\n            repo.delete()\n        result['changed'] = True\n    except UnknownObjectException:\n        pass\n\n    return result\n\n\ndef run_module(params, check_mode=False):\n    gh = authenticate(\n        username=params['username'], password=params['password'], access_token=params['access_token'])\n    if params['state'] == \"absent\":\n        return delete_repo(\n            gh=gh,\n            name=params['name'],\n            organization=params['organization'],\n            check_mode=check_mode\n        )\n    else:\n        return create_repo(\n            gh=gh,\n            name=params['name'],\n            organization=params['organization'],\n            private=params['private'],\n            description=params['description'],\n            check_mode=check_mode\n        )\n\n\ndef main():\n    module_args = dict(\n        username=dict(type='str', required=False, default=None),\n        password=dict(type='str', required=False, default=None, no_log=True),\n        access_token=dict(type='str', required=False,\n                          default=None, no_log=True),\n        name=dict(type='str', required=True),\n        state=dict(type='str', required=False, default=\"present\",\n                   choices=[\"present\", \"absent\"]),\n        organization=dict(type='str', required=False, default=None),\n        private=dict(type='bool', required=False, default=False),\n        description=dict(type='str', required=False, default=''),\n    )\n    module = AnsibleModule(\n        argument_spec=module_args,\n        supports_check_mode=True,\n        required_together=[('username', 'password')],\n        required_one_of=[('username', 'access_token')],\n        mutually_exclusive=[('username', 'access_token')]\n    )\n\n    if not HAS_GITHUB_PACKAGE:\n        module.fail_json(msg=missing_required_lib(\n            \"PyGithub\"), exception=GITHUB_IMP_ERR)\n\n    try:\n        result = run_module(module.params, module.check_mode)\n        module.exit_json(**result)\n    except GithubException as e:\n        module.fail_json(msg=\"Github error. {0}\".format(repr(e)))\n    except Exception as e:\n        module.fail_json(msg=\"Unexpected error. {0}\".format(repr(e)))\n\n\nif __name__ == '__main__':\n    main()\n", "path": "plugins/modules/source_control/github/github_repo.py"}]}