Skip to content

Commit c771729

Browse files
Use updates_url for plugin version if it exists
1 parent 2e39d0b commit c771729

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

plugins/modules/jenkins_plugin.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
- A list of URL segment(s) to retrieve the update center JSON file from.
9494
default: ['update-center.json', 'updates/update-center.json']
9595
version_added: 3.3.0
96+
plugin_versions_url_segment:
97+
type: list
98+
elements: str
99+
description:
100+
- A list of URL segment(s) to retrieve the plugin versions JSON file from.
101+
default: ['plugin-versions.json', 'current/plugin-versions.json']
102+
version_added: 11.1.0
96103
latest_plugins_url_segments:
97104
type: list
98105
elements: str
@@ -688,6 +695,10 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
688695
self.jenkins_version = self.parse_version(raw_version)
689696
name = plugin_name or self.params['name']
690697
cache_path = "{}/ansible_jenkins_plugin_cache.json".format(self.params['jenkins_home'])
698+
plugin_version_urls = []
699+
for base_url in self.params['updates_url']:
700+
for update_json in self.params['plugin_versions_url_segment']:
701+
plugin_version_urls.append("{}/{}".format(base_url, update_json))
691702

692703
try: # Check if file is saved localy
693704
if os.path.exists(cache_path):
@@ -697,10 +708,19 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
697708

698709
now = time.time()
699710
if now - file_mtime >= 86400:
700-
response, info = fetch_url(self.module, "https://updates.jenkins.io/current/plugin-versions.json")
701-
if info['status'] != 200:
702-
self.module.fail_json(msg="Failed to fetch plugin-versions.json", details=info)
703-
plugin_data = json.loads(to_native(response.read()), object_pairs_hook=OrderedDict)
711+
try:
712+
response = self._get_urls_data(plugin_version_urls, what="plugin-versions.json")
713+
plugin_data = json.loads(to_native(response.read()), object_pairs_hook=OrderedDict)
714+
if "plugins" not in plugin_data:
715+
raise ValueError("Missing 'plugins' key")
716+
except Exception as e:
717+
self.module.warn(
718+
"Could not fetch plugin-versions.json from custom updates_url(s). Falling back to updates.jenkins.io. Details: {}".format(to_native(e))
719+
)
720+
response, info = fetch_url(self.module, "https://updates.jenkins.io/current/plugin-versions.json")
721+
if info['status'] != 200:
722+
self.module.fail_json(msg="Failed to fetch fallback plugin-versions.json", details=info)
723+
plugin_data = json.loads(to_native(response.read()), object_pairs_hook=OrderedDict)
704724

705725
# Save it to file for next time
706726
with open(cache_path, "w") as f:
@@ -710,6 +730,8 @@ def _get_latest_compatible_plugin_version(self, plugin_name=None):
710730
plugin_data = json.load(f)
711731

712732
except Exception as e:
733+
if os.path.exists(cache_path):
734+
os.remove(cache_path)
713735
self.module.fail_json(msg="Failed to parse plugin-versions.json", details=to_native(e))
714736

715737
plugin_versions = plugin_data.get("plugins", {}).get(name)
@@ -939,6 +961,8 @@ def main():
939961
updates_url_password=dict(type="str", no_log=True),
940962
update_json_url_segment=dict(type="list", elements="str", default=['update-center.json',
941963
'updates/update-center.json']),
964+
plugin_versions_url_segment=dict(type="list", elements="str", default=['plugin-versions.json',
965+
'current/plugin-versions.json']),
942966
latest_plugins_url_segments=dict(type="list", elements="str", default=['latest']),
943967
versioned_plugins_url_segments=dict(type="list", elements="str", default=['download/plugins', 'plugins']),
944968
url=dict(default='http://localhost:8080'),
@@ -968,7 +992,7 @@ def main():
968992
module.params['state'] = 'present'
969993
module.params['version'] = jp._get_latest_compatible_plugin_version()
970994

971-
# Ser version to latest compatible version if version is latest
995+
# Set version to latest compatible version if version is latest
972996
if module.params['version'] == 'latest':
973997
module.params['version'] = jp._get_latest_compatible_plugin_version()
974998

tests/unit/plugins/modules/test_jenkins_plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ def test__get_latest_compatible_plugin_version(fetch_mock, mocker):
209209
"name": "git",
210210
"version": "latest",
211211
"updates_url": ["https://some.base.url"],
212+
"plugin_versions_url_segment": ["plugin-versions.json"],
212213
"latest_plugins_url_segments": ["test_latest"],
213214
"jenkins_home": "/var/lib/jenkins",
214215
}

0 commit comments

Comments
 (0)