Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2022, Guillaume MARTINEZ ([email protected])
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: gitlab_project_badge
11 short_description: Manage project badges on GitLab Server
12 version_added: 6.1.0
13 description:
14 - This module allows to add and remove badges to/from a project.
15 author: Guillaume MARTINEZ (@Lunik)
16 requirements:
17 - C(owner) or C(maintainer) rights to project on the GitLab server
18 extends_documentation_fragment:
19 - community.general._auth_basic
20 - community.general._gitlab
21 - community.general._attributes
22
23 attributes:
24 check_mode:
25 support: full
26 diff_mode:
27 support: none
28
29 options:
30 project:
31 description:
32 - The name (or full path) of the GitLab project the badge is added to/removed from.
33 required: true
34 type: str
35
36 state:
37 description:
38 - State of the badge in the project.
39 - On V(present), it adds a badge to a GitLab project.
40 - On V(absent), it removes a badge from a GitLab project.
41 choices: ['present', 'absent']
42 default: 'present'
43 type: str
44
45 link_url:
46 description:
47 - The URL associated with the badge.
48 required: true
49 type: str
50
51 image_url:
52 description:
53 - The image URL of the badge.
54 - A badge is identified by this URL.
55 required: true
56 type: str
57 """
58
59 EXAMPLES = r"""
60 - name: Add a badge to a GitLab Project
61 community.general.gitlab_project_badge:
62 api_url: 'https://example.gitlab.com'
63 api_token: 'Your-Private-Token'
64 project: projectname
65 state: present
66 link_url: 'https://example.gitlab.com/%{project_path}'
67 image_url: 'https://example.gitlab.com/%{project_path}/badges/%{default_branch}/pipeline.svg'
68
69 - name: Remove a badge from a GitLab Project
70 community.general.gitlab_project_badge:
71 api_url: 'https://example.gitlab.com'
72 api_token: 'Your-Private-Token'
73 project: projectname
74 state: absent
75 link_url: 'https://example.gitlab.com/%{project_path}'
76 image_url: 'https://example.gitlab.com/%{project_path}/badges/%{default_branch}/pipeline.svg'
77 """
78
79 RETURN = r"""
80 badge:
81 description: The badge information.
82 returned: when O(state=present)
83 type: dict
84 sample:
85 id: 1
86 link_url: 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}'
87 image_url: 'https://shields.io/my/badge'
88 rendered_link_url: 'http://example.com/ci_status.svg?project=example-org/example-project&ref=master'
89 rendered_image_url: 'https://shields.io/my/badge'
90 kind: project
91 """
92
93 from ansible.module_utils.api import basic_auth_argument_spec
94 from ansible.module_utils.basic import AnsibleModule
95
96 from ansible_collections.community.general.plugins.module_utils._gitlab import (
97 auth_argument_spec,
98 find_project,
99 gitlab_authentication,
100 list_all_kwargs,
101 )
102
103
104 def present_strategy(module, gl, project, wished_badge):
105 changed = False
106
107 existing_badge = None
108 for badge in project.badges.list(**list_all_kwargs):
109 if badge.image_url == wished_badge["image_url"]:
110 existing_badge = badge
111 break
112
113 if not existing_badge:
114 changed = True
115 if module.check_mode:
116 return changed, {"status": "A project badge would be created."}
117
118 badge = project.badges.create(wished_badge)
119 return changed, badge.attributes
120
121 if existing_badge.link_url != wished_badge["link_url"]:
122 changed = True
123 existing_badge.link_url = wished_badge["link_url"]
124
125 if changed:
126 if module.check_mode:
127 return changed, {"status": "Project badge attributes would be changed."}
128
129 existing_badge.save()
130
131 return changed, existing_badge.attributes
132
133
134 def absent_strategy(module, gl, project, wished_badge):
135 changed = False
136
137 existing_badge = None
138 for badge in project.badges.list(**list_all_kwargs):
139 if badge.image_url == wished_badge["image_url"]:
140 existing_badge = badge
141 break
142
143 if not existing_badge:
144 return changed, None
145
146 changed = True
147 if module.check_mode:
148 return changed, {"status": "Project badge would be destroyed."}
149
150 existing_badge.delete()
151
152 return changed, None
153
154
155 state_strategy = {"present": present_strategy, "absent": absent_strategy}
156
157
158 def core(module):
159 # check prerequisites and connect to gitlab server
160 gl = gitlab_authentication(module)
161
162 gitlab_project = module.params["project"]
163 state = module.params["state"]
164
165 project = find_project(gl, gitlab_project)
166 # project doesn't exist
167 if not project:
168 module.fail_json(msg=f"project '{gitlab_project}' not found.")
169
170 wished_badge = {
171 "link_url": module.params["link_url"],
172 "image_url": module.params["image_url"],
173 }
174
175 changed, summary = state_strategy[state](module=module, gl=gl, project=project, wished_badge=wished_badge)
176
177 module.exit_json(changed=changed, badge=summary)
178
179
180 def main():
181 argument_spec = basic_auth_argument_spec()
182 argument_spec.update(auth_argument_spec())
183 argument_spec.update(
184 dict(
185 project=dict(type="str", required=True),
186 state=dict(type="str", default="present", choices=["present", "absent"]),
187 link_url=dict(type="str", required=True),
188 image_url=dict(type="str", required=True),
189 )
190 )
191
192 module = AnsibleModule(
193 argument_spec=argument_spec,
194 mutually_exclusive=[
195 ["api_username", "api_token"],
196 ["api_username", "api_oauth_token"],
197 ["api_username", "api_job_token"],
198 ["api_token", "api_oauth_token"],
199 ["api_token", "api_job_token"],
200 ],
201 required_together=[
202 ["api_username", "api_password"],
203 ],
204 required_one_of=[
205 ["api_username", "api_token", "api_oauth_token", "api_job_token"],
206 ],
207 supports_check_mode=True,
208 )
209
210 core(module)
211
212
213 if __name__ == "__main__":
214 main()