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 cran support #255

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: '10.x'
node-version: '12.x'
Copy link
Member Author

Choose a reason for hiding this comment

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

Sync Node.js version with version specified in the package.json file

api/package.json

Lines 38 to 40 in 2758891

"engines": {
"node": "12.x"
},

- run: npm ci
- run: npm test
env:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Type must be one of:
- `java`
- `go`
- `pub`
- `cran`
- `ping`

Response:
Expand Down
15 changes: 15 additions & 0 deletions functional.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,19 @@ describe('functional', () => {
'path',
'https://github.com/dart-lang/path',
);
testBulk(
'cran',
'fracdiff',
'https://github.com/mmaechler/fracdiff',
);
testBulk(
'cran',
'usethis',
'https://github.com/r-lib/usethis',
);
testBulk(
'cran',
'boot',
'https://github.com/cran/boot',
);
});
2 changes: 1 addition & 1 deletion src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = async (req, res) => {
await cache.auth();
const timingCacheAuth = Date.now() - timingCacheAuthStart;

let result;
let result = [];
Copy link
Member Author

Choose a reason for hiding this comment

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

result is being used within the finally catch block, which could break under some conditions. Adding an initial value helps preventing this

let timingTotalEnd;
let completed = false;
const payload = preparePayload(body);
Expand Down
7 changes: 7 additions & 0 deletions src/registries/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"/project_uri"
]
},
"cran": {
"registry": "https://crandb.r-pkg.org/%s",
"fallback": "https://github.com/cran/%s",
"xpaths": [
"/URL"
]
},
"npm": {
"registry": "https://registry.npmjs.org/%s",
"fallback": "https://www.npmjs.com/package/%s",
Expand Down
10 changes: 9 additions & 1 deletion src/registries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const xpathHelper = require('./xpath-helper');
const registryConfig = require('./config.json');
const cache = require('../utils/cache');
const log = require('../utils/log');
const { prioritiesHost } = require('../utils/url');

async function resolve(type, packageName) {
const cacheKey = `${type}_${packageName}`;
Expand Down Expand Up @@ -47,7 +48,7 @@ async function resolve(type, packageName) {
return;
}

const urls = xpathHelper(json, config.xpaths);
let urls = xpathHelper(json, config.xpaths);

if (type === 'npm') {
if (json.repository && json.repository.directory) {
Expand All @@ -63,6 +64,13 @@ async function resolve(type, packageName) {
}
}

if (type === 'cran') {
// Some packages export multiple urls seperated by comma.
urls = urls.map((url) => url.split(',').map((str) => str.trim())).flat();

urls = prioritiesHost('https://github.com', urls);
}

const validUrls = urls.map((bestMatchUrl) => {
try {
let url = repositoryUrl(bestMatchUrl);
Expand Down
15 changes: 15 additions & 0 deletions src/utils/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function prioritiesHost(host, urls = []) {
let insertIndex = 0;
return urls.reduce((memo, url) => {
if (url.includes(host)) {
memo.splice(insertIndex++, 0, url); // eslint-disable-line no-plusplus
} else {
memo.push(url);
}
return memo;
}, []);
}

module.exports = {
prioritiesHost,
};
39 changes: 39 additions & 0 deletions src/utils/url.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { prioritiesHost } = require('./url.js');

const inputUrls = [
'https://foo.com',
'https://github.com/foo',
'https://bar.com',
'https://github.com/bar',
];

describe('url', () => {
describe('prioritiesHost', () => {
it('priorities github.com', () => {
expect(prioritiesHost('github.com', inputUrls)).toStrictEqual([
'https://github.com/foo',
'https://github.com/bar',
'https://foo.com',
'https://bar.com',
]);
});

it('priorities foo.com', () => {
expect(prioritiesHost('foo.com', inputUrls)).toStrictEqual([
'https://foo.com',
'https://github.com/foo',
'https://bar.com',
'https://github.com/bar',
]);
});

it('priorities bar.com', () => {
expect(prioritiesHost('bar.com', inputUrls)).toStrictEqual([
'https://bar.com',
'https://foo.com',
'https://github.com/foo',
'https://github.com/bar',
]);
});
});
});