Shields: Badge Request: Add badge for total stars

Created on 3 Sep 2020  路  15Comments  路  Source: badges/shields

:clipboard: Description

  • Which service is this badge for: Github
  • What sort of information should this badge show: Total stars and watchers of a user's all repos

    • Provide an example in plain text:




Currently total stars badge is available for a specific repo only.

A badge for total stars count for all public repos of a specific user can be added to a users profile page.
Same can be done for total watchers as well.

For example if a user has 2 repos.
Repo1: stars: 2 watch: 20
Repo2: stars: 4: watch: 40

Then a badge should show Total Stars: 6 (2 + 4) and Total Watch: 60 (20 + 40)

:link: Data
Where can we get the data from?

  • Is there a public API: Yes Github Rest API or GraphQL api
  • Does the API requires an API key? No
  • Link to the API documentation. NA

Since a user may have lots of repository, it must be paginated. A max of 100 repos can be fetched in one go.
A graphql query to fetch repos of a user

  • First 100 Repos
{
  user(login: "mtlynch") {
    repositories(first: 100, ownerAffiliations: OWNER, isFork: false) {
      totalCount
      edges {
        cursor
        node {
          name
          stargazers {
            totalCount
          }
          watchers {
            totalCount
          }
        }
      }
    }
  }
}
  • Next 100 Repos
{
  user(login: "mtlynch") {
    repositories(first: 100, after: "Y3Vyc29yOnYyOpHOClW-bw==", ownerAffiliations: OWNER, isFork: false) {
      totalCount
      edges {
        cursor
        node {
          name
          stargazers {
            totalCount
          }
          watchers {
            totalCount
          }
        }
      }
    }
  }
}

Please Note: cursor is used for pagination to fetch next set of records, for more info see here

An alternative REST APIs is also available.

:microphone: Motivation

  • What is the specific use case?
    Github has a new feature where if you create a repo with your account name, it's readme will be visible at your profile page. These badges can be added there.
    Screenshot of my profile below
    Profile ScreenShot
good first issue service-badge

Most helpful comment

Using the graphQL API, could we:

  • filter to only repos with >0 stars (so we don't waste our quota to request repos with 0 stars which we don't need to count)
  • order by stars (from high to low)
    I think if we can do that I would be happy to say we only count your first 100 (or maybe even 200) most-starred repos to keep the number of requests reasonable.

All 15 comments

If it looks fine, then I can open PR for the same.

Hello @hemantsonu20,

Thanks for the suggestion, this sounds like a good idea to me. However, do you have a feel for the performance of this badge for a user with many public repositories?

@PyvesB Yes there will be a performance hit for users having multiple hundreds repository. But most users have less than 100 repo ( (although I don't have any data to prove it)) and It will be fetched in a single call.
I proposed graphql api where less data will travel over network and that's only optimization we can do from our side.

Is there any way, perhaps with the v3 API, to issue all the paginated calls in parallel? While I'd agree most users don't have thousands of repos, we're really talking about an owner here which could be an Organization (https://github.com/google/) or a user that does happen to have a higher count of repositories (https://github.com/isaacs/ https://github.com/sindresorhus/) and we wouldn't want to have to issue 4+ sequential API calls

@calebcartwright It could be possible if we fetch repo count first.
/users/{username} api returns public_repos count in given user / org account. Then we can fire additional call in parallel for each hundred repos varying page number. Say if account has 350 repos, Issue four parallel call with page 1 to page 4.

Using the graphQL API, could we:

  • filter to only repos with >0 stars (so we don't waste our quota to request repos with 0 stars which we don't need to count)
  • order by stars (from high to low)
    I think if we can do that I would be happy to say we only count your first 100 (or maybe even 200) most-starred repos to keep the number of requests reasonable.
  • There is no option to filter based on no of stars.
  • Sort by no of stars on repo is available.

Being able to sort by stars is probably the more useful of those two anyway.

Are you interested in working on a PR for this? There's a tutorial at https://github.com/badges/shields/blob/master/doc/TUTORIAL.md and you can look at other services which implement GithubAuthV4Service to get an idea of how to implement a badge that uses the github graphql api.

Yes, will be happy to work on it.
Will it be feasible to add a query param repoCount (default value 100), which can be further customized. Initially its values can be limited till 200 or any other value you suggest.

Opened pull request #5507. Applied a limit of 200 most starred repos.

We forgot to discuss about Total Watch. The problem is there is no option to sort based on watchers of repo.

I did a google search on how to use shield.io to display total count of stars and I stumbled upon this open issue.
I'm glad that this issue already addresses the problem I was trying to solve.

@hemantsonu20 please I would love to know if what you suggested has been implemented?

We forgot to discuss about Total Watch. The problem is there is no option to sort based on watchers of repo.

I think it'd be best to go ahead close this given the completion of #5507 and that a similar type of badge for watchers would require separate discussion and implementation. If someone wants to push for a total watchers badge then that could be moved to a new issue.

@emmysteven - the best way to view the available badges we offer is by going to Shields.io and searching for a specific badge or service (via the text box at the very top of the landing page) and/or via browsing the categories and listings of badges.

The aforementioned badges for total stars for a user/organization was indeed implemented via the PR linked to and discussed in the preceding comments, and can also be found on the site:

image

Hello @calebcartwright,

Thank you for point me to the right direction.
Cheers!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stclairdaniel picture stclairdaniel  路  3Comments

rominf picture rominf  路  3Comments

Fazendaaa picture Fazendaaa  路  3Comments

paulmelnikow picture paulmelnikow  路  3Comments

AlexWayfer picture AlexWayfer  路  3Comments