Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017-18, Abhijeet Kasurde <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: github_issue
12 short_description: View GitHub issue
13 description:
14 - View GitHub issue for a given repository and organization.
15 extends_documentation_fragment:
16 - community.general._attributes
17 attributes:
18 check_mode:
19 support: full
20 diff_mode:
21 support: none
22 options:
23 repo:
24 description:
25 - Name of repository from which issue needs to be retrieved.
26 required: true
27 type: str
28 organization:
29 description:
30 - Name of the GitHub organization in which the repository is hosted.
31 required: true
32 type: str
33 issue:
34 description:
35 - Issue number for which information is required.
36 required: true
37 type: int
38 action:
39 description:
40 - Get various details about issue depending upon action specified.
41 default: 'get_status'
42 choices:
43 - get_status
44 type: str
45 author:
46 - Abhijeet Kasurde (@Akasurde)
47 """
48
49 RETURN = r"""
50 issue_status:
51 description: State of the GitHub issue.
52 type: str
53 returned: success
54 sample: open, closed
55 """
56
57 EXAMPLES = r"""
58 - name: Check if GitHub issue is closed or not
59 community.general.github_issue:
60 organization: ansible
61 repo: ansible
62 issue: 23642
63 action: get_status
64 register: r
65
66 - name: Take action depending upon issue status
67 ansible.builtin.debug:
68 msg: Do something when issue 23642 is open
69 when: r.issue_status == 'open'
70 """
71
72 import json
73
74 from ansible.module_utils.basic import AnsibleModule
75 from ansible.module_utils.urls import fetch_url
76
77
78 def main():
79 module = AnsibleModule(
80 argument_spec=dict(
81 organization=dict(required=True),
82 repo=dict(required=True),
83 issue=dict(type="int", required=True),
84 action=dict(choices=["get_status"], default="get_status"),
85 ),
86 supports_check_mode=True,
87 )
88
89 organization = module.params["organization"]
90 repo = module.params["repo"]
91 issue = module.params["issue"]
92 action = module.params["action"]
93
94 result = dict()
95
96 headers = {
97 "Content-Type": "application/json",
98 "Accept": "application/vnd.github.v3+json",
99 }
100
101 url = f"https://api.github.com/repos/{organization}/{repo}/issues/{issue}"
102
103 response, info = fetch_url(module, url, headers=headers)
104 if not (200 <= info["status"] < 400):
105 if info["status"] == 404:
106 module.fail_json(msg=f"Failed to find issue {issue}")
107 module.fail_json(msg=f"Failed to send request to {url}: {info['msg']}")
108
109 gh_obj = json.loads(response.read())
110
111 if action == "get_status" or action is None:
112 if module.check_mode:
113 result.update(changed=True)
114 else:
115 result.update(changed=True, issue_status=gh_obj["state"])
116
117 module.exit_json(**result)
118
119
120 if __name__ == "__main__":
121 main()