From 0a7fce2f50d6d468358089bbcd7609145e01a9c1 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Thu, 7 Nov 2024 13:42:26 -0800 Subject: [PATCH] Add step for emitting rate limits for a token --- .../steps/emit-ratelimit-metrics.yml | 25 +++++++++++++++++++ .../templates/steps/git-push-changes.yml | 5 ++++ 2 files changed, 30 insertions(+) create mode 100644 eng/common/pipelines/templates/steps/emit-ratelimit-metrics.yml diff --git a/eng/common/pipelines/templates/steps/emit-ratelimit-metrics.yml b/eng/common/pipelines/templates/steps/emit-ratelimit-metrics.yml new file mode 100644 index 0000000000..44274a981d --- /dev/null +++ b/eng/common/pipelines/templates/steps/emit-ratelimit-metrics.yml @@ -0,0 +1,25 @@ +parameters: + - name: User + type: string + - name: GitHubToken + type: string + +steps: +- pwsh: | + $headers = @{ + "Authorization" = "Bearer $env:GITHUB_TOKEN" + "X-GitHub-Api-Version" = "2022-11-28" + } + + $response = Invoke-RestMethod -Uri 'https://api.github.com/rate_limit' -Headers $headers -Method Get + $timestamp = Get-Date + foreach ($property in $response.resources.PSObject.Properties.Value) + { + Write-Host "logmetric: $( @{ name= "github_rate_limit_remaining_total"; value= $property.remaining; timestamp= $timestamp; labels= @{ user=$env:GITHUB_USER; resource=$property.resource }} | ConvertTo-Json -Compress)" + Write-Host "logmetric: $( @{ name= "github_rate_limit_limit_total"; value= $property.limit; timestamp= $timestamp; labels= @{ user=$env:GITHUB_USER; resource=$property.resource }} | ConvertTo-Json -Compress)" + Write-Host "logmetric: $( @{ name= "github_rate_limit_used_total"; value= $property.used; timestamp= $timestamp; labels= @{ user=$env:GITHUB_USER; resource=$property.resource }} | ConvertTo-Json -Compress)" + } + displayName: Check GitHub Rate Limit + env: + GITHUB_TOKEN: ${{ parameters.GitHubToken}} + GITHUB_USER: ${{ parameters.User}} \ No newline at end of file diff --git a/eng/common/pipelines/templates/steps/git-push-changes.yml b/eng/common/pipelines/templates/steps/git-push-changes.yml index c8fbeaa976..da2a101764 100644 --- a/eng/common/pipelines/templates/steps/git-push-changes.yml +++ b/eng/common/pipelines/templates/steps/git-push-changes.yml @@ -28,6 +28,11 @@ steps: condition: succeeded() workingDirectory: ${{ parameters.WorkingDirectory }} +- template: ./emit-ratelimit-metrics.yml + parameters: + User: 'azure-sdk' + GitHubToken: $(azuresdk-github-pat) + - task: PowerShell@2 displayName: Push changes condition: and(succeeded(), eq(variables['HasChanges'], 'true'))