Skip to content

Commit

Permalink
Add cran support
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck committed Oct 1, 2020
1 parent 2758891 commit ad41e1c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 3 deletions.
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'
- 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 = [];
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((s) => s.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',
]);
});
});
});

0 comments on commit ad41e1c

Please sign in to comment.