Skip to content

Commit

Permalink
remove useless boolean / replaced switch case with ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
kalote committed Jul 5, 2019
1 parent c18cf13 commit 57c1cc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
35 changes: 12 additions & 23 deletions lib/utils/downloadTemplateFromRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ function validateUrl(url, hostname, service, owner, repo) {
}

/**
* isGhe = is github entreprise url?
* @param {Object} url
* @param {Boolean} isGhe
* @returns {Object}
*/
function parseGitHubURL(url, isGhe) {
function parseGitHubURL(url) {
const pathLength = 4;
const parts = url.pathname.split('/');
const isSubdirectory = parts.length > pathLength;
const owner = parts[1];
const repo = parts[2];
const branch = isSubdirectory ? parts[pathLength] : 'master';
const isGhe = url.hostname !== 'github.com';

if (!isGhe) {
// validate if given url is a valid GitHub url
Expand Down Expand Up @@ -157,27 +156,17 @@ function parseRepoURL(inputUrl) {
throw new ServerlessError('The URL you passed is not a valid URL');
}

switch (url.hostname) {
case 'github.com': {
return parseGitHubURL(url, false);
}
case 'bitbucket.org': {
return parseBitbucketURL(url);
}
case 'gitlab.com': {
return parseGitlabURL(url);
}
default: {
// test for github entreprise url
// check if the hostname contains 'github'
if (url.hostname.indexOf('github') !== -1) {
return parseGitHubURL(url, true);
}
const msg =
'The URL you passed is not one of the valid providers: "GitHub", "GitHub Entreprise", "Bitbucket", or "GitLab".';
throw new ServerlessError(msg);
}
if (url.hostname === 'github.com' || url.hostname.indexOf('github.') !== -1) {
return parseGitHubURL(url);
} else if (url.hostname === 'bitbucket.org') {
return parseBitbucketURL(url);
} else if (url.hostname === 'gitlab.com') {
return parseGitlabURL(url);
}

const msg =
'The URL you passed is not one of the valid providers: "GitHub", "GitHub Entreprise", "Bitbucket", or "GitLab".';
throw new ServerlessError(msg);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/downloadTemplateFromRepo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ describe('downloadTemplateFromRepo', () => {
});

it('should parse a valid GitHub Entreprise with subdirectory', () => {
const output = parseRepoURL('https://github.mydomain.com/serverless/serverless/tree/master/assets');
const output = parseRepoURL(
'https://github.mydomain.com/serverless/serverless/tree/master/assets'
);

expect(output).to.deep.eq({
owner: 'serverless',
Expand All @@ -209,8 +211,8 @@ describe('downloadTemplateFromRepo', () => {
branch: 'master',
downloadUrl: 'https://github.com/serverless/serverless/archive/master.zip',
isSubdirectory: false,
pathToDirectory: '',
auth: 'username:password',
pathToDirectory: '',
});
});

Expand Down

0 comments on commit 57c1cc3

Please sign in to comment.