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

Add repository info #101

Merged
merged 6 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
}
],
"stage-2"
],
"plugins": [
"rewire"
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"escape-html": "1.0.3",
"got": "8.0.1",
"gravatar-url": "2.0.0",
"hosted-git-info": "^2.5.0",
"lodash": "4.17.4",
"ms": "2.1.1",
"nice-package": "3.0.3",
Expand All @@ -52,6 +53,7 @@
"truncate-utf8-bytes": "1.0.2"
},
"devDependencies": {
"babel-plugin-rewire": "^1.1.0",
"babel-eslint": "8.0.3",
"doctoc": "1.3.0",
"eslint": "4.13.1",
Expand Down
22 changes: 21 additions & 1 deletion src/__tests__/__snapshots__/formatPkg.test.js.snap

Large diffs are not rendered by default.

131 changes: 130 additions & 1 deletion src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,138 @@ it('truncates long readmes', () => {
formatted.readme.length - truncatedEnding.length
);

expect(formatted.readme).toHaveLength(451050);
expect(formatted.readme).toHaveLength(451070);
expect(ending).toBe(truncatedEnding);

formatted.lastCrawl = '<!-- date replaced -->';
expect(formatted).toMatchSnapshot();
});

describe('test getRepositoryInfo', () => {
const getRepositoryInfo = formatPkg.__RewireAPI__.__get__(
'getRepositoryInfo'
);

it('should get information from short repository url', () => {
expect(getRepositoryInfo('gitlab:user/repo')).toEqual({
host: 'gitlab.com',
user: 'user',
project: 'repo',
path: '',
});

expect(getRepositoryInfo('github:user/repo')).toEqual({
host: 'github.com',
user: 'user',
project: 'repo',
path: '',
});

expect(getRepositoryInfo('bitbucket:user/repo')).toEqual({
host: 'bitbucket.org',
user: 'user',
project: 'repo',
path: '',
});
});

it('should get information from repository (git?+http) URLs', () => {
expect(
getRepositoryInfo(
'https://github.com/babel/babel/tree/master/packages/babel'
)
).toEqual({
host: 'github.com',
user: 'babel',
project: 'babel',
path: '/tree/master/packages/babel',
});

expect(
getRepositoryInfo(
'https://gitlab.com/user/repo/tree/master/packages/a-package'
)
).toEqual({
host: 'gitlab.com',
user: 'user',
project: 'repo',
path: '/tree/master/packages/a-package',
});

expect(
getRepositoryInfo(
'https://bitbucket.org/user/repo/src/ae8df4cd0e809a789e3f96fd114075191c0d5c8b/packages/project1'
)
).toEqual({
host: 'bitbucket.org',
user: 'user',
project: 'repo',
path: '/src/ae8df4cd0e809a789e3f96fd114075191c0d5c8b/packages/project1',
});

expect(
getRepositoryInfo(
'git+https://bitbucket.org/atlassian/confluence-web-components.git'
)
).toEqual({
host: 'bitbucket.org',
user: 'atlassian',
project: 'confluence-web-components',
path: '',
});

expect(
getRepositoryInfo('https://bitbucket.org/2klicdev/2klic-sdk.git')
).toEqual({
host: 'bitbucket.org',
user: '2klicdev',
project: '2klic-sdk',
path: '',
});
});

it('should get information from repository objects', () => {
const githubRepo = {
type: 'git',
url: 'https://github.com/webpack/webpack.git',
};

const gitlabRepo = {
type: 'git',
url: 'git+https://gitlab.com/hyper-expanse/semantic-release-gitlab.git',
};

const bitbucketRepo = {
type: 'git',
url: 'git+https://bitbucket.org/2klicdev/2klic-sdk.git',
};

expect(getRepositoryInfo(githubRepo)).toEqual({
host: 'github.com',
user: 'webpack',
project: 'webpack',
path: '',
});

expect(getRepositoryInfo(gitlabRepo)).toEqual({
host: 'gitlab.com',
user: 'hyper-expanse',
project: 'semantic-release-gitlab',
path: '',
});

expect(getRepositoryInfo(bitbucketRepo)).toEqual({
host: 'bitbucket.org',
user: '2klicdev',
project: '2klic-sdk',
path: '',
});
});

it('should return null if it cannot get information', () => {
expect(getRepositoryInfo('')).toBe(null);
expect(getRepositoryInfo(undefined)).toBe(null);
expect(getRepositoryInfo(null)).toBe(null);
expect(getRepositoryInfo('aaaaaaaa')).toBe(null);
});
});
73 changes: 73 additions & 0 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const defaultGravatar = 'https://www.gravatar.com/avatar/';
import escape from 'escape-html';
import traverse from 'traverse';
import truncate from 'truncate-utf8-bytes';
import hostedGitInfo from 'hosted-git-info';

import c from './config';

Expand Down Expand Up @@ -41,6 +42,18 @@ export default function formatPkg(pkg) {
const dependencies = cleaned.dependencies || {};
const devDependencies = cleaned.devDependencies || {};
const concatenatedName = cleaned.name.replace(/[-/@_.]+/g, '');
const defaultRepository =
typeof cleaned.repository === 'string'
? { url: cleaned.repository }
: cleaned.repository;
const repository = cleaned.repository
? {
...defaultRepository, // Default info: type, url
...getRepositoryInfo(cleaned.repository), // Extra info: host, project, user...
head: cleaned.gitHead,
branch: cleaned.gitHead || 'master',
}
: null;

const tags = pkg['dist-tags'];

Expand All @@ -59,6 +72,7 @@ export default function formatPkg(pkg) {
dependencies,
devDependencies,
originalAuthor: cleaned.author,
repository,
githubRepo,
gitHead: githubRepo && githubRepo.head, // remove this when we update to the new schema frontend
readme: pkg.readme,
Expand Down Expand Up @@ -222,6 +236,65 @@ function getHomePage(homepage, repository) {
return null;
}

/**
* Get info from urls like this: (has multiple packages in one repo, like babel does)
* https://github.com/babel/babel/tree/master/packages/babel
* https://gitlab.com/user/repo/tree/master/packages/project1
* https://bitbucket.org/user/repo/src/ae8df4cd0e809a789e3f96fd114075191c0d5c8b/packages/project1/
*
* This function is like getGitHubRepoInfo (above), but support github, gitlab and bitbucket.
*/
function getRepositoryInfoFromHttpUrl(repository) {
const result = repository.match(
/^https?:\/\/(?:www\.)?((?:github|gitlab|bitbucket)).((?:com|org))\/([^/]+)\/([^/]+)(\/.+)?$/
);

if (!result || result.length < 4) {
return null;
}

return {
host: `${result[1]}.${result[2]}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here (and above as well) it would be prettier if we destructured result

Copy link
Contributor Author

@leonardosnt leonardosnt Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like this:

  const [, domain, domainTld, user, project, path = ''] = result;
  
  return {
    host: `${domain}.${domainTld}`,
    user,
    project,
    path,
  };

right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, that seems nicer to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and above as well

Where? do you mean in another function? which one?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant in getGithubInfo, to which you referred in the comment

user: result[3],
project: result[4],
path: result[5] || '',
};
}

function getRepositoryInfo(repository) {
if (!repository) {
return null;
}

const url = typeof repository === 'string' ? repository : repository.url;

if (!url) {
return null;
}

/**
* Get information using hosted-git-info.
*/
const repositoryInfo = hostedGitInfo.fromUrl(url);

if (repositoryInfo) {
const { project, user, domain } = repositoryInfo;
return {
project,
user,
host: domain,
path: '',
};
}

/**
* Unfortunately, hosted-git-info can't handle URL like this: (has path)
* https://github.com/babel/babel/tree/master/packages/babel-core
* so we need to do it
*/
return getRepositoryInfoFromHttpUrl(url);
}

function formatUser(user) {
return {
...user,
Expand Down
6 changes: 5 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ babel-plugin-jest-hoist@^22.1.0:
version "22.1.0"
resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-22.1.0.tgz#c1281dd7887d77a1711dc760468c3b8285dde9ee"

babel-plugin-rewire@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/babel-plugin-rewire/-/babel-plugin-rewire-1.1.0.tgz#a6b966d9d8c06c03d95dcda2eec4e2521519549b"

babel-plugin-syntax-async-functions@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"
Expand Down Expand Up @@ -2587,7 +2591,7 @@ home-or-tmp@^2.0.0:
os-homedir "^1.0.0"
os-tmpdir "^1.0.1"

hosted-git-info@^2.1.4:
hosted-git-info@^2.1.4, hosted-git-info@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.5.0.tgz#6d60e34b3abbc8313062c3b798ef8d901a07af3c"

Expand Down