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(emoji): Use colorful version of warning emoji #28888

Merged
merged 8 commits into from
May 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,7 @@ export function massageMarkdown(input: string): string {
.replace(regEx(/]: https:\/\/github\.com\//g), ']: https://togithub.com/')
.replace('> ℹ **Note**\n> \n', '> [!NOTE]\n')
viceice marked this conversation as resolved.
Show resolved Hide resolved
.replace('> ⚠ **Warning**\n> \n', '> [!WARNING]\n')
.replace('> ⚠️ **Warning**\n> \n', '> [!WARNING]\n')
.replace('> ❗ **Important**\n> \n', '> [!IMPORTANT]\n');
return smartTruncate(massagedInput, GitHubMaxPrBodyLen);
}
Expand Down
33 changes: 24 additions & 9 deletions lib/util/emoji.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe('util/emoji', () => {
expect(emojify(':foo: :bar: :bee:')).toBe(':foo: :bar: 🐝');
});

it('convert warning shortcode to emoji', () => {
rarkins marked this conversation as resolved.
Show resolved Hide resolved
const warning = emojify(':warning:');
expect(warning).toBe('⚠️');
});

it('does not encode when config option is disabled', () => {
setEmojiConfig({ unicodeEmoji: false });
expect(emojify('Let it :bee:')).toBe('Let it :bee:');
Expand Down Expand Up @@ -54,20 +59,30 @@ describe('util/emoji', () => {
expect(unemojify(unsupported)).toBe('�');
});
});
});

describe('problem characters', () => {
it.each(['🚀', '💎', '🧹', '📦'])('converts %s forth and back', (char) => {
it('converts warning emoji to shortcode', () => {
setEmojiConfig({ unicodeEmoji: false });
const codified = unemojify(char);
expect(codified).not.toEqual(char);

setEmojiConfig({ unicodeEmoji: true });
const emojified = emojify(codified);
expect(emojified).toEqual(char);
const emoji = '⚠️';
const result = unemojify(emoji);
expect(result).toBe(':warning:');
});
});

describe('problematic characters', () => {
it.each(['🚀', '💎', '🧹', '📦', '⚠️'])(
'converts %s forth and back',
(char) => {
setEmojiConfig({ unicodeEmoji: false });
const codified = unemojify(char);
expect(codified).not.toEqual(char);

setEmojiConfig({ unicodeEmoji: true });
const emojified = emojify(codified);
expect(emojified).toEqual(char);
},
);
});

describe('stripEmojis', () => {
const makeEmoji = (hexCode: string): string =>
fromCodepointToUnicode(fromHexcodeToCodepoint(hexCode));
Expand Down
55 changes: 38 additions & 17 deletions lib/util/emoji.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import is from '@sindresorhus/is';
import mathiasBynensEmojiRegex from 'emoji-regex';
import {
fromCodepointToUnicode,
Expand All @@ -12,6 +11,7 @@ import type { RenovateConfig } from '../config/types';
import dataFiles from '../data-files.generated';
import { logger } from '../logger';
import { regEx } from './regex';
import { Result } from './result';
import { Json } from './schema-utils';

let unicodeEmoji = true;
Expand All @@ -21,27 +21,48 @@ const shortCodesByHex = new Map<string, string>();
const hexCodesByShort = new Map<string, string>();

const EmojiShortcodesSchema = Json.pipe(
z.record(z.string(), z.union([z.string(), z.array(z.string())])),
z.record(
z.string(),
z.union([z.string().transform((val) => [val]), z.array(z.string())]),
),
);
type EmojiShortcodeMapping = z.infer<typeof EmojiShortcodesSchema>;

const patchedEmojis: EmojiShortcodeMapping = {
'26A0-FE0F': ['warning'], // Colorful warning (⚠️) instead of black and white (⚠)
};

function initMapping(mapping: EmojiShortcodeMapping): void {
for (const [hex, shortcodes] of Object.entries(mapping)) {
const mainShortcode = `:${shortcodes[0]}:`;

shortCodesByHex.set(hex, mainShortcode);
shortCodesByHex.set(stripHexCode(hex), mainShortcode);

for (const shortcode of shortcodes) {
hexCodesByShort.set(`:${shortcode}:`, hex);
}
}
}

function lazyInitMappings(): void {
if (!mappingsInitialized) {
const result = EmojiShortcodesSchema.safeParse(
dataFiles.get('node_modules/emojibase-data/en/shortcodes/github.json')!,
const githubShortcodes = dataFiles.get(
'node_modules/emojibase-data/en/shortcodes/github.json',
);
// istanbul ignore if: not easily testable
if (!result.success) {
logger.warn({ error: result.error }, 'Unable to parse emoji shortcodes');
return;
}
for (const [hex, val] of Object.entries(result.data)) {
const shortCodes = is.array(val) ? val : [val];
shortCodesByHex.set(hex, `:${shortCodes[0]}:`);
shortCodes.forEach((shortCode) => {
hexCodesByShort.set(`:${shortCode}:`, hex);
});
}
mappingsInitialized = true;

Result.parse(githubShortcodes, EmojiShortcodesSchema)
.onValue((data) => {
initMapping(data);
initMapping(patchedEmojis);
mappingsInitialized = true;
})
.onError(
/* istanbul ignore next */
(error) => {
logger.warn({ error }, 'Unable to parse emoji shortcodes');
},
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/workers/repository/errors-warnings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('workers/repository/errors-warnings', () => {
"
---

> ⚠ **Warning**
> ⚠ **Warning**
>
> Some dependencies could not be looked up. Check the Dependency Dashboard for more information.

Expand Down Expand Up @@ -133,7 +133,7 @@ describe('workers/repository/errors-warnings', () => {
"
---

> ⚠ **Warning**
> ⚠ **Warning**
>
> Some dependencies could not be looked up. Check the warning logs for more information.

Expand Down Expand Up @@ -197,7 +197,7 @@ describe('workers/repository/errors-warnings', () => {
"
---

> ⚠ **Warning**
> ⚠ **Warning**
>
> Renovate failed to look up the following dependencies: \`dependency-1\`, \`dependency-2\`.
>
Expand Down Expand Up @@ -305,7 +305,7 @@ describe('workers/repository/errors-warnings', () => {
"
---
>
> ⚠ **Warning**
> ⚠ **Warning**
>
> Please correct - or verify that you can safely ignore - these dependency lookup failures before you merge this PR.
>
Expand Down