Skip to content

Commit

Permalink
🐛 Fix optional history bug + add more logs (#1375)
Browse files Browse the repository at this point in the history
* add log

* Add more logging

* Move changes to correct files

* fixes

* Add more logs to PowerShell scripts
  • Loading branch information
bradystroud authored Jun 11, 2024
1 parent af59ae3 commit ed42366
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 30 deletions.
10 changes: 9 additions & 1 deletion .azure/scripts/update-rule-history.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ param (
[string]$UpdateRuleHistoryKey,
[string]$UpdateHistorySyncCommitHashKey,
[string]$endCommitHash = "HEAD",
[bool]$SkipGenerateHistory = $false
[bool]$SkipGenerateHistory = $false
# Do this if your PR is giant
# https://github.com/SSWConsulting/SSW.Rules/issues/1367
)

if ($SkipGenerateHistory) {
echo "Skipping history generation"
} else {
echo "Generating history"
echo $SkipGenerateHistory
echo $SkipGenerateHistory == $true
}

$ErrorActionPreference = 'Stop'

cd SSW.Rules.Content/
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/template-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ jobs:
- name: Get Rule History Commits
shell: pwsh
run: |
echo ${{ inputs.should_generate_commit_data }}
./SSW.Rules/build-scripts/update-rule-history.ps1 `
-AzFunctionBaseUrl ${{ vars.AzFunctionBaseUrl }} `
-GetHistorySyncCommitHashKey ${{ secrets.GETHISTORYSYNCCOMMITHASHFUNCTIONKEY }} `
-UpdateRuleHistoryKey ${{ secrets.UPDATERULEHISTORYFUNCTIONKEY }} `
-UpdateHistorySyncCommitHashKey ${{ secrets.UPDATEHISTORYSYNCCOMMITHASHFUNCTIONKEY }} `
-SkipGenerateHistory ${{ inputs.should_generate_commit_data == 'no' }}
-SkipGenerateHistoryString ${{ inputs.should_generate_commit_data == 'false' }}
- name: Update Latest Rules
shell: pwsh
Expand Down
15 changes: 15 additions & 0 deletions build-scripts/create-commits-history.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ function Get-FileMetadata($currentFolder, $commits) {
foreach ($commitPath in $commits) {
$updatedFilesChanged = @()

$sha = $commit.sha
$filesChangedCount = git whatchanged --oneline --name-status $sha -1 | tail -n +2 | wc -l

if ($filesChangedCount -gt 100) { # Skip commits with more than 100 files changed - these ruin data
echo "Skipping commit with more than 100 files changed: $sha"
continue
}

$fullPath = Join-Path $currentFolder $commitPath.Replace("rules/", "")

try {
Expand Down Expand Up @@ -116,6 +124,13 @@ foreach ($author in $authors) {

foreach ($commit in $commits) {
$sha = $commit.sha
$filesChangedCount = git whatchanged --oneline --name-status $sha -1 | tail -n +2 | wc -l

if ($filesChangedCount -gt 100) { # Skip commits with more than 100 files changed - these ruin data
echo "Skipping commit with more than 100 files changed: $sha"
continue
}

$filesChangedList = Get-CommitDiffFiles $sha
$commitDetails = Get-CommitInfo $sha

Expand Down
82 changes: 54 additions & 28 deletions build-scripts/update-rule-history.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@ param (
[string]$GetHistorySyncCommitHashKey,
[string]$UpdateRuleHistoryKey,
[string]$UpdateHistorySyncCommitHashKey,
[string]$endCommitHash = "HEAD"
[string]$endCommitHash = "HEAD",
[string]$SkipGenerateHistoryString = "false"
# Do this if your PR is giant
# https://github.com/SSWConsulting/SSW.Rules/issues/1367
)


if ($SkipGenerateHistoryString -eq "true") {
$SkipGenerateHistory = $true
} else {
$SkipGenerateHistory = $false
}

if ($SkipGenerateHistory) {
echo "Skipping history generation"
} else {
echo "Generating history"
echo $SkipGenerateHistory
echo $SkipGenerateHistory == $true
}


$ErrorActionPreference = 'Stop'

cd SSW.Rules.Content/
Expand Down Expand Up @@ -38,38 +57,42 @@ $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>"

$filesProcessed.Add($_, 0)
$historyFileArray += @{
file = $($_)
lastUpdated = $lastUpdated
lastUpdatedBy = $lastUpdatedBy
lastUpdatedByEmail = $lastUpdatedByEmail
created = $createdDetails[0]
createdBy = $createdDetails[1]
createdByEmail = $createdDetails[2]
}
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]
}

echo $_
echo $_
}
}
}
}

$historyFileContents = ConvertTo-Json $historyFileArray

#Step 3: UpdateRuleHistory - Send History Patch to AzureFunction
$Uri = $AzFunctionBaseUrl + '/api/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 + '/api/UpdateRuleHistory'
$Headers = @{'x-functions-key' = $UpdateRuleHistoryKey}
$Response = Invoke-WebRequest -Uri $Uri -Method Post -Body $historyFileContents -Headers $Headers -ContentType 'application/json; charset=utf-8'
}

if(![string]::IsNullOrWhiteSpace($commitSyncHash))
{
Expand All @@ -82,5 +105,8 @@ if(![string]::IsNullOrWhiteSpace($commitSyncHash))
$Result = Invoke-WebRequest -Uri $Uri -Method Post -Body $Body -Headers $Headers
}

echo $historyFileContents
if (!$SkipGenerateHistory) {
echo $historyFileContents
}

echo $commitSyncHash

0 comments on commit ed42366

Please sign in to comment.