From fd955635e57368de46998487ec54bae47bf26d97 Mon Sep 17 00:00:00 2001 From: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Date: Tue, 20 Apr 2021 22:47:42 +0100 Subject: [PATCH] feat(client): transform app url to editor (#41858) --- .../Challenges/redux/create-question-epic.js | 3 +- .../redux/create-question-epic.test.js | 32 +++++++++++++++++++ .../src/templates/Challenges/utils/index.js | 12 +++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 client/src/templates/Challenges/redux/create-question-epic.test.js diff --git a/client/src/templates/Challenges/redux/create-question-epic.js b/client/src/templates/Challenges/redux/create-question-epic.js index 946cd62cb0..7dba35a24a 100644 --- a/client/src/templates/Challenges/redux/create-question-epic.js +++ b/client/src/templates/Challenges/redux/create-question-epic.js @@ -8,6 +8,7 @@ import { projectFormValuesSelector } from '../redux'; import { tap, mapTo } from 'rxjs/operators'; +import { transformEditorLink } from '../utils'; import envData from '../../../../../config/env.json'; const { forumLocation } = envData; @@ -63,7 +64,7 @@ function createQuestionEpic(action$, state$, { window }) { } ${ projectFormValues - ?.map(([key, val]) => `${key}: ${val}\n`) + ?.map(([key, val]) => `${key}: ${transformEditorLink(val)}\n`) ?.join('') || filesToMarkdown(files) } diff --git a/client/src/templates/Challenges/redux/create-question-epic.test.js b/client/src/templates/Challenges/redux/create-question-epic.test.js new file mode 100644 index 0000000000..c613c5e4a4 --- /dev/null +++ b/client/src/templates/Challenges/redux/create-question-epic.test.js @@ -0,0 +1,32 @@ +/* global expect */ + +import { transformEditorLink } from '../utils'; + +describe('create-question-epic', () => { + describe('transformEditorLink', () => { + const links = [ + { + input: 'https://some-project.camperbot.repl.co', + expected: 'https://replit.com/@camperbot/some-project' + }, + { + input: 'https://some-project.glitch.me/', + expected: 'https://glitch.com/edit/#!/some-project' + }, + { + input: 'https://github.com/user/repo-name', + expected: 'https://github.com/user/repo-name' + } + ]; + it('should correctly transform app links to editor links', () => { + links.forEach(link => { + expect(transformEditorLink(link.input)).toStrictEqual(link.expected); + }); + }); + it('should not transform editor links in GitHub submission', () => { + links.forEach(link => { + expect(transformEditorLink(link.expected)).toStrictEqual(link.expected); + }); + }); + }); +}); diff --git a/client/src/templates/Challenges/utils/index.js b/client/src/templates/Challenges/utils/index.js index a9d4237ffa..5b3a02ecf3 100644 --- a/client/src/templates/Challenges/utils/index.js +++ b/client/src/templates/Challenges/utils/index.js @@ -13,3 +13,15 @@ export function isGoodXHRStatus(status) { const statusInt = parseInt(status, 10); return (statusInt >= 200 && statusInt < 400) || statusInt === 402; } + +export function transformEditorLink(url) { + return url + .replace( + /(?<=\/\/)(?[^.]+)\.(?[^.]+)\.repl\.co\/?/, + 'replit.com/@$/$' + ) + .replace( + /(?<=\/\/)(?[^.]+)\.glitch\.me\/?/, + 'glitch.com/edit/#!/$' + ); +}