@artginzburg/github-user-downloads
Advanced tools
Comparing version 0.2.0 to 0.3.0
27
app.js
@@ -1,26 +0,7 @@ | ||
const getUserDownloads = require('./index.js'); | ||
const express = require('express'); | ||
const { endpoint } = require('./endpoint'); | ||
const app = express(); | ||
const { PORT = 3000 } = process.env; | ||
const shieldsSchema = { | ||
schemaVersion: 1, | ||
label: 'downloads', | ||
color: 'teal', | ||
namedLogo: 'github', | ||
style: 'social', | ||
cacheSeconds: 24 * 60 * 60, // 24h | ||
}; | ||
app.get('/:user', async (req, res) => { | ||
const userDownloads = await getUserDownloads(req.params.user); | ||
res.end( | ||
JSON.stringify({ | ||
...shieldsSchema, | ||
message: String(userDownloads.total), | ||
}), | ||
); | ||
endpoint.listen(PORT, () => { | ||
console.log(`Endpoint listening on http://localhost:${PORT}`); | ||
}); | ||
module.exports.endpoint = app; |
22
index.js
const { Octokit } = require('@octokit/rest'); | ||
async function getUserDownloads(username, personalAccessToken) { | ||
const octokit = new Octokit({ auth: personalAccessToken }); | ||
async function getUserDownloads(username, auth) { | ||
const octokit = new Octokit({ auth }); | ||
const { data: repos } = await octokit.rest.repos.listForUser({ | ||
username, | ||
}); | ||
const userDownloads = { | ||
@@ -15,2 +11,6 @@ total: 0, | ||
const { data: repos } = await octokit.rest.repos.listForUser({ | ||
username, | ||
}); | ||
for (const repo of repos) { | ||
@@ -35,13 +35,7 @@ const { data: releases } = await octokit.rest.repos.listReleases({ | ||
const releaseDownloadCount = assets.reduce( | ||
(releaseDownloadsAccumulator, currentAsset) => | ||
releaseDownloadsAccumulator + currentAsset.download_count, | ||
0, | ||
); | ||
repoDownloadCount += releaseDownloadCount; | ||
repoDownloadCount += assets.reduce((accum, asset) => accum + asset.download_count, 0); | ||
} | ||
userDownloads.total += repoDownloadCount; | ||
userDownloads.data.push([{ name: repo.name, download_count: repoDownloadCount }]); | ||
userDownloads.data.push({ name: repo.name, download_count: repoDownloadCount }); | ||
} | ||
@@ -48,0 +42,0 @@ |
{ | ||
"name": "@artginzburg/github-user-downloads", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Get total downloads of all repos by username", | ||
@@ -13,5 +13,6 @@ "license": "ISC", | ||
}, | ||
"main": "app.js", | ||
"main": "endpoint.js", | ||
"scripts": { | ||
"start": "node test" | ||
"start": "node test", | ||
"test": "node app" | ||
}, | ||
@@ -18,0 +19,0 @@ "dependencies": { |
12
test.js
@@ -1,5 +0,13 @@ | ||
const getUserDownloads = require('./index.js'); | ||
const getUserDownloads = require('./index'); | ||
require('dotenv').config(); | ||
getUserDownloads(({ USER = 'artginzburg', PERSONAL_ACCESS_TOKEN } = process.env)).then(console.log); | ||
const { USER = 'artginzburg', PERSONAL_ACCESS_TOKEN } = process.env; | ||
const timeLabel = 'github-user-downloads'; | ||
console.time(timeLabel); | ||
getUserDownloads(USER, PERSONAL_ACCESS_TOKEN).then((data) => { | ||
console.log(data); | ||
console.timeEnd(timeLabel); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3553
7
70
3