Skip to content

Commit

Permalink
👷 CI/CD - Make commit history update optional (#1365)
Browse files Browse the repository at this point in the history
* CI/CD - Allow skipping history

* Improve naming

* Make it optional

* Fix steps skipped

* Make the same change to staging

* Still update the hash, so the big commits are skipped in the future

* Add error handling for requests

* Add continuation backtick `
  • Loading branch information
bradystroud authored Jun 10, 2024
1 parent ed59d48 commit ce4d079
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
81 changes: 51 additions & 30 deletions .azure/scripts/update-rule-history.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ param (
[string]$GetHistorySyncCommitHashKey,
[string]$UpdateRuleHistoryKey,
[string]$UpdateHistorySyncCommitHashKey,
[string]$endCommitHash = "HEAD"
[string]$endCommitHash = "HEAD",
[bool]$SkipGenerateHistory = $false
# Do this if your PR is giant
# https://github.com/SSWConsulting/SSW.Rules/issues/1367
)

$ErrorActionPreference = 'Stop'
Expand All @@ -16,7 +19,12 @@ git commit-graph write --reachable --changed-paths
#Step 1: GetHistorySyncCommitHash - Retrieve CommitHash from AzureFunction
$Uri = $AzFunctionBaseUrl + 'GetHistorySyncCommitHash'
$Headers = @{'x-functions-key' = $GetHistorySyncCommitHashKey}
$Response = Invoke-WebRequest -URI $Uri -Headers $Headers
try {
$Response = Invoke-WebRequest -URI $Uri -Headers $Headers
} catch {
Write-Error "Failed to get response Azure Function $Uri: $_"
exit 1
}
$startCommitHash = $Response.Content -replace '"', ''

$filesProcessed = @{}
Expand All @@ -38,38 +46,45 @@ $historyArray | Foreach-Object {
$commitSyncHash = $userDetails[1]
}

$lastUpdated = $userDetails[2]
$lastUpdatedBy = $userDetails[3]
$lastUpdatedByEmail = $userDetails[4]

$fileArray | Where-Object {$_ -Match "^*.md" } | Foreach-Object {
if(!$filesProcessed.ContainsKey($_))
{
$createdRecord = git log --diff-filter=A --reverse --pretty="%ad<LINE>%aN<LINE>%ae" --date=iso-strict -- $_
$createdDetails = $createdRecord -split "<LINE>"
if (!$SkipGenerateHistory) {
$lastUpdated = $userDetails[2]
$lastUpdatedBy = $userDetails[3]
$lastUpdatedByEmail = $userDetails[4]

$fileArray | Where-Object {$_ -Match "^*.md" } | Foreach-Object {
if(!$filesProcessed.ContainsKey($_))
{
$createdRecord = git log --diff-filter=A --reverse --pretty="%ad<LINE>%aN<LINE>%ae" --date=iso-strict -- $_
$createdDetails = $createdRecord -split "<LINE>"

$filesProcessed.Add($_, 0)
$historyFileArray += @{
file = $($_)
lastUpdated = $lastUpdated
lastUpdatedBy = $lastUpdatedBy
lastUpdatedByEmail = $lastUpdatedByEmail
created = $createdDetails[0]
createdBy = $createdDetails[1]
createdByEmail = $createdDetails[2]
}
$filesProcessed.Add($_, 0)
$historyFileArray += @{
file = $($_)
lastUpdated = $lastUpdated
lastUpdatedBy = $lastUpdatedBy
lastUpdatedByEmail = $lastUpdatedByEmail
created = $createdDetails[0]
createdBy = $createdDetails[1]
createdByEmail = $createdDetails[2]
}

echo $_
echo $_
}
}
}
}

$historyFileContents = ConvertTo-Json $historyFileArray

#Step 3: UpdateRuleHistory - Send History Patch to AzureFunction
$Uri = $AzFunctionBaseUrl + 'UpdateRuleHistory'
$Headers = @{'x-functions-key' = $UpdateRuleHistoryKey}
$Response = Invoke-WebRequest -Uri $Uri -Method Post -Body $historyFileContents -Headers $Headers -ContentType 'application/json; charset=utf-8'
if (!$SkipGenerateHistory) {
#Step 3: UpdateRuleHistory - Send History Patch to AzureFunction
$historyFileContents = ConvertTo-Json $historyFileArray
$Uri = $AzFunctionBaseUrl + 'UpdateRuleHistory'
$Headers = @{'x-functions-key' = $UpdateRuleHistoryKey}
try {
$Response = Invoke-WebRequest -Uri $Uri -Method Post -Body $historyFileContents -Headers $Headers -ContentType 'application/json; charset=utf-8'
} catch {
Write-Error "Failed to update rule history $Uri: $_"
}
}

if(![string]::IsNullOrWhiteSpace($commitSyncHash))
{
Expand All @@ -79,8 +94,14 @@ if(![string]::IsNullOrWhiteSpace($commitSyncHash))
$Body = @{
commitHash = $commitSyncHash
}
$Result = Invoke-WebRequest -Uri $Uri -Method Post -Body $Body -Headers $Headers
try {
$Result = Invoke-WebRequest -Uri $Uri -Method Post -Body $JsonBody -Headers $Headers
} catch {
Write-Error "Failed to update history sync commit hash at $Uri: $_"
}
}

echo $historyFileContents
if (!$SkipGenerateHistory) {
echo $historyFileContents
}
echo $commitSyncHash
9 changes: 9 additions & 0 deletions .github/workflows/build-deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ on:
branches:
- release/*
workflow_dispatch:
inputs:
should_generate_commit_data:
description: 'Generate commit data'
default: 'yes'
type: choice
options:
- yes
- no

concurrency:
group: production
Expand All @@ -17,6 +25,7 @@ jobs:
with:
branch_name: ${{ github.ref }}
environment: production
should_generate_commit_data: ${{ github.event.inputs.should_generate_commit_data }}
secrets: inherit

deploy:
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/build-deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ on:
branches:
- main
workflow_dispatch:
inputs:
should_generate_commit_data:
description: 'Generate commit data'
default: 'yes'
type: choice
options:
- yes
- no

concurrency:
group: staging
Expand All @@ -17,6 +25,7 @@ jobs:
with:
branch_name: ${{ github.ref }}
environment: staging
should_generate_commit_data: ${{ github.event.inputs.should_generate_commit_data }}
secrets: inherit

deploy:
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/template-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ on:
type: string
required: true
description: 'The environment to build for'
should_generate_commit_data:
type: string
description: 'Generate commit data for rules history. You might want to skip this on large rules changes.'
required: false
default: 'yes'

jobs:
get-content-commit-data:
Expand Down Expand Up @@ -49,9 +54,11 @@ jobs:
-AzFunctionBaseUrl ${{ vars.AzFunctionBaseUrl }} `
-GetHistorySyncCommitHashKey ${{ secrets.GETHISTORYSYNCCOMMITHASHFUNCTIONKEY }} `
-UpdateRuleHistoryKey ${{ secrets.UPDATERULEHISTORYFUNCTIONKEY }} `
-UpdateHistorySyncCommitHashKey ${{ secrets.UPDATEHISTORYSYNCCOMMITHASHFUNCTIONKEY }}
-UpdateHistorySyncCommitHashKey ${{ secrets.UPDATEHISTORYSYNCCOMMITHASHFUNCTIONKEY }} `
-SkipGenerateHistory ${{ inputs.should_generate_commit_data == 'yes' }}
- name: Update Latest Rules
if: ${{ inputs.should_generate_commit_data == 'yes' }}
shell: pwsh
run: |
./SSW.Rules/build-scripts/update-latest-rules.ps1 `
Expand All @@ -68,6 +75,7 @@ jobs:
-outputFileName ${{ github.workspace }}/static/history.json
- name: Get History Commits of Each Contributor
if: ${{ inputs.should_generate_commit_data == 'yes' }}
shell: pwsh
run: |
./SSW.Rules/build-scripts/create-commits-history.ps1 `
Expand Down

0 comments on commit ce4d079

Please sign in to comment.