Skip to content

Commit

Permalink
Merge pull request #53 from algolia/feat/schema-githead
Browse files Browse the repository at this point in the history
feat(schema): move git head into githubRepo
  • Loading branch information
vvo authored Aug 28, 2017
2 parents 14d7ea8 + 837bf5a commit c004ed8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ For every single NPM package, we create a record in the Algolia index. The resul
"githubRepo": {
"user": "babel",
"project": "babel",
"path": "/tree/master/packages/babel-core"
"path": "/tree/master/packages/babel-core",
"head": "f6ad789eba27db50d25cc7eac062bbb0dde19c16"
},
"readme": "# babel-core\n> Babel compiler core.\n```javascript\nvar babel = require(\"babel-core\");\nimport { transform } from 'babel-core';\nimport * as babel from 'babel-core';\n```\nAll transformations will use your local configuration files (.babelrc or in package.json). See [options](#options) to disable it.\n## babel.transform(code: string, [options?](#options): Object)\nTransforms the passed in `code`. Returning an object with the generated code,\nsource map, and AST.\n```js\nbabel.transform(code, options)", //truncated at 200kb with **TRUNCATED**
"owner": {
Expand Down
30 changes: 16 additions & 14 deletions formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ export default function formatPkg(pkg) {
return undefined;
}

const githubRepo = cleaned.repository
? getGitHubRepoInfo(cleaned.repository)
: null;
const lastPublisher = cleaned.lastPublisher
? formatUser(cleaned.lastPublisher)
: null;
const author = getAuthor(cleaned);
const license = getLicense(cleaned);

const version = cleaned.version ? cleaned.version : '0.0.0';

const gitHead = getGitHead(pkg, version);
const versions = getVersions(cleaned);
const githubRepo = cleaned.repository
? getGitHubRepoInfo({ repository: cleaned.repository, versions, version })
: null;

if (!githubRepo && !lastPublisher && !author) {
return undefined; // ignore this package, we cannot link it to anyone
Expand Down Expand Up @@ -61,7 +60,7 @@ export default function formatPkg(pkg) {
devDependencies,
originalAuthor: cleaned.author,
githubRepo,
gitHead,
gitHead: githubRepo.head, // remove this when we update to the new schema frontend
readme: pkg.readme,
owner,
deprecated: cleaned.deprecated !== undefined ? cleaned.deprecated : false,
Expand Down Expand Up @@ -154,13 +153,6 @@ function getGravatar(obj) {
return gravatarUrl(obj.email);
}

function getGitHead(pkg, version) {
if (pkg.versions && pkg.versions[version] && pkg.versions[version].gitHead) {
return pkg.versions[version].gitHead;
}
return 'master';
}

function getVersions(cleaned) {
if (cleaned.other && cleaned.other.time) {
return Object.keys(cleaned.other.time)
Expand All @@ -185,7 +177,14 @@ function getKeywords(cleaned) {
return [];
}

function getGitHubRepoInfo(repository) {
function getGitHead({ versions, version }) {
if (versions[version] && versions[version].gitHead) {
return versions[version].gitHead;
}
return 'master';
}

function getGitHubRepoInfo({ repository, versions, version }) {
if (!repository || typeof repository !== 'string') return null;

const result = repository.match(
Expand All @@ -200,10 +199,13 @@ function getGitHubRepoInfo(repository) {
return null;
}

const head = getGitHead({ versions, version });

return {
user: result[1],
project: result[2],
path: result[3] || '',
head,
};
}

Expand Down
6 changes: 3 additions & 3 deletions github.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import got from 'got';
import race from 'promise-rat-race';

function getChangelog({ githubRepo, gitHead }) {
function getChangelog(githubRepo) {
if (githubRepo === null) {
return { changelogFilename: null };
}

const { user, project, path } = githubRepo;
const { user, project, path, head } = githubRepo;
if (user.length < 1 || project.length < 1) {
return { changelogFilename: null };
}

const baseGithubURL = `https://raw.githubusercontent.com/${user}/${project}/${gitHead}/${`${path.replace('/tree/', '')}`}`;
const baseGithubURL = `https://raw.githubusercontent.com/${user}/${project}/${head}/${`${path.replace('/tree/', '')}`}`;
const files = [
'CHANGELOG.md',
'ChangeLog.md',
Expand Down

0 comments on commit c004ed8

Please sign in to comment.