Debian Codegraph @

ansible_collections/community/general/plugins/modules/github_webhook_info.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2018, Ansible Project
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: github_webhook_info
11 short_description: Query information about GitHub webhooks
12 description:
13 - Query information about GitHub webhooks.
14 requirements:
15 - "PyGithub >= 1.3.5"
16 extends_documentation_fragment:
17 - community.general._attributes
18 - community.general._attributes.info_module
19 options:
20 repository:
21 description:
22 - Full name of the repository to configure a hook for.
23 type: str
24 required: true
25 aliases:
26 - repo
27 user:
28 description:
29 - User to authenticate to GitHub as.
30 type: str
31 required: true
32 password:
33 description:
34 - Password to authenticate to GitHub with.
35 type: str
36 token:
37 description:
38 - Token to authenticate to GitHub with.
39 type: str
40 github_url:
41 description:
42 - Base URL of the GitHub API.
43 type: str
44 default: https://api.github.com
45
46 author:
47 - "Chris St. Pierre (@stpierre)"
48 """
49
50 EXAMPLES = r"""
51 - name: List hooks for a repository (password auth)
52 community.general.github_webhook_info:
53 repository: ansible/ansible
54 user: "{{ github_user }}"
55 password: "{{ github_password }}"
56 register: ansible_webhooks
57
58 - name: List hooks for a repository on GitHub Enterprise (token auth)
59 community.general.github_webhook_info:
60 repository: myorg/myrepo
61 user: "{{ github_user }}"
62 token: "{{ github_user_api_token }}"
63 github_url: https://github.example.com/api/v3/
64 register: myrepo_webhooks
65 """
66
67 RETURN = r"""
68 hooks:
69 description: A list of hooks that exist for the repo.
70 returned: always
71 type: list
72 elements: dict
73 sample:
74 - has_shared_secret: true
75 url: https://jenkins.example.com/ghprbhook/
76 events: [issue_comment, pull_request]
77 insecure_ssl: "1"
78 content_type: json
79 active: true
80 id: 6206
81 last_response:
82 status: active
83 message: OK
84 code: 200
85 """
86
87 import traceback
88
89 GITHUB_IMP_ERR = None
90 try:
91 import github
92
93 HAS_GITHUB = True
94 except ImportError:
95 GITHUB_IMP_ERR = traceback.format_exc()
96 HAS_GITHUB = False
97
98 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
99
100
101 def _munge_hook(hook_obj):
102 retval = {
103 "active": hook_obj.active,
104 "events": hook_obj.events,
105 "id": hook_obj.id,
106 "url": hook_obj.url,
107 }
108 retval.update(hook_obj.config)
109 retval["has_shared_secret"] = "secret" in retval
110 if "secret" in retval:
111 del retval["secret"]
112
113 retval["last_response"] = hook_obj.last_response.raw_data
114 return retval
115
116
117 def main():
118 module = AnsibleModule(
119 argument_spec=dict(
120 repository=dict(type="str", required=True, aliases=["repo"]),
121 user=dict(type="str", required=True),
122 password=dict(type="str", no_log=True),
123 token=dict(type="str", no_log=True),
124 github_url=dict(type="str", default="https://api.github.com"),
125 ),
126 mutually_exclusive=(("password", "token"),),
127 required_one_of=(("password", "token"),),
128 supports_check_mode=True,
129 )
130
131 if not HAS_GITHUB:
132 module.fail_json(msg=missing_required_lib("PyGithub"), exception=GITHUB_IMP_ERR)
133
134 try:
135 github_conn = github.Github(
136 module.params["user"],
137 module.params.get("password") or module.params.get("token"),
138 base_url=module.params["github_url"],
139 )
140 except github.GithubException as err:
141 module.fail_json(msg=f"Could not connect to GitHub at {module.params['github_url']}: {err}")
142
143 try:
144 repo = github_conn.get_repo(module.params["repository"])
145 except github.BadCredentialsException as err:
146 module.fail_json(msg=f"Could not authenticate to GitHub at {module.params['github_url']}: {err}")
147 except github.UnknownObjectException as err:
148 module.fail_json(
149 msg=f"Could not find repository {module.params['repository']} in GitHub at {module.params['github_url']}: {err}"
150 )
151 except Exception as err:
152 module.fail_json(
153 msg=f"Could not fetch repository {module.params['repository']} from GitHub at {module.params['github_url']}: {err}",
154 exception=traceback.format_exc(),
155 )
156
157 try:
158 hooks = [_munge_hook(h) for h in repo.get_hooks()]
159 except github.GithubException as err:
160 module.fail_json(
161 msg=f"Unable to get hooks from repository {module.params['repository']}: {err}",
162 exception=traceback.format_exc(),
163 )
164
165 module.exit_json(changed=False, hooks=hooks)
166
167
168 if __name__ == "__main__":
169 main()