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

Fixes #38265 - Use jquery data for nested hostgroups #11335

Merged

Conversation

jeremylenz
Copy link
Member

@jeremylenz jeremylenz commented Mar 4, 2025

What are the changes introduced in this pull request?

In the host_and_hostgroup_edit form, there is a hidden select element created with the ID #hostgroup_organization_ids.

image

Its value is an array of organization IDs. When a user changes the content source using the dropdown, this hidden element is used to get organization IDs to query. This is done in order to populate the Lifecycle Environment and Content View fields.

Previously (I'm assuming before jQuery 3?) this was accessed via

$("#hostgroup_organization_ids").val()

Problem is, when creating a nested hostgroup, that jQuery call seems to return [] -- even though the vanilla JS call document.querySelector("#hostgroup_organization_ids") confirms the element is there, and has the required data ([1] in my case).

I fixed the issue by using .data('useds') instead of .val(). This fixed the nested hostgroup issue, but broke hostgroup edit.
So I altered the logic to only use .data('useds') in the specific case of nested hostgroups.

Considerations taken when implementing this change?

I am not sure why my solution works, or why it breaks the other flow, or if my solution is at all correct.

What are the testing steps for this pull request?

Steps to Reproduce:

  1. Set up a Foreman server with a Smart Proxy with Content and have multiple LCE

  2. Create a hostgroup HG1 with Foreman server as content source and choose any set of LCE and CV

  3. Now create a nested hostgroup HG2 from HG1
    and
    Change the content source to Smart Proxy
    Now try to select other cv or lce

Before patch:
It wont populate any and says "No matches found"

Expected behavior:
It should populate other cv and lce

@jeremylenz
Copy link
Member Author

@MariaAga 👀

orgIds = [$("#host_organization_id").val()];
};
orgIds = orgIds.map(id => Number(id));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot and myself came to the conclusion, that this is technically 100% the same but the "arrow" function is the modern one. I would stay with the modern one :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it back to ES5-style JavaScript because that's what the rest of the file is, and I got an eslint warning about it.

if(orgIds === undefined || orgIds === null || orgIds.length === 0) {
var orgIdsElem = $("#hostgroup_organization_ids");
var orgIds = orgIdsElem.val();
if (orgIds === undefined || orgIds === null || orgIds.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not tested, but copilot suggest this:

  const orgIdsElem = $("#hostgroup_organization_ids");
  let orgIds = orgIdsElem.val() ?? orgIdsElem.data('useds') ?? [$("#host_organization_id").val()];

The nullish coalescing operatos looks like exactly what is required here:
The nullish

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I would love to write it this way, but this also is not ES5-compatible, unfortunately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MariaAga Now that I think of this.. Does jQuery 3 mean we can start using modern JS in every place besides Angular pages?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure what prevented us before from using es5/6?

@sbernhard
Copy link
Member

Pretty funny that you are working on the same day/same hour on something similar than myself in #11334
Would be cool if you can have a look at this PR, too :)

@lfu
Copy link
Member

lfu commented Mar 7, 2025

Tested. It worked well.

Before

HG2

After

Hg22

Copy link
Member

@lfu lfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@jeremylenz jeremylenz merged commit bf83a7a into Katello:master Mar 12, 2025
29 of 31 checks passed
qcjames53 pushed a commit to qcjames53/katello that referenced this pull request Mar 14, 2025
qcjames53 pushed a commit that referenced this pull request Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants