Skip to content

Commit 83c8a5c

Browse files
authored
Merge pull request #29 from samsmithnz/ConsoleOutputUpdates
Experimenting with better console logging
2 parents 3a300ab + dc147f7 commit 83c8a5c

File tree

6 files changed

+27
-62
lines changed

6 files changed

+27
-62
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// "forwardPorts": [],
1414

1515
// Use 'postCreateCommand' to run commands after the container is created.
16-
// "postCreateCommand": "src/deploymentfrequency.ps1 -ownerRepo 'SamSmithnz/SamsFeatureFlags' -workflows 'Feature Flags CI/CD,CodeQL' -branch 'Main' -numberOfDays 30 -patToken $env:PATTOKEN -showVerboseLogging $true",
16+
// "postCreateCommand": "src/deploymentfrequency.ps1 -ownerRepo 'SamSmithnz/SamsFeatureFlags' -workflows 'Feature Flags CI/CD,CodeQL' -branch 'Main' -numberOfDays 30 -patToken $env:PATTOKEN",
1717

1818

1919
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.

.github/workflows/workflow.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,19 @@ jobs:
5151
owner-repo: 'samsmithnz/DevOpsMetrics'
5252
default-branch: 'main'
5353
number-of-days: 30
54-
show-verbose-logging: 0
5554
- name: Test no workflow
5655
uses: ./
5756
with:
5857
workflows: ''
5958
owner-repo: 'samsmithnz/GitHubToAzureDevOps'
60-
show-verbose-logging: 0
6159

6260
#authenication tests
6361
- name: Test elite repo with PAT Token
6462
uses: ./
6563
with:
6664
workflows: 'Feature Flags CI/CD'
6765
owner-repo: 'samsmithnz/SamsFeatureFlags'
68-
pat-token: "${{ secrets.PATTOKEN }}"
69-
show-verbose-logging: 0
66+
pat-token: "${{ secrets.PATTOKEN }}"
7067
- name: Test elite repo, multiple workflows, with PAT Token
7168
uses: ./
7269
with:

GitVersion.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
next-version: 1.2.0
1+
next-version: 1.3.0

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ A GitHub Action to roughly calculate DORA deployment frequency. This is not mean
2424
- `app-id`: optional, string, defaults to '', application id of the registered GitHub app
2525
- `app-install-id`: optional, string, defaults to '', id of the installed instance of the GitHub app
2626
- `app-private-key`: optional, string, defaults to '', private key which has been generated for the installed instance of the GitHub app. Must be provided without leading `'-----BEGIN RSA PRIVATE KEY----- '` and trailing `' -----END RSA PRIVATE KEY-----'`.
27-
- `show-verbose-logging`: optional, bool, defaults to 0, if set to 1, will show more verbose logging information
2827

2928
To test the current repo (same as where the action runs)
3029
```

action.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ inputs:
3131
app-private-key:
3232
description: 'private key which has been generated for the installed instance of the GitHub app'
3333
default: ""
34-
show-verbose-logging:
35-
description: 'show more details of the calculation and processing'
36-
default: 0 # (0 == false)
3734
runs:
3835
using: "composite"
3936
steps:
4037
- name: Run DORA deployment frequency
4138
shell: pwsh
4239
run: |
43-
$result = ${{ github.action_path }}/src/deploymentfrequency.ps1 -ownerRepo "${{ inputs.owner-repo }}" -workflows "${{ inputs.workflows }}" -branch "${{ inputs.default-branch }}" -numberOfDays ${{ inputs.number-of-days }} -patToken "${{ inputs.pat-token }}" -actionsToken "${{ inputs.actions-token }}" -appId "${{ inputs.app-id }}" -appInstallationId "${{ inputs.app-install-id }}" -appPrivateKey "${{ inputs.app-private-key }}" -showVerboseLogging ${{ inputs.show-verbose-logging }}
40+
$result = ${{ github.action_path }}/src/deploymentfrequency.ps1 -ownerRepo "${{ inputs.owner-repo }}" -workflows "${{ inputs.workflows }}" -branch "${{ inputs.default-branch }}" -numberOfDays ${{ inputs.number-of-days }} -patToken "${{ inputs.pat-token }}" -actionsToken "${{ inputs.actions-token }}" -appId "${{ inputs.app-id }}" -appInstallationId "${{ inputs.app-install-id }}" -appPrivateKey "${{ inputs.app-private-key }}"
4441
Write-Host "::set-output name=result::$result"
4542
Write-Output $result >> $env:GITHUB_STEP_SUMMARY
4643

src/deploymentfrequency.ps1

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ Param(
88
[string] $actionsToken = "",
99
[string] $appId = "",
1010
[string] $appInstallationId = "",
11-
[string] $appPrivateKey = "",
12-
[bool] $showVerboseLogging = $false
11+
[string] $appPrivateKey = ""
1312
)
1413

1514
#The main function
@@ -21,8 +20,7 @@ function Main ([string] $ownerRepo,
2120
[string] $actionsToken = "",
2221
[string] $appId = "",
2322
[string] $appInstallationId = "",
24-
[string] $appPrivateKey = "",
25-
[bool] $showVerboseLogging = $false)
23+
[string] $appPrivateKey = "")
2624
{
2725

2826
#==========================================
@@ -31,19 +29,15 @@ function Main ([string] $ownerRepo,
3129
$owner = $ownerRepoArray[0]
3230
$repo = $ownerRepoArray[1]
3331
$workflowsArray = $workflows -split ','
34-
$numberOfDays = $numberOfDays
35-
#Write-Output "showVerboseLogging: $showVerboseLogging"
36-
if ($showVerboseLogging -eq $true)
37-
{
38-
Write-Output "Owner/Repo: $owner/$repo"
39-
Write-Output "Workflows: $workflows"
40-
Write-Output "Branch: $branch"
41-
Write-Output "Number of days: $numberOfDays"
42-
}
32+
$numberOfDays = $numberOfDays
33+
Write-Host "Owner/Repo: $owner/$repo"
34+
Write-Host "Workflows: $workflows"
35+
Write-Host "Branch: $branch"
36+
Write-Host "Number of days: $numberOfDays"
4337

4438
#==========================================
4539
# Get authorization headers
46-
$authHeader = GetAuthHeader $patToken $actionsToken $appId $appInstallationId $appPrivateKey $showVerboseLogging
40+
$authHeader = GetAuthHeader $patToken $actionsToken $appId $appInstallationId $appPrivateKey
4741

4842
#==========================================
4943
#Get workflow definitions from github
@@ -83,10 +77,6 @@ function Main ([string] $ownerRepo,
8377
$result = $workflowNames.Add($workflow.name)
8478
}
8579
}
86-
# else
87-
# {
88-
# Write-Output "'$($workflow.name)' DID NOT match with $arrayItem"
89-
# }
9080
}
9181
}
9282

@@ -114,7 +104,7 @@ function Main ([string] $ownerRepo,
114104
#Count workflows that are completed, on the target branch, and were created within the day range we are looking at
115105
if ($run.head_branch -eq $branch -and $run.created_at -gt (Get-Date).AddDays(-$numberOfDays))
116106
{
117-
#Write-Output "Adding item with status $($run.status), branch $($run.head_branch), created at $($run.created_at), compared to $((Get-Date).AddDays(-$numberOfDays))"
107+
#Write-Host "Adding item with status $($run.status), branch $($run.head_branch), created at $($run.created_at), compared to $((Get-Date).AddDays(-$numberOfDays))"
118108
$buildTotal++
119109
#get the workflow start and end time
120110
$dateList += New-Object PSObject -Property @{start_datetime=$run.created_at;end_datetime=$run.updated_at}
@@ -133,11 +123,10 @@ function Main ([string] $ownerRepo,
133123
$deploymentsPerDay = $dateList.Count / $numberOfDays
134124
}
135125
$deploymentsPerDayList += $deploymentsPerDay
136-
#Write-Output "Adding to list, workflow id $workflowId deployments per day of $deploymentsPerDay"
126+
#Write-Host "Adding to list, workflow id $workflowId deployments per day of $deploymentsPerDay"
137127
}
138128
}
139129

140-
#Write-Output "Total items in list is $($deploymentsPerDayList.Length)"
141130
$totalDeployments = 0
142131
Foreach ($deploymentItem in $deploymentsPerDayList){
143132
$totalDeployments += $deploymentItem
@@ -146,7 +135,7 @@ function Main ([string] $ownerRepo,
146135
{
147136
$deploymentsPerDay = $totalDeployments / $deploymentsPerDayList.Length
148137
}
149-
#Write-Output "Total deployments $totalDeployments with a final deployments value of $deploymentsPerDay"
138+
#Write-Host "Total deployments $totalDeployments with a final deployments value of $deploymentsPerDay"
150139

151140
#==========================================
152141
#Show current rate limit
@@ -159,10 +148,7 @@ function Main ([string] $ownerRepo,
159148
{
160149
$rateLimitResponse = Invoke-RestMethod -Uri $uri3 -ContentType application/json -Method Get -Headers @{Authorization=($authHeader["Authorization"])} -SkipHttpErrorCheck -StatusCodeVariable "HTTPStatus"
161150
}
162-
if ($showVerboseLogging -eq $true)
163-
{
164-
Write-Output "Rate limit consumption: $($rateLimitResponse.rate.used) / $($rateLimitResponse.rate.limit)"
165-
}
151+
Write-Host "Rate limit consumption: $($rateLimitResponse.rate.used) / $($rateLimitResponse.rate.limit)"
166152

167153
#==========================================
168154
#Calculate deployments per day
@@ -242,10 +228,7 @@ function Main ([string] $ownerRepo,
242228

243229
if ($dateList.Count -gt 0 -and $numberOfDays -gt 0)
244230
{
245-
if ($showVerboseLogging -eq $true)
246-
{
247-
Write-Output "Deployment frequency over last $numberOfDays days, is $displayMetric $displayUnit, with a DORA rating of '$rating'"
248-
}
231+
Write-Host "Deployment frequency over last $numberOfDays days, is $displayMetric $displayUnit, with a DORA rating of '$rating'"
249232
return Format-OutputMarkdown -workflowNames $workflowNames -displayMetric $displayMetric -displayUnit $displayUnit -repo $ownerRepo -branch $branch -numberOfDays $numberOfDays -numberOfUniqueDates $uniqueDates.Length.ToString() -color $color -rating $rating
250233
}
251234
else
@@ -257,7 +240,7 @@ function Main ([string] $ownerRepo,
257240
#Generate the authorization header for the PowerShell call to the GitHub API
258241
#warning: PowerShell has really wacky return semantics - all output is captured, and returned
259242
#reference: https://stackoverflow.com/questions/10286164/function-return-value-in-powershell
260-
function GetAuthHeader ([string] $patToken, [string] $actionsToken, [string] $appId, [string] $appInstallationId, [string] $appPrivateKey, [bool] $showVerboseLogging = $false)
243+
function GetAuthHeader ([string] $patToken, [string] $actionsToken, [string] $appId, [string] $appInstallationId, [string] $appPrivateKey)
261244
{
262245
#Clean the string - without this the PAT TOKEN doesn't process
263246
$patToken = $patToken.Trim()
@@ -267,36 +250,24 @@ function GetAuthHeader ([string] $patToken, [string] $actionsToken, [string] $ap
267250
#Write-Host "patToken is something: $(![string]::IsNullOrEmpty($patToken))"
268251
if (![string]::IsNullOrEmpty($patToken))
269252
{
270-
if ($showVerboseLogging -eq $true)
271-
{
272-
Write-Host "Authentication detected: PAT TOKEN"
273-
}
253+
Write-Host "Authentication detected: PAT TOKEN"
274254
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$patToken"))
275255
$authHeader = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
276256
}
277257
elseif (![string]::IsNullOrEmpty($actionsToken))
278258
{
279-
if ($showVerboseLogging -eq $true)
280-
{
281-
Write-Host "Authentication detected: GITHUB TOKEN"
282-
}
259+
Write-Host "Authentication detected: GITHUB TOKEN"
283260
$authHeader = @{Authorization=("Bearer {0}" -f $base64AuthInfo)}
284261
}
285262
elseif (![string]::IsNullOrEmpty($appId)) # GitHup App auth
286263
{
287-
if ($showVerboseLogging -eq $true)
288-
{
289-
Write-Host "Authentication detected: GITHUB APP TOKEN"
290-
}
264+
Write-Host "Authentication detected: GITHUB APP TOKEN"
291265
$token = Get-JwtToken $appId $appInstallationId $appPrivateKey
292266
$authHeader = @{Authorization=("token {0}" -f $token)}
293267
}
294268
else
295269
{
296-
if ($showVerboseLogging -eq $true)
297-
{
298-
Write-Host "No authentication detected"
299-
}
270+
Write-Host "No authentication detected"
300271
$base64AuthInfo = $null
301272
$authHeader = $null
302273
}
@@ -367,8 +338,8 @@ function Get-JwtToken([string] $appId, [string] $appInstallationId, [string] $ap
367338
function Format-OutputMarkdown([array] $workflowNames, [string] $rating, [string] $displayMetric, [string] $displayUnit, [string] $repo, [string] $branch, [string] $numberOfDays, [string] $numberOfUniqueDates, [string] $color)
368339
{
369340
$encodedDeploymentFrequency = [uri]::EscapeUriString($displayMetric + " " + $displayUnit)
370-
371-
$markdown = "![Deployment Frequency](https://img.shields.io/badge/frequency-" + $encodedDeploymentFrequency + "-" + $color + "?logo=github&label=Deployment%20frequency)`r`n" +
341+
#double newline to start the line helps with formatting in GitHub logs
342+
$markdown = "`n`n![Deployment Frequency](https://img.shields.io/badge/frequency-" + $encodedDeploymentFrequency + "-" + $color + "?logo=github&label=Deployment%20frequency)`n" +
372343
"**Definition:** For the primary application or service, how often is it successfully deployed to production.`n" +
373344
"**Results:** Deployment frequency is **$displayMetric $displayUnit** with a **$rating** rating, over the last **$numberOfDays days**.`n" +
374345
"**Details**:`n" +
@@ -381,10 +352,11 @@ function Format-OutputMarkdown([array] $workflowNames, [string] $rating, [string
381352

382353
function Format-NoOutputMarkdown([string] $workflows, [string] $numberOfDays)
383354
{
384-
$markdown = "![Deployment Frequency](https://img.shields.io/badge/frequency-none-lightgrey?logo=github&label=Deployment%20frequency)`r`n`n" +
355+
#double newline to start the line helps with formatting in GitHub logs
356+
$markdown = "`n`n![Deployment Frequency](https://img.shields.io/badge/frequency-none-lightgrey?logo=github&label=Deployment%20frequency)`n`n" +
385357
"No data to display for $ownerRepo for workflow(s) $workflows over the last $numberOfDays days`n`n" +
386358
"---"
387359
return $markdown
388360
}
389361

390-
main -ownerRepo $ownerRepo -workflows $workflows -branch $branch -numberOfDays $numberOfDays -patToken $patToken -actionsToken $actionsToken -appId $appId -appInstallationId $appInstallationId -appPrivateKey $appPrivateKey -showVerboseLogging $showVerboseLogging
362+
main -ownerRepo $ownerRepo -workflows $workflows -branch $branch -numberOfDays $numberOfDays -patToken $patToken -actionsToken $actionsToken -appId $appId -appInstallationId $appInstallationId -appPrivateKey $appPrivateKey

0 commit comments

Comments
 (0)