Skip to content

Commit

Permalink
refactor(preview): split out the message or summary creation
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Feb 16, 2023
1 parent 66a5a16 commit feb07b6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 28 deletions.
46 changes: 36 additions & 10 deletions build/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23545,7 +23545,7 @@ __nccwpck_require__.r(__webpack_exports__);
// EXPORTS
__nccwpck_require__.d(__webpack_exports__, {
"MESSAGE_ID": () => (/* binding */ MESSAGE_ID),
"getMessage": () => (/* binding */ getMessage),
"createSummary": () => (/* binding */ createSummary),
"getVariables": () => (/* binding */ getVariables),
"previewAction": () => (/* binding */ previewAction),
"previewInput": () => (/* binding */ previewInput)
Expand Down Expand Up @@ -23885,7 +23885,7 @@ async function previewAction(input = previewInput()) {
}
const variables = getVariables(config, updates);
const messageId = template(input.commentId, variables);
const messageBody = getMessage(updates, variables);
const messageBody = createSummary(updates, variables);
if (!input.shouldComment) {
(0,core.info)(`Skipped comment: 'comment' is disabled`);
}
Expand Down Expand Up @@ -23972,29 +23972,55 @@ function getVariables(config, updates) {
* Generate the message body for a single update.
* Note, this is not configurable, but you can use the variables used to construct your own.
*/
function getMessage(updates, vars) {
/* eslint-disable prettier/prettier */
function createSummary(updates, vars) {
// If all updates are in the same group, we can unify QR codes
if (updates.every(update => update.group === updates[0].group)) {
return `🚀 Expo preview is ready!
return createSingleQrSummary(updates, vars);
}
return createMultipleQrSummary(updates, vars);
}
function createSingleQrSummary(updates, vars) {
const platformName = `Platform${updates.length === 1 ? '' : 's'}`;
const platformValue = updates.map(update => `**${update.platform}**`).join(', ');
return `🚀 Expo preview is ready!

- Project → **${vars.projectSlug}**
- Platform${updates.length === 1 ? '' : 's'} → ${updates.map(update => `**${update.platform}**`).join(', ')}
- ${platformName} → ${platformValue}
- Runtime Version → **${vars.runtimeVersion}**
- **[More info](${vars.link})**

<a href="${vars.qr}"><img src="${vars.qr}" width="250px" height="250px" /></a>

> Learn more about [𝝠 Expo Github Action](https://github.com/expo/expo-github-action#publish-a-preview-from-pr)`;
}
// If the updates are in different groups, we need to split the QR codes
}
function createMultipleQrSummary(updates, vars) {
const createTableHeader = (segments) => segments.filter(Boolean).join(' <br /> ');
const platformName = `Platform${updates.length === 1 ? '' : 's'}`;
const platformValue = updates.map(update => `**${update.platform}**`).join(', ');
const androidHeader = createTableHeader([
'Android',
vars.androidId && vars.androidRuntimeVersion ? `_(${vars.androidRuntimeVersion})_` : '',
vars.androidId && vars.androidLink ? `**[More info](${vars.androidLink})**` : '',
]);
const androidQr = vars.androidId && vars.androidQR
? `<a href="${vars.androidQR}"><img src="${vars.androidQR}" width="250px" height="250px" /></a>`
: null;
const iosHeader = createTableHeader([
'iOS',
vars.iosId && vars.iosRuntimeVersion ? `_(${vars.iosRuntimeVersion})_` : '',
vars.iosId && vars.iosLink ? `**[More info](${vars.iosLink})**` : '',
]);
const iosQr = vars.iosId && vars.iosQR
? `<a href="${vars.iosQR}"><img src="${vars.iosQR}" width="250px" height="250px" /></a>`
: null;
return `🚀 Expo preview is ready!

- Project → **${vars.projectSlug}**
- ${platformName} → ${platformValue}

Android <br /> ${vars.androidId ? `_(${vars.androidRuntimeVersion})_ <br />` : ''} ${vars.androidLink ? `**[More info](${vars.androidLink})**` : ''} | iOS <br /> ${vars.iosId ? `_(${vars.iosRuntimeVersion})_ <br />` : ''} ${vars.iosLink ? `**[More info](${vars.iosLink})**` : ''}
${androidHeader} | ${iosHeader}
--- | ---
${vars.androidId ? `<a href="${vars.androidQR}"><img src="${vars.androidQR}" width="250px" height="250px" /></a>` : '_not created_'} | ${vars.iosId ? `<a href="${vars.iosQR}"><img src="${vars.iosQR}" width="250px" height="250px" /></a>` : '_not created_'}
${androidQr || '_not created_'} | ${iosQr || '_not created_'}

> Learn more about [𝝠 Expo Github Action](https://github.com/expo/expo-github-action#publish-a-preview-from-pr)`;
}
Expand Down
56 changes: 46 additions & 10 deletions src/actions/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function previewAction(input = previewInput()) {

const variables = getVariables(config, updates);
const messageId = template(input.commentId, variables);
const messageBody = getMessage(updates, variables);
const messageBody = createSummary(updates, variables);

if (!input.shouldComment) {
info(`Skipped comment: 'comment' is disabled`);
Expand Down Expand Up @@ -141,31 +141,67 @@ export function getVariables(config: ExpoConfig, updates: EasUpdate[]) {
* Generate the message body for a single update.
* Note, this is not configurable, but you can use the variables used to construct your own.
*/
export function getMessage(updates: EasUpdate[], vars: ReturnType<typeof getVariables>) {
/* eslint-disable prettier/prettier */

export function createSummary(updates: EasUpdate[], vars: ReturnType<typeof getVariables>) {
// If all updates are in the same group, we can unify QR codes
if (updates.every(update => update.group === updates[0].group)) {
return `🚀 Expo preview is ready!
return createSingleQrSummary(updates, vars);
}

return createMultipleQrSummary(updates, vars);
}

function createSingleQrSummary(updates: EasUpdate[], vars: ReturnType<typeof getVariables>) {
const platformName = `Platform${updates.length === 1 ? '' : 's'}`;
const platformValue = updates.map(update => `**${update.platform}**`).join(', ');

return `🚀 Expo preview is ready!
- Project → **${vars.projectSlug}**
- Platform${updates.length === 1 ? '' : 's'}${updates.map(update => `**${update.platform}**`).join(', ')}
- ${platformName}${platformValue}
- Runtime Version → **${vars.runtimeVersion}**
- **[More info](${vars.link})**
<a href="${vars.qr}"><img src="${vars.qr}" width="250px" height="250px" /></a>
> Learn more about [𝝠 Expo Github Action](https://github.com/expo/expo-github-action#publish-a-preview-from-pr)`;
}
}

function createMultipleQrSummary(updates: EasUpdate[], vars: ReturnType<typeof getVariables>) {
const createTableHeader = (segments: string[]) => segments.filter(Boolean).join(' <br /> ');

const platformName = `Platform${updates.length === 1 ? '' : 's'}`;
const platformValue = updates.map(update => `**${update.platform}**`).join(', ');

const androidHeader = createTableHeader([
'Android',
vars.androidId && vars.androidRuntimeVersion ? `_(${vars.androidRuntimeVersion})_` : '',
vars.androidId && vars.androidLink ? `**[More info](${vars.androidLink})**` : '',
]);

const androidQr =
vars.androidId && vars.androidQR
? `<a href="${vars.androidQR}"><img src="${vars.androidQR}" width="250px" height="250px" /></a>`
: null;

const iosHeader = createTableHeader([
'iOS',
vars.iosId && vars.iosRuntimeVersion ? `_(${vars.iosRuntimeVersion})_` : '',
vars.iosId && vars.iosLink ? `**[More info](${vars.iosLink})**` : '',
]);

const iosQr =
vars.iosId && vars.iosQR
? `<a href="${vars.iosQR}"><img src="${vars.iosQR}" width="250px" height="250px" /></a>`
: null;

// If the updates are in different groups, we need to split the QR codes
return `🚀 Expo preview is ready!
- Project → **${vars.projectSlug}**
- ${platformName}${platformValue}
Android <br /> ${vars.androidId ? `_(${vars.androidRuntimeVersion})_ <br />` : ''} ${vars.androidLink ? `**[More info](${vars.androidLink})**` : ''} | iOS <br /> ${vars.iosId ? `_(${vars.iosRuntimeVersion})_ <br />` : ''} ${vars.iosLink ? `**[More info](${vars.iosLink})**` : ''}
${androidHeader} | ${iosHeader}
--- | ---
${vars.androidId ? `<a href="${vars.androidQR}"><img src="${vars.androidQR}" width="250px" height="250px" /></a>` : '_not created_'} | ${vars.iosId ? `<a href="${vars.iosQR}"><img src="${vars.iosQR}" width="250px" height="250px" /></a>` : '_not created_'}
${androidQr || '_not created_'} | ${iosQr || '_not created_'}
> Learn more about [𝝠 Expo Github Action](https://github.com/expo/expo-github-action#publish-a-preview-from-pr)`;
}
16 changes: 8 additions & 8 deletions tests/actions/preview.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExpoConfig } from '@expo/config';

import { getMessage, getVariables } from '../../src/actions/preview';
import { createSummary, getVariables } from '../../src/actions/preview';
import { EasUpdate } from '../../src/eas';

const fakeExpoConfig = {
Expand Down Expand Up @@ -37,10 +37,10 @@ const fakeUpdatesSingle: EasUpdate[] = [

const fakeUpdatesMultiple = fakeUpdatesSingle.map(update => ({ ...update, group: `fake-group-${update.id}` }));

describe(getMessage, () => {
describe(createSummary, () => {
describe('single update group', () => {
it('returns expected message for both platforms', () => {
expect(getMessage(fakeUpdatesSingle, getVariables(fakeExpoConfig, fakeUpdatesSingle))).toMatchInlineSnapshot(`
expect(createSummary(fakeUpdatesSingle, getVariables(fakeExpoConfig, fakeUpdatesSingle))).toMatchInlineSnapshot(`
"🚀 Expo preview is ready!
- Project → **fake-project**
Expand All @@ -55,7 +55,7 @@ describe(getMessage, () => {

it('returns expected message for android only', () => {
const fakeUpdate = fakeUpdatesSingle.filter(update => update.platform === 'android');
expect(getMessage(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
expect(createSummary(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
"🚀 Expo preview is ready!
- Project → **fake-project**
Expand All @@ -70,7 +70,7 @@ describe(getMessage, () => {

it('returns expected message for ios only', () => {
const fakeUpdate = fakeUpdatesSingle.filter(update => update.platform === 'ios');
expect(getMessage(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
expect(createSummary(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
"🚀 Expo preview is ready!
- Project → **fake-project**
Expand All @@ -86,7 +86,7 @@ describe(getMessage, () => {

describe('mutliple update groups', () => {
it('returns expected message for both platforms', () => {
expect(getMessage(fakeUpdatesMultiple, getVariables(fakeExpoConfig, fakeUpdatesMultiple))).toMatchInlineSnapshot(`
expect(createSummary(fakeUpdatesMultiple, getVariables(fakeExpoConfig, fakeUpdatesMultiple))).toMatchInlineSnapshot(`
"🚀 Expo preview is ready!
- Project → **fake-project**
Expand All @@ -101,7 +101,7 @@ describe(getMessage, () => {

it('returns expected message for android only', () => {
const fakeUpdate = fakeUpdatesSingle.filter(update => update.platform === 'android');
expect(getMessage(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
expect(createSummary(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
"🚀 Expo preview is ready!
- Project → **fake-project**
Expand All @@ -116,7 +116,7 @@ describe(getMessage, () => {

it('returns expected message for ios only', () => {
const fakeUpdate = fakeUpdatesSingle.filter(update => update.platform === 'ios');
expect(getMessage(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
expect(createSummary(fakeUpdate, getVariables(fakeExpoConfig, fakeUpdate))).toMatchInlineSnapshot(`
"🚀 Expo preview is ready!
- Project → **fake-project**
Expand Down

0 comments on commit feb07b6

Please sign in to comment.