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: [M3-6430] - Event entities should only be linked for true labels #9003

Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Fixed:
- Typesafety of the `<Select />` component #8986
- Clear the Kubernetes Delete Dialog when it is re-opened #9000
- Event entities should only be linked for true labels

### Tech Stories:
- MUIv5 Migration - Components > TagsInput, TagsPanel #8995
Expand Down
39 changes: 39 additions & 0 deletions packages/manager/src/eventMessageGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getEventMessage, {
eventMessageCreators,
safeSecondaryEntityLabel,
} from './eventMessageGenerator';
import { applyLinking } from './eventMessageGenerator';

beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {});
Expand Down Expand Up @@ -87,4 +88,42 @@ describe('Event message generation', () => {
).not.toMatch('booted with');
});
});

describe('apply linking to labels', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for adding test coverage!

const entity = entityFactory.build({ id: 10, label: 'foo' });

it('should return empty string if message is falsy', () => {
const mockEvent = eventFactory.build({ action: 'domain_record_create' });
const message = null;
const result = applyLinking(mockEvent, message as any); // casting since message is a required prop

expect(result).toEqual('');
});

it('should replace entity label with link if entity and link exist', async () => {
const mockEvent = eventFactory.build({ entity });
const message = 'created entity foo';
const result = applyLinking(mockEvent, message);

expect(result).toEqual(`created entity <a href="/linodes/10">foo</a> `);
});

it('should replace secondary entity label with link if entity and link exist', () => {
const mockEvent = eventFactory.build({ entity });
const message = 'created secondary_entity for foo';
const result = applyLinking(mockEvent, message);

expect(result).toEqual(
`created secondary_entity for <a href="/linodes/10">foo</a> `
);
});

it('should not replace entity label if label is inside backticks', () => {
const mockEvent = eventFactory.build({ entity });
const message = 'created `foo`';
const result = applyLinking(mockEvent, message);

expect(result).toEqual('created `foo`');
});
});
});
11 changes: 8 additions & 3 deletions packages/manager/src/eventMessageGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ function applyBolding(event: Event, message: string) {
return newMessage;
}

function applyLinking(event: Event, message: string) {
export function applyLinking(event: Event, message: string) {
if (!message) {
return '';
}
Expand All @@ -857,14 +857,19 @@ function applyLinking(event: Event, message: string) {
event.secondary_entity,
false
);

let newMessage = message;

if (event.entity && entityLinkTarget) {
const label = event.entity.label;
const nonTickedLabels = new RegExp(`(?<!\`)${label}`, 'g');

newMessage = newMessage.replace(
event.entity.label,
`<a href="${entityLinkTarget}">${event.entity.label}</a>`
nonTickedLabels,
`<a href="${entityLinkTarget}">${label}</a> `
);
}

if (event.secondary_entity && secondaryEntityLinkTarget) {
newMessage = newMessage.replace(
event.secondary_entity.label,
Expand Down