Skip to content

Commit 043d287

Browse files
arthurdennerCompuIves
authored andcommitted
Improving GitHub URL parse (codesandbox#448)
* refactor(github-integration): Adding better URL parse * test(github-integration): Adding tests to the gitHubRepoPattern * chore: Adding myself to contributors table
1 parent 66958e1 commit 043d287

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@
245245
"contributions": [
246246
"code"
247247
]
248+
},
249+
{
250+
"login": "arthurdenner",
251+
"name": "Arthur Denner",
252+
"avatar_url": "https://avatars0.githubusercontent.com/u/13774309?v=4",
253+
"profile": "https://github.com/arthurdenner",
254+
"contributions": [
255+
"code"
256+
]
248257
}
249258
]
250259
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [CodeSandbox](https://codesandbox.io) [![Chat](https://img.shields.io/badge/chat-on%20discord-7289da.svg)](https://discord.gg/KE3TbEZ) [![All Contributors](https://img.shields.io/badge/all_contributors-24-orange.svg?style=flat-square)](#contributors) [![Build Status](https://travis-ci.org/CompuIves/codesandbox-client.svg?branch=master)](https://travis-ci.org/CompuIves/codesandbox-client) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![first-timers-only Friendly](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](http://www.firsttimersonly.com/)
1+
# [CodeSandbox](https://codesandbox.io) [![Chat](https://img.shields.io/badge/chat-on%20discord-7289da.svg)](https://discord.gg/KE3TbEZ) [![All Contributors](https://img.shields.io/badge/all_contributors-25-orange.svg?style=flat-square)](#contributors) [![Build Status](https://travis-ci.org/CompuIves/codesandbox-client.svg?branch=master)](https://travis-ci.org/CompuIves/codesandbox-client) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![first-timers-only Friendly](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](http://www.firsttimersonly.com/)
22

33
An online code editor tailored for web applications.
44

@@ -40,5 +40,5 @@ Thanks goes to these wonderful people
4040
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
4141
| [<img src="https://avatars3.githubusercontent.com/u/1239401?v=4" width="100px;"/><br /><sub><b>Johann Hubert Sonntagbauer</b></sub>](https://github.com/johann-sonntagbauer)<br />[🐛](https://github.com/CompuIves/codesandbox-client/issues?q=author%3Ajohann-sonntagbauer "Bug reports") [💻](https://github.com/CompuIves/codesandbox-client/commits?author=johann-sonntagbauer "Code") | [<img src="https://avatars2.githubusercontent.com/u/9586897?v=4" width="100px;"/><br /><sub><b>Joachim Seminck</b></sub>](https://github.com/jseminck)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=jseminck "Code") | [<img src="https://avatars3.githubusercontent.com/u/5210019?v=4" width="100px;"/><br /><sub><b>Subramanya Chakravarthy</b></sub>](http://chakrihacker.github.io)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=chakrihacker "Code") | [<img src="https://avatars3.githubusercontent.com/u/23088?v=4" width="100px;"/><br /><sub><b>Robert (Robby) O'Connor</b></sub>](http://robby.oconnor.ninja)<br />[🚇](#infra-robbyoconnor "Infrastructure (Hosting, Build-Tools, etc)") | [<img src="https://avatars0.githubusercontent.com/u/2083930?v=4" width="100px;"/><br /><sub><b>Bogdan Luca</b></sub>](https://github.com/lbogdan)<br />[🐛](https://github.com/CompuIves/codesandbox-client/issues?q=author%3Albogdan "Bug reports") [💻](https://github.com/CompuIves/codesandbox-client/commits?author=lbogdan "Code") | [<img src="https://avatars3.githubusercontent.com/u/6177621?v=4" width="100px;"/><br /><sub><b>Divjot Singh</b></sub>](http://bogas04.github.io)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=bogas04 "Code") | [<img src="https://avatars3.githubusercontent.com/u/5249539?v=4" width="100px;"/><br /><sub><b>Jason Nall</b></sub>](http://www.jsonnull.com)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=jsonnull "Code") |
4242
| [<img src="https://avatars3.githubusercontent.com/u/784056?v=4" width="100px;"/><br /><sub><b>Lionel</b></sub>](https://elrumordelaluz.com)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=elrumordelaluz "Code") [🎨](#design-elrumordelaluz "Design") | [<img src="https://avatars3.githubusercontent.com/u/170500?v=4" width="100px;"/><br /><sub><b>Philipp Brumm</b></sub>](https://github.com/brumm)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=brumm "Code") | [<img src="https://avatars2.githubusercontent.com/u/2678610?v=4" width="100px;"/><br /><sub><b>Valentin Hervieu</b></sub>](http://valentin-hervieu.fr)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=ValentinH "Code") [🐛](https://github.com/CompuIves/codesandbox-client/issues?q=author%3AValentinH "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/1499218?v=4" width="100px;"/><br /><sub><b>Anenth</b></sub>](http://anenth.js.org)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=Anenth "Code") [🎨](#design-Anenth "Design") [🤔](#ideas-Anenth "Ideas, Planning, & Feedback") | [<img src="https://avatars0.githubusercontent.com/u/410792?v=4" width="100px;"/><br /><sub><b>Dony Sukardi</b></sub>](http://dsds.io)<br />[🐛](https://github.com/CompuIves/codesandbox-client/issues?q=author%3Adonysukardi "Bug reports") [💻](https://github.com/CompuIves/codesandbox-client/commits?author=donysukardi "Code") | [<img src="https://avatars3.githubusercontent.com/u/89046?v=4" width="100px;"/><br /><sub><b>Geoffrey Dhuyvetters</b></sub>](https://github.com/duivvv)<br />[🎨](#design-duivvv "Design") [💻](https://github.com/CompuIves/codesandbox-client/commits?author=duivvv "Code") | [<img src="https://avatars3.githubusercontent.com/u/3381746?v=4" width="100px;"/><br /><sub><b>Eswar Yaganti</b></sub>](http://nyaganti.com)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=nagamalli9999 "Code") [🚇](#infra-nagamalli9999 "Infrastructure (Hosting, Build-Tools, etc)") |
43-
| [<img src="https://avatars3.githubusercontent.com/u/9488719?v=4" width="100px;"/><br /><sub><b>Frank Tan</b></sub>](https://github.com/tansongyang)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=tansongyang "Code") | [<img src="https://avatars0.githubusercontent.com/u/1650995?v=4" width="100px;"/><br /><sub><b>Bilal Budhani</b></sub>](https://bilalbudhani.com)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=BilalBudhani "Code") | [<img src="https://avatars3.githubusercontent.com/u/843342?v=4" width="100px;"/><br /><sub><b>El Juli</b></sub>](https://github.com/JulianMayorga)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=JulianMayorga "Code") |
43+
| [<img src="https://avatars3.githubusercontent.com/u/9488719?v=4" width="100px;"/><br /><sub><b>Frank Tan</b></sub>](https://github.com/tansongyang)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=tansongyang "Code") | [<img src="https://avatars0.githubusercontent.com/u/1650995?v=4" width="100px;"/><br /><sub><b>Bilal Budhani</b></sub>](https://bilalbudhani.com)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=BilalBudhani "Code") | [<img src="https://avatars3.githubusercontent.com/u/843342?v=4" width="100px;"/><br /><sub><b>El Juli</b></sub>](https://github.com/JulianMayorga)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=JulianMayorga "Code") | [<img src="https://avatars0.githubusercontent.com/u/13774309?v=4" width="100px;"/><br /><sub><b>Arthur Denner</b></sub>](https://github.com/arthurdenner)<br />[💻](https://github.com/CompuIves/codesandbox-client/commits?author=arthurdenner "Code") |
4444
<!-- ALL-CONTRIBUTORS-LIST:END -->

packages/app/src/app/pages/GitHub/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import MaxWidth from 'common/components/flex/MaxWidth';
1010
import Margin from 'common/components/spacing/Margin';
1111
import Input from 'app/components/Input';
1212
import Button from 'app/components/buttons/Button';
13-
import { gitHubToSandboxUrl, protocolAndHost } from 'common/utils/url-generator';
13+
import {
14+
gitHubRepoPattern,
15+
gitHubToSandboxUrl,
16+
protocolAndHost,
17+
} from 'common/utils/url-generator';
1418

1519
const Container = styled.div`
1620
height: 100%;
@@ -31,7 +35,9 @@ const Label = styled.label`
3135
color: rgba(255, 255, 255, 0.3);
3236
`;
3337

34-
const Description = styled.div`margin-bottom: 1rem;`;
38+
const Description = styled.div`
39+
margin-bottom: 1rem;
40+
`;
3541

3642
const StyledInput = styled(Input)`
3743
font-size: 1.25rem;
@@ -62,15 +68,22 @@ export default class GitHub extends React.PureComponent<{}, State> {
6268
updateUrl = e => {
6369
const url = e.target.value;
6470

65-
if (!url.includes('github.com')) {
71+
if (!url) {
72+
this.setState({
73+
url,
74+
error: null,
75+
transformedUrl: '',
76+
});
77+
} else if (!gitHubRepoPattern.test(url)) {
6678
this.setState({
6779
url,
68-
error: "The URL should contain from 'github.com'.",
80+
error: 'The URL provided is not valid.',
81+
transformedUrl: '',
6982
});
7083
} else {
7184
this.setState({
7285
url: e.target.value,
73-
transformedUrl: getFullGitHubUrl(url),
86+
transformedUrl: getFullGitHubUrl(url.trim()),
7487
error: null,
7588
});
7689
}

packages/common/utils/url-generator.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// @flow
22
import type { Sandbox } from 'common/types';
33

4+
export const gitHubRepoPattern = /(https?:\/\/)?((www.)?)github.com(\/\w+){2,}/;
5+
const gitHubPrefix = /(https?:\/\/)?((www.)?)github.com/;
6+
const dotGit = /(\.git)$/;
7+
48
const buildEncodedUri = (strings: Array<string>, ...values: Array<string>) =>
59
strings[0] +
610
values
@@ -115,7 +119,7 @@ export const optionsToParameterizedUrl = (options: Object) => {
115119
};
116120

117121
export const gitHubToSandboxUrl = (githubUrl: string) =>
118-
githubUrl.replace(/https?:\/\/(www.)?github.com/, '/s/github');
122+
githubUrl.replace(gitHubPrefix, '/s/github').replace(dotGit, '');
119123

120124
export const searchUrl = query => `/search${query ? `?query=${query}` : ''}`;
121125
export const patronUrl = () => `/patron`;
Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1-
import { gitHubToSandboxUrl } from './url-generator';
1+
import { gitHubRepoPattern, gitHubToSandboxUrl } from './url-generator';
2+
3+
const invalidUrls = [
4+
'github.com/',
5+
'github.com/user/',
6+
'http://github.com/',
7+
'http://github.com/user/',
8+
'http://www.github.com/',
9+
'http://www.github.com/user/',
10+
'https://github.com/user/',
11+
'https://www.github.com/user/',
12+
'www.github.com/',
13+
'www.github.com/user/',
14+
];
15+
16+
const validUrls = [
17+
'github.com/user/repo',
18+
'http://github.com/user/repo',
19+
'http://www.github.com/user/repo',
20+
'https://github.com/user/repo',
21+
'https://www.github.com/user/repo',
22+
'www.github.com/user/repo',
23+
];
224

325
describe('url-generator', () => {
426
describe('gitHubToSandboxUrl', () => {
5-
[
6-
'http://github.com/user/repo',
7-
'http://www.github.com/user/repo',
8-
'https://github.com/user/repo',
9-
'https://www.github.com/user/repo',
10-
].forEach(inputUrl => {
27+
validUrls.forEach(inputUrl => {
1128
test(`handles ${inputUrl} urls`, () => {
1229
expect(gitHubToSandboxUrl(inputUrl)).toBe('/s/github/user/repo');
1330
});
1431
});
1532
});
33+
34+
describe('gitHubRepoPattern', () => {
35+
validUrls.forEach(inputUrl => {
36+
test(`validates ${inputUrl} as truthy`, () => {
37+
expect(gitHubRepoPattern.test(inputUrl)).toBeTruthy();
38+
});
39+
});
40+
41+
invalidUrls.forEach(inputUrl => {
42+
test(`validates ${inputUrl} as falsy`, () => {
43+
expect(gitHubRepoPattern.test(inputUrl)).toBeFalsy();
44+
});
45+
});
46+
});
1647
});

0 commit comments

Comments
 (0)