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

Allow easily copying markdown of OSS author badges #356

Merged
merged 1 commit into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ issues_template: |

oss_template: |
<p class="mt-6">You contributed to the following repositories, with <strong>{{ format downloads }}</strong> combined daily downloads:</p>
<p>
<img src="https://img.shields.io/endpoint?label=popular%20packages&style=social&logo=nuget&url=https%3A%2F%2Fsponsorlink.devlooped.com%2Fnuget%2Fall?{{account}}" />
<img src="https://img.shields.io/endpoint?label=Daily%20downloads&style=social&logo=nuget&url=https%3A%2F%2Fsponsorlink.devlooped.com%2Fnuget%2Fdl?{{account}}" />
<a href="#" onclick="copyMarkdown(event)"><img src="https://img.shields.io/badge/copy-md?logo=Markdown&color=%23000000"></a>
</p>
<table class="borderless" style="border-collapse: collapse; padding: 4px; min-width: unset;">
<tr>
<th class="borderless">Repository</th>
Expand Down
18 changes: 17 additions & 1 deletion docs/assets/js/oss.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
layout: null
---
Handlebars.registerHelper('format', function(number) {
return new Intl.NumberFormat().format(number);
});
Expand Down Expand Up @@ -75,7 +78,7 @@ async function lookupAccount() {
}, 0);
}, 0);

document.getElementById('data').innerHTML = template({ repositories: repositories, downloads: totalDownloads });
document.getElementById('data').innerHTML = template({ account: account, icon: "{{ '/assets/img/copy.svg' | relative_url }}", repositories: repositories, downloads: totalDownloads });
document.getElementById('unsupported').style.display = 'none';
document.getElementById('supported').style.display = '';

Expand All @@ -101,4 +104,17 @@ function setError(message) {

function setBusy(busy) {
document.getElementById('spinner').style.display = busy ? '' : 'none';
}

function copyMarkdown() {
const url = new URL(window.location);
const account = url.searchParams.get('a');
if (account)
{
const markdown = `
![Popular packages](https://img.shields.io/endpoint?label=popular%20packages&style=social&logo=nuget&url=https%3A%2F%2Fsponsorlink.devlooped.com%2Fnuget%2Fall?${account})
![Daily downloads](https://img.shields.io/endpoint?label=Daily%20downloads&style=social&logo=nuget&url=https%3A%2F%2Fsponsorlink.devlooped.com%2Fnuget%2Fdl?${account})
`;
navigator.clipboard.writeText(markdown);
}
}
45 changes: 37 additions & 8 deletions src/Web/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@ public async Task<HttpResponseData> NuGetCountAsync([HttpTrigger(AuthorizationLe
{
var stats = await oss;
var manifest = await manager.GetManifestAsync();
var owner = req.Query.Count == 1 ? req.Query.ToString() : manifest.Sponsorable;
var count = 0;

var count = stats.Packages
.Where(x => x.Key.StartsWith(owner + "/"))
.Sum(x => x.Value.Count);
if (req.Query.Count == 1)
{
// Sum all packages across all repositories contributed to by the author in the querystring
if (req.Query.ToString() is { } author &&
stats.Authors.TryGetValue(author, out var repositories))
{
count = stats.Packages
.Where(x => repositories.Contains(x.Key))
.SelectMany(x => x.Value.Keys)
.Count();
}
}
else
{
count = stats.Packages
.Where(x => x.Key.StartsWith(manifest.Sponsorable + "/"))
.Sum(x => x.Value.Count);
}

var output = req.CreateResponse(HttpStatusCode.OK);

Expand All @@ -39,11 +54,25 @@ public async Task<HttpResponseData> NuGetDownloadsAsync([HttpTrigger(Authorizati
{
var stats = await oss;
var manifest = await manager.GetManifestAsync();
var owner = req.Query.Count == 1 ? req.Query.ToString() : manifest.Sponsorable;
var count = 0l;

var count = stats.Packages
.Where(x => x.Key.StartsWith(owner + "/"))
.Sum(x => x.Value.Sum(y => y.Value));
if (req.Query.Count == 1)
{
// Sum all packages across all repositories contributed to by the author in the querystring
if (req.Query.ToString() is { } author &&
stats.Authors.TryGetValue(author, out var repositories))
{
count = stats.Packages
.Where(x => repositories.Contains(x.Key))
.Sum(x => x.Value.Sum(x => x.Value));
}
}
else
{
count = stats.Packages
.Where(x => x.Key.StartsWith(manifest.Sponsorable + "/"))
.Sum(x => x.Value.Sum(y => y.Value));
}

var output = req.CreateResponse(HttpStatusCode.OK);

Expand Down