Get GitHub Download Release Metrics

Cody Hosterman, Argenis Fernandez and I have posted a lot of content to GitHub and one of the details I always wanted to know was “# of downloads for project X”. Although GitHub provides some nice traffic and cloning graphs it still does not show the number of downloads for a release. So I took to the GitHub API and PowerShell to figure it out. The below script can be run with three parameters:

  1. $gitHubRepository
  2. $gitHubUserName
  3. $gitHubApiKey

As an example I have used the Pure Storage PowerShell SDK to show sample results. The URL for the PowerShell SDK is https://github.com/purestorage-openconnect/powershellsdk.

  1. $gitHubRepository = powershellsdk
  2. $gitHubUserName = purestorage-openconnect
    Note: Change this to your specific username to run against your own Public and Private repos.
  3. $gitHubApiKey — Log into your GitHub account and navigate to https://github.com/settings/tokens to generate a new API token or find an existing token.

 

PowerShell script:

$gitHubRepository = "powershellsdk"
$gitHubUsername = "purestorage-connect"

$gitHubApiKey = "<TOKEN>"

$releaseParams = @{}

$releaseParams = @{
    Uri = "https://api.github.com/repos/$gitHubUsername/$gitHubRepository/releases";
    Method = 'GET';
    Headers = @{
        Authorization = 'Basic ' + [Convert]::ToBase64String(
            [Text.Encoding]::ASCII.GetBytes($gitHubApiKey + ":x-oauth-basic")
        );
    }
    ContentType = 'application/json';
    Body = (ConvertTo-Json $releaseData -Compress)
}
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($($releaseParams.Uri))

$results = Invoke-RestMethod @releaseParams

$assets = $result.assets
ForEach($result in $results) {
    Write-Host "Name: $($result.name)"
    Write-Host "Release: $($result.tag_name)"
    Write-Host "Downloads: $($result.assets.download_count)"
    Write-Host "URL: $($result.assets_url)"
}

$ServicePoint.CloseConnectionGroup("") | Out-Null

Sample output:

Name: Pure Storage PowerShell SDK 1.5.5.0
Release: v1.5.5.0
Downloads: 0 0 11 4
URL: https://api.github.com/repos/PureStorage-Connect/PowerShellSDK/releases/3395328/assets
Name: Pure Storage PowerShell SDK 1.5.4.0
Release: v1.5.4.0
Downloads: 0 0 6 2
URL: https://api.github.com/repos/PureStorage-Connect/PowerShellSDK/releases/3124079/assets

 

Download the script from https://github.com/barkz/PurePowerShellGuy.

Cheers,
Barkz