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

OCPBUGS-28856: make getGroupVersionKindForResource null safe #13574

Merged
merged 1 commit into from Feb 6, 2024

Conversation

rhamilto
Copy link
Member

@rhamilto rhamilto commented Feb 5, 2024

Demo:

getGroupVersionKindForResource is now null safe and will return undefined if apiVersion or kind are undefined. useDeleteModal will not work with an undefined resource, but the run time error no longer occurs.

Screen.Recording.2024-02-05.at.11.05.35.AM.mov

@openshift-ci-robot openshift-ci-robot added jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Feb 5, 2024
@openshift-ci-robot
Copy link
Contributor

@rhamilto: This pull request references Jira Issue OCPBUGS-28856, which is invalid:

  • expected the bug to target the "4.16.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

In response to this:

Demo:

getGroupVersionKindForResource is now null safe and will return undefined if apiVersion or kind are undefined. useDeleteModal will not work with an undefined resource, but the run time error no longer occurs.

Screen.Recording.2024-02-05.at.11.05.35.AM.mov

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@rhamilto
Copy link
Member Author

rhamilto commented Feb 5, 2024

/jira refresh

@openshift-ci-robot openshift-ci-robot added jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. and removed jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Feb 5, 2024
@openshift-ci-robot
Copy link
Contributor

@rhamilto: This pull request references Jira Issue OCPBUGS-28856, which is valid. The bug has been moved to the POST state.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.16.0) matches configured target version for branch (4.16.0)
  • bug is in the state ASSIGNED, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @yapei

In response to this:

/jira refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci bot added component/sdk Related to console-plugin-sdk approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Feb 5, 2024
Copy link
Member

@TheRealJon TheRealJon left a comment

Choose a reason for hiding this comment

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

/lgtm

No changes needed, just a couple of observations.

const apiVersion = resource?.apiVersion;
const kind = resource?.kind;
const apiVersionSplit = apiVersion?.split('/');
const apiVersionSplitLen = apiVersionSplit?.length;
if (apiVersionSplitLen > 2) throw new Error('Provided resource has invalid apiVersion.');
Copy link
Member

Choose a reason for hiding this comment

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

Not new, but is this check necessary? Wouldn't the API server reject resources with an invalid apiVersion?

@@ -48,18 +48,21 @@ export const getAPIVersionForModel: GetAPIVersionForModel = (model) =>
* If the resource has an invalid apiVersion then it'll throw Error.
Copy link
Member

Choose a reason for hiding this comment

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

Line 47 says that this function returns group 'core' if the resource doesn't have an API group. As far as I can tell, that's not true.

Comment on lines 50 to 66
export const getGroupVersionKindForResource: GetGroupVersionKindForResource = (resource) => {
const { apiVersion, kind } = resource;
const apiVersionSplit = apiVersion.split('/');
const apiVersionSplitLen = apiVersionSplit.length;
const apiVersion = resource?.apiVersion;
const kind = resource?.kind;
const apiVersionSplit = apiVersion?.split('/');
const apiVersionSplitLen = apiVersionSplit?.length;
if (apiVersionSplitLen > 2) throw new Error('Provided resource has invalid apiVersion.');

return {
...(apiVersionSplitLen === 2 && {
group: apiVersionSplit[0],
}),
version: apiVersionSplitLen === 2 ? apiVersionSplit[1] : apiVersion,
kind,
};
return !apiVersion || !kind
? undefined
: {
...(apiVersionSplitLen === 2 && {
group: apiVersionSplit[0],
}),
version: apiVersionSplitLen === 2 ? apiVersionSplit[1] : resource?.apiVersion,
kind,
};
};
Copy link
Member

Choose a reason for hiding this comment

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

We could simplify this with a few tweaks:

export const getGroupVersionKindForResource: GetGroupVersionKindForResource = (resource) => {
  const { apiVersion, kind } = resource ?? {};
  if (!kind || !apiVersion) return undefined;
  const [group, version, invalid] = apiVersion.split('/');
  if (invalid) throw new Error('Provided apiVersion is invalid.'); // remove?
  return version ? { group, version, kind } : { version: group, kind };
};

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Feb 5, 2024
Copy link
Contributor

openshift-ci bot commented Feb 5, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: rhamilto, TheRealJon

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Contributor

openshift-ci bot commented Feb 6, 2024

@rhamilto: all tests passed!

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@openshift-merge-bot openshift-merge-bot bot merged commit 13f3942 into openshift:master Feb 6, 2024
6 checks passed
@openshift-ci-robot
Copy link
Contributor

@rhamilto: Jira Issue OCPBUGS-28856: All pull requests linked via external trackers have merged:

Jira Issue OCPBUGS-28856 has been moved to the MODIFIED state.

In response to this:

Demo:

getGroupVersionKindForResource is now null safe and will return undefined if apiVersion or kind are undefined. useDeleteModal will not work with an undefined resource, but the run time error no longer occurs.

Screen.Recording.2024-02-05.at.11.05.35.AM.mov

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-bot
Copy link
Contributor

[ART PR BUILD NOTIFIER]

This PR has been included in build openshift-enterprise-console-container-v4.16.0-202402060810.p0.g13f3942.assembly.stream for distgit openshift-enterprise-console.
All builds following this will include this PR.

@rhamilto rhamilto deleted the OCPBUGS-28856 branch February 6, 2024 13:13
@rhamilto
Copy link
Member Author

rhamilto commented Feb 6, 2024

/cherry-pick release-4.15

@openshift-cherrypick-robot

@rhamilto: new pull request created: #13582

In response to this:

/cherry-pick release-4.15

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-merge-robot
Copy link
Contributor

Fix included in accepted release 4.16.0-0.nightly-2024-02-07-073830

@yanpzhan
Copy link
Contributor

@rhamilto Hi, I'd like to reproduce/verify the bug, if you have example codes for the modal test like that in your demo?

@rhamilto
Copy link
Member Author

@rhamilto Hi, I'd like to reproduce/verify the bug, if you have example codes for the modal test like that in your demo?

To test as shown in the video above, I added the useDeleteButton Example to the Pod details page.

yapei added a commit to yapei/console that referenced this pull request Mar 7, 2024
yapei added a commit to yapei/console that referenced this pull request Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. component/sdk Related to console-plugin-sdk jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants