Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a logical flaw when deleting a build in the jenkins_build module #5514

Merged
merged 12 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- jenkins_build - fix the logical flaw when deleting a Jenkins build (https://github.com/ansible-collections/community.general/pull/5514).
10 changes: 8 additions & 2 deletions plugins/modules/jenkins_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ def get_build_status(self):
try:
response = self.server.get_build_info(self.name, self.build_number)
return response

except jenkins.JenkinsException as e:
response = {}
response["result"] = "ABSENT"
return response
except Exception as e:
self.module.fail_json(msg='Unable to fetch build information, %s' % to_native(e),
exception=traceback.format_exc())
Expand Down Expand Up @@ -231,7 +234,10 @@ def get_result(self):
if self.state == "stopped" and build_status['result'] == "ABORTED":
result['changed'] = True
result['build_info'] = build_status
elif build_status['result'] == "SUCCESS":
elif self.state == "absent" and build_status['result'] == "ABSENT":
result['changed'] = True
result['build_info'] = build_status
elif self.state != "absent" and build_status['result'] == "SUCCESS":
result['changed'] = True
result['build_info'] = build_status
else:
Expand Down
23 changes: 21 additions & 2 deletions tests/unit/plugins/modules/test_jenkins_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ansible_collections.community.general.plugins.modules import jenkins_build

import json
import jenkins


def set_module_args(args):
Expand Down Expand Up @@ -51,6 +52,8 @@ def get_job_info(self, name):
}

def get_build_info(self, name, build_number):
if name == "host-delete":
raise jenkins.JenkinsException("job {0} number {1} does not exist".format(name, build_number))
return {
"building": True,
"result": "SUCCESS"
Expand Down Expand Up @@ -83,7 +86,7 @@ def build_job(self, *args):
return None

def delete_build(self, name, build_number):
return None
raise jenkins.NotFoundException("job {0} number {1} does not exist".format(name, build_number))

def stop_build(self, name, build_number):
return None
Expand Down Expand Up @@ -173,7 +176,23 @@ def test_module_delete_build(self, jenkins_connection, test_deps):

with self.assertRaises(AnsibleExitJson):
set_module_args({
"name": "host-check",
"name": "host-delete",
"build_number": "1234",
"state": "absent",
"user": "abc",
"token": "xyz"
})
jenkins_build.main()

@patch('ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies')
@patch('ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection')
def test_module_delete_build_again(self, jenkins_connection, test_deps):
test_deps.return_value = None
jenkins_connection.return_value = JenkinsMockIdempotent()

with self.assertRaises(AnsibleFailJson):
set_module_args({
"name": "host-delete",
"build_number": "1234",
"state": "absent",
"user": "abc",
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ dataclasses ; python_version == '3.6'
elastic-apm ; python_version >= '3.6'

# requirements for scaleway modules
passlib[argon2]
passlib[argon2]

# requirements for jenkins modules
python-jenkins >= 0.4.12