From ce4d079c5e7eb40ce9b3914126e422869e68cbfa Mon Sep 17 00:00:00 2001 From: "Brady Stroud [SSW]" Date: Mon, 10 Jun 2024 15:44:43 +0930 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20CI/CD=20-=20Make=20commit=20hist?= =?UTF-8?q?ory=20update=20=20optional=20(#1365)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 ` --- .azure/scripts/update-rule-history.ps1 | 81 ++++++++++++------- .github/workflows/build-deploy-production.yml | 9 +++ .github/workflows/build-deploy-staging.yml | 9 +++ .github/workflows/template-build.yml | 10 ++- 4 files changed, 78 insertions(+), 31 deletions(-) diff --git a/.azure/scripts/update-rule-history.ps1 b/.azure/scripts/update-rule-history.ps1 index 2e84937f1..5c3b3beb5 100644 --- a/.azure/scripts/update-rule-history.ps1 +++ b/.azure/scripts/update-rule-history.ps1 @@ -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' @@ -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 = @{} @@ -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%aN%ae" --date=iso-strict -- $_ - $createdDetails = $createdRecord -split "" + 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%aN%ae" --date=iso-strict -- $_ + $createdDetails = $createdRecord -split "" - $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)) { @@ -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 diff --git a/.github/workflows/build-deploy-production.yml b/.github/workflows/build-deploy-production.yml index bc3c4aefc..3a4142a5c 100644 --- a/.github/workflows/build-deploy-production.yml +++ b/.github/workflows/build-deploy-production.yml @@ -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 @@ -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: diff --git a/.github/workflows/build-deploy-staging.yml b/.github/workflows/build-deploy-staging.yml index da8d0371d..8db1399c1 100644 --- a/.github/workflows/build-deploy-staging.yml +++ b/.github/workflows/build-deploy-staging.yml @@ -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 @@ -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: diff --git a/.github/workflows/template-build.yml b/.github/workflows/template-build.yml index 065e245bb..746022dfc 100644 --- a/.github/workflows/template-build.yml +++ b/.github/workflows/template-build.yml @@ -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: @@ -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 ` @@ -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 `