Skip to content
activity

GitHub Action

Sonatype Stats

v1.0.0 Latest version

Sonatype Stats

activity

Sonatype Stats

Generates index.html and JSONs with stats downloaded from Sonatype Central Statistics

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Sonatype Stats

uses: MateuszKubuszok/[email protected]

Learn more about this action in MateuszKubuszok/sonatype-stats

Choose a version

sonatype-stats

This is the fork of the sonatype-stats utility which has a goal of:

  • migrating the tool from Ammonite to Scala CLI
  • migrating from Scala 2.12 to Scala 3
  • migrating from Circe to Jsoniter Scala
  • migrating to newer version of STTP
  • migrating charts to Chart.js
  • migrating from Travis CI to GH Actions

What it does

Sonatype exposes projects stats only after you log in, and only for your projects (Central Statistics). Its UI also only shows last 12 months.

Through their API you can fetch data since the beginning of the project for every artifact. And this is what this script does:

  • it fetches unique IPs in every month since project start
  • it fetches both unique IPs and all downloads for every artifact since the project start
  • it creates chart containing all of that data (using Chart.js)
  • it saves both chart and values from Sonatype in data directory
  • it uses content of data directory as cache to not download the same data twice unnecessarily

You can then looks though the statistics, archive them or publish on GitHub pages.

Running locally

Install Scala CLI. Then run:

SONATYPE_PROJECT=[project name] SONATYPE_USERNAME=[username] SONATYPE_PASSWORD='password' scala-cli run .

putting the right values for your project and Sonatype user. The data will be generated in data directory.

Running in scheduled GitHub Action

We can create a GH Action which could publish Sonatype Stats on GitHub Pages in a dedicated repository:

  • create a repository dedicated for Sonatype statistics - they will be visible on its GitHub Pages site
  • obtain Sonatype credentials (instead of username you can create User Token)
    • setup SONATYPE_USERNAME secret
    • setup SONATYPE_PASSWORD secret
  • find which project ID you want to use (you can check what projects you see on e.g. a Central Statistics after you log in)
    • setup SONATYPE_PROJECT variable
  • create GitHub Access Token with an access to the repository (we'll be using Publish to GitHub Pages action)
    • in Fine-graned tokens you need to grant "Read access to metadata" and "Read and Write access to code and pages" just for the repository which will host stats on its GH Pages
  • finally, create in your repository file .github/workflows/sonatype-stats.yml with the content like below:
name: Publish Sonatype Stats to GitHub Pages

on:
  push:
    branches:
      # update index.html also if anything was modified in the workflow
      - master
  schedule:
    # * is a special character in YAML so you have to quote this string
    - cron:  '0 0 7,15 * *'

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v2

      - name: Cache fetched data
        uses: actions/cache@v4
        with:
          path: data
          key: setup-1

      - name: Fetch and render Sonatype Stats
        uses: MateuszKubuszok/[email protected]
        with:
          sonatype-project: ${{ vars.SONATYPE_PROJECT }}
          sonatype-username: ${{ secrets.SONATYPE_USERNAME }}
          sonatype-password: ${{ secrets.SONATYPE_PASSWORD }}

      - name: Publish generated content to GitHub Pages
        uses: tsunematsu21/[email protected]
        with:
          dir: data
          branch: gh-pages
          token: ${{ secrets.ACCESS_TOKEN }}

This will create a job that is running every 1st and 15th of the month - once a month ends Sonatype triggers a workflow that computes stats for it. Usually it takes several days before they are avaiable (often around a week), so there is no point in running this action very often. (Stats once downloaded are cached, so we only have to add missing ones).

Once it finishes running, don't forget to enable GitHub Pages for your repository.

See example for Scalaland.io.

You can tweak this setup as it only shows the most basic use case.