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

[Cases] Bulk edit tags: Prevent error when updating cases with the same tags #145608

Merged
merged 5 commits into from
Nov 21, 2022

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Nov 17, 2022

Summary

This PR fixes a bug where an error occurs when updating the case with the same tags. To fix it, we filter cases without any changes.

Fixes: #145217

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@cnasikas cnasikas added bug Fixes for quality problems that affect the customer experience release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.6.0 v8.7.0 labels Nov 17, 2022
@cnasikas cnasikas requested a review from a team as a code owner November 17, 2022 18:37
@cnasikas cnasikas self-assigned this Nov 17, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

@@ -37,16 +38,26 @@ export const useTagsAction = ({ onAction, onActionSuccess, isDisabled }: UseActi
(tagsSelection: TagsSelectionState) => {
onAction();
onFlyoutClosed();
const casesToUpdate = selectedCasesToEditTags.map((theCase) => {

const casesToUpdate = selectedCasesToEditTags.reduce((acc, theCase) => {
Copy link
Member Author

@cnasikas cnasikas Nov 17, 2022

Choose a reason for hiding this comment

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

I changed it to reduce to map and filter at the same time

const tags = difference(theCase.tags, tagsSelection.unSelectedTags);
const uniqueTags = new Set([...tags, ...tagsSelection.selectedTags]);
const uniqueTagsAsArray = Array.from(uniqueTags.values());

if (isEqual(theCase.tags, uniqueTagsAsArray)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to update the case if the original tags are the same as the new ones

Copy link
Contributor

Choose a reason for hiding this comment

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

How would sorting work for these arrays? If the arrays contain the same strings but are in a different order isEqual will return false. Maybe that's ok though, I'd have to check the backend to see if it would still throw an error in that situation. I suppose ideally we'd just skip the update since reordering the tags doesn't really do much in the UI.

Copy link
Member Author

@cnasikas cnasikas Nov 18, 2022

Choose a reason for hiding this comment

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

Thanks, I was not aware of that. I fixed it here a200227. As we have already a set of uniqueTags I iterate through the theCase.tags and check if it is a member of the uniqueTags set. If all of the elements are part of the set then the tags are identical and there is no need to do an update.

const tags = difference(theCase.tags, tagsSelection.unSelectedTags);
const uniqueTags = new Set([...tags, ...tagsSelection.selectedTags]);
const uniqueTagsAsArray = Array.from(uniqueTags.values());

if (isEqual(theCase.tags, uniqueTagsAsArray)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How would sorting work for these arrays? If the arrays contain the same strings but are in a different order isEqual will return false. Maybe that's ok though, I'd have to check the backend to see if it would still throw an error in that situation. I suppose ideally we'd just skip the update since reordering the tags doesn't really do much in the UI.

const uniqueTagsAsArray = Array.from(uniqueTags.values());

if (isEqual(theCase.tags, uniqueTagsAsArray)) {
onActionSuccess();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we want to call the success callback here? Would that trigger a refresh of the cases?

https://github.com/elastic/kibana/blob/main/x-pack/plugins/cases/public/components/all_cases/use_actions.tsx#L61

const tagsAction = useTagsAction({
    isDisabled: false,
    onAction: closePopover,
    onActionSuccess: refreshCases,
  });

Copy link
Member Author

Choose a reason for hiding this comment

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

My mistake! I forgot to remove it.

version: theCase.version,
},
];
}, [] as CaseUpdateRequest[]);
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed with @cnasikas , I still see the error toast as an empty array casesToUpdate is getting sent to the backend.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in a200227

Copy link
Contributor

Choose a reason for hiding this comment

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

Confirmed fixed for the bulk edit of tags.

Note this same type of issue still occurs with the View case page - would be good to fix this in a follow-up PR:

edit_case_tags_bug

Copy link
Contributor

Choose a reason for hiding this comment

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

Created #145736 to fix this in the View case page.

Copy link
Contributor

@peteharverson peteharverson left a comment

Choose a reason for hiding this comment

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

Tested latest edits and LGTM

};
});
const casesToUpdate = selectedCasesToEditTags.reduce((acc, theCase) => {
const tagsWithoutUnselectedTags = difference(theCase.tags, tagsSelection.unSelectedTags);
Copy link
Contributor

@adcoelho adcoelho Nov 18, 2022

Choose a reason for hiding this comment

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

If you make this one a set too you can just use lodash's .isEqual() instead of the aux function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@cnasikas cnasikas enabled auto-merge (squash) November 21, 2022 14:54
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cases 347.3KB 347.4KB +78.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
cases 126.8KB 127.0KB +162.0B
Unknown metric groups

ESLint disabled in files

id before after diff
osquery 1 2 +1

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
fleet 59 65 +6
osquery 109 115 +6
securitySolution 442 448 +6
total +20

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
fleet 67 73 +6
osquery 110 117 +7
securitySolution 519 525 +6
total +21

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @cnasikas

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Nov 21, 2022
…me tags (elastic#145608)

## Summary

This PR fixes a bug where an error occurs when updating the case with
the same tags. To fix it, we filter cases without any changes.

Fixes: elastic#145217

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

(cherry picked from commit b1fb286)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.6

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Nov 21, 2022
…the same tags (#145608) (#145886)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Cases] Bulk edit tags: Prevent error when updating cases with the
same tags (#145608)](#145608)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Christos
Nasikas","email":"christos.nasikas@elastic.co"},"sourceCommit":{"committedDate":"2022-11-21T15:51:00Z","message":"[Cases]
Bulk edit tags: Prevent error when updating cases with the same tags
(#145608)\n\n## Summary\r\n\r\nThis PR fixes a bug where an error occurs
when updating the case with\r\nthe same tags. To fix it, we filter cases
without any changes.\r\n\r\nFixes:
https://github.com/elastic/kibana/issues/145217\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"b1fb286d84158f75b95d4a9f875df6d07d6c1214","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:ResponseOps","Feature:Cases","v8.6.0","v8.7.0"],"number":145608,"url":"https://github.com/elastic/kibana/pull/145608","mergeCommit":{"message":"[Cases]
Bulk edit tags: Prevent error when updating cases with the same tags
(#145608)\n\n## Summary\r\n\r\nThis PR fixes a bug where an error occurs
when updating the case with\r\nthe same tags. To fix it, we filter cases
without any changes.\r\n\r\nFixes:
https://github.com/elastic/kibana/issues/145217\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"b1fb286d84158f75b95d4a9f875df6d07d6c1214"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/145608","number":145608,"mergeCommit":{"message":"[Cases]
Bulk edit tags: Prevent error when updating cases with the same tags
(#145608)\n\n## Summary\r\n\r\nThis PR fixes a bug where an error occurs
when updating the case with\r\nthe same tags. To fix it, we filter cases
without any changes.\r\n\r\nFixes:
https://github.com/elastic/kibana/issues/145217\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"b1fb286d84158f75b95d4a9f875df6d07d6c1214"}}]}]
BACKPORT-->

Co-authored-by: Christos Nasikas <christos.nasikas@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Feature:Cases Cases feature release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.6.0 v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Security Solution]Error prompt on editing tag under cases
7 participants