[PR #11878/1b0b8d5c backport][stable-12] gitlab_project_variable - use find_project() for graceful error handling (#11882)

gitlab_project_variable - use find_project() for graceful error handling (#11878)

* gitlab_project_variable - use find_project() for consistent error handling

Replace the bare projects.get() call in GitlabProjectVariables.get_project()
with find_project() from module_utils/gitlab, which all other GitLab modules
already use. This ensures a graceful fail_json (with a clear error message)
when the project is not found, rather than an unhandled GitlabGetError
propagating as a module traceback.



* Add changelog fragment for PR 11878



* minor adjustment in f-string

---------


(cherry picked from commit 1b0b8d5cc1)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
patchback[bot]
2026-04-19 12:49:16 +02:00
committed by GitHub
parent 973b5be063
commit 09201bf49e
2 changed files with 11 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
bugfixes:
- gitlab_project_variable - use ``find_project()`` from module utils for project lookup, consistent
with all other GitLab modules in the collection
(https://github.com/ansible-collections/community.general/issues/3157,
https://github.com/ansible-collections/community.general/pull/11878).

View File

@@ -240,6 +240,7 @@ from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec,
filter_returned_variables,
find_project,
gitlab_authentication,
list_all_kwargs,
vars_to_variables,
@@ -249,11 +250,14 @@ from ansible_collections.community.general.plugins.module_utils.gitlab import (
class GitlabProjectVariables:
def __init__(self, module, gitlab_instance):
self.repo = gitlab_instance
self.project = self.get_project(module.params["project"])
self._module = module
self.project = self.get_project(module.params["project"])
def get_project(self, project_name):
return self.repo.projects.get(project_name)
project = find_project(self.repo, project_name)
if project is None:
self._module.fail_json(msg=f"Project {project_name} not found or insufficient permissions.")
return project
def list_all_project_variables(self):
return list(self.project.variables.list(**list_all_kwargs))