From 4d03cc6c1a3a673367b08bd0f02cf924cbf7dddb Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Thu, 28 Apr 2022 08:41:45 -0700 Subject: [PATCH 01/12] Change the direction of the commit date --- eng/common/scripts/Delete-RemoteBranches.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 6b5e1703096..5b1b1efa1a3 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -60,7 +60,7 @@ foreach ($res in $responses) } try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken - if ($commitDate -and ($commitDate -gt $LastCommitOlderThan)) { + if ($commitDate -and ($commitDate -lt $LastCommitOlderThan)) { LogDebug "The branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." continue } From f7ad031d6b8affb29407b09c2c074af88c99b0ce Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Thu, 28 Apr 2022 09:01:35 -0700 Subject: [PATCH 02/12] Log on right place --- eng/common/scripts/Delete-RemoteBranches.ps1 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 5b1b1efa1a3..2ad63f6625f 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -60,22 +60,23 @@ foreach ($res in $responses) } try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken - if ($commitDate -and ($commitDate -lt $LastCommitOlderThan)) { + if ($commitDate -and ($commitDate -gt $LastCommitOlderThan)) { LogDebug "The branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." continue } + + LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. Last commit date $commitDate is older than $LastCommitOlderThan. Deleting..." } catch { LogError "Get-GithubReferenceCommitDate failed with exception:`n$_" exit 1 } } - LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. " - try { - Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken - } - catch { - LogError "Remove-GitHubSourceReferences failed with exception:`n$_" - exit 1 - } + # try { + # Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken + # } + # catch { + # LogError "Remove-GitHubSourceReferences failed with exception:`n$_" + # exit 1 + # } } From 8865aaa34fab8eab6ad623dc5f5948f2516bf359 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Thu, 28 Apr 2022 09:06:40 -0700 Subject: [PATCH 03/12] remove auth --- eng/common/scripts/Delete-RemoteBranches.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 2ad63f6625f..7edade43482 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -59,7 +59,7 @@ foreach ($res in $responses) continue } try { - $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken + $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url if ($commitDate -and ($commitDate -gt $LastCommitOlderThan)) { LogDebug "The branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." continue From fe1d94ebb0d4a384b3ce3f1284ca9b361197e85b Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Thu, 28 Apr 2022 09:16:01 -0700 Subject: [PATCH 04/12] fix typo --- eng/common/scripts/Delete-RemoteBranches.ps1 | 4 ++-- eng/common/scripts/Invoke-GitHubAPI.ps1 | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 7edade43482..4437a9ee4e5 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -60,8 +60,8 @@ foreach ($res in $responses) } try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url - if ($commitDate -and ($commitDate -gt $LastCommitOlderThan)) { - LogDebug "The branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." + if (!$commitDate -or ($commitDate -gt $LastCommitOlderThan)) { + LogDebug "No last commit date or the branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." continue } diff --git a/eng/common/scripts/Invoke-GitHubAPI.ps1 b/eng/common/scripts/Invoke-GitHubAPI.ps1 index 6f02d2edc61..0bc2e871c83 100644 --- a/eng/common/scripts/Invoke-GitHubAPI.ps1 +++ b/eng/common/scripts/Invoke-GitHubAPI.ps1 @@ -429,5 +429,5 @@ function Get-GithubReferenceCommitDate($commitUrl, $AuthToken) { LogDebug "No date returned from the commit sha. " return $null } - return $commitData.committer.date + return $commitResponse.committer.date } From 6e6789c9cff7e7a3eabb7d36a0ee42ad47632b43 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Thu, 28 Apr 2022 09:17:30 -0700 Subject: [PATCH 05/12] Add auth token back --- eng/common/scripts/Delete-RemoteBranches.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 4437a9ee4e5..4c6cc0c0a03 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -59,7 +59,7 @@ foreach ($res in $responses) continue } try { - $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url + $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken if (!$commitDate -or ($commitDate -gt $LastCommitOlderThan)) { LogDebug "No last commit date or the branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." continue From 40b165f68ad56897acf675b729250c49df90b066 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Thu, 28 Apr 2022 09:18:13 -0700 Subject: [PATCH 06/12] add delete back --- eng/common/scripts/Delete-RemoteBranches.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 4c6cc0c0a03..73c3dc1b2b9 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -72,11 +72,11 @@ foreach ($res in $responses) exit 1 } } - # try { - # Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken - # } - # catch { - # LogError "Remove-GitHubSourceReferences failed with exception:`n$_" - # exit 1 - # } + try { + Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken + } + catch { + LogError "Remove-GitHubSourceReferences failed with exception:`n$_" + exit 1 + } } From 9405b70c7694dc39c097c6ee7b008ece5d0bdb50 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Thu, 28 Apr 2022 09:28:45 -0700 Subject: [PATCH 07/12] Update eng/common/scripts/Delete-RemoteBranches.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/Delete-RemoteBranches.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 73c3dc1b2b9..7a37058178d 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -61,7 +61,7 @@ foreach ($res in $responses) try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken if (!$commitDate -or ($commitDate -gt $LastCommitOlderThan)) { - LogDebug "No last commit date or the branch $branch last commit date $commitDate is newer than the date $LastCommitOlderThan. Skipping." + LogDebug "No last commit date or the branch $branch last commit date [$commitDate] is newer than the date $LastCommitOlderThan. Skipping." continue } From 13408504b6b820914f94f7474ccbcd2e8993deda Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Thu, 28 Apr 2022 09:30:12 -0700 Subject: [PATCH 08/12] Update Delete-RemoteBranches.ps1 --- eng/common/scripts/Delete-RemoteBranches.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 7a37058178d..d8e03690dd5 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -53,6 +53,7 @@ foreach ($res in $responses) continue } + LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. " if ($LastCommitOlderThan) { if (!$res.object -or !$res.object.url) { LogWarning "No commit url returned from response. Skipping... " @@ -65,7 +66,7 @@ foreach ($res in $responses) continue } - LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has no associated open Pull Request. Last commit date $commitDate is older than $LastCommitOlderThan. Deleting..." + LogDebug "Last commit date $commitDate is older than $LastCommitOlderThan. Deleting..." } catch { LogError "Get-GithubReferenceCommitDate failed with exception:`n$_" From c4004c948f503bd295349e1cbdf7a8c44ed7896a Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Thu, 28 Apr 2022 09:31:11 -0700 Subject: [PATCH 09/12] Update Delete-RemoteBranches.ps1 --- eng/common/scripts/Delete-RemoteBranches.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index d8e03690dd5..31f04d5b9ab 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -66,7 +66,7 @@ foreach ($res in $responses) continue } - LogDebug "Last commit date $commitDate is older than $LastCommitOlderThan. Deleting..." + LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has a last commit date $commitDate that is older than $LastCommitOlderThan. Deleting branch..." } catch { LogError "Get-GithubReferenceCommitDate failed with exception:`n$_" From d1a3e81f479de4cc2b7c3946b8345a9c337f5d80 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Thu, 28 Apr 2022 09:33:05 -0700 Subject: [PATCH 10/12] Update Delete-RemoteBranches.ps1 --- eng/common/scripts/Delete-RemoteBranches.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 31f04d5b9ab..6b2e4347224 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -66,7 +66,7 @@ foreach ($res in $responses) continue } - LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has a last commit date $commitDate that is older than $LastCommitOlderThan. Deleting branch..." + LogDebug "Branch [ $branch ] in repo [ $RepoId ] has a last commit date $commitDate that is older than $LastCommitOlderThan. " } catch { LogError "Get-GithubReferenceCommitDate failed with exception:`n$_" @@ -75,6 +75,7 @@ foreach ($res in $responses) } try { Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken + LogDebug "The branch $branch in $RepoId has been deleted." } catch { LogError "Remove-GitHubSourceReferences failed with exception:`n$_" From 54a6ae0d25cd8ea438c72c544e3f094194321aa0 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Thu, 28 Apr 2022 09:36:24 -0700 Subject: [PATCH 11/12] Update Delete-RemoteBranches.ps1 --- eng/common/scripts/Delete-RemoteBranches.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index 6b2e4347224..f9e73e75405 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -33,8 +33,8 @@ foreach ($res in $responses) continue } $branch = $res.ref + $branchName = $branch.Replace("refs/heads/","") try { - $branchName = $branch.Replace("refs/heads/","") $head = "${RepoId}:${branchName}" LogDebug "Operating on branch [ $branchName ]" $pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken @@ -62,11 +62,11 @@ foreach ($res in $responses) try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken if (!$commitDate -or ($commitDate -gt $LastCommitOlderThan)) { - LogDebug "No last commit date or the branch $branch last commit date [$commitDate] is newer than the date $LastCommitOlderThan. Skipping." + LogDebug "No last commit date or the branch $branch last commit date [ $commitDate ] is newer than the date $LastCommitOlderThan. Skipping." continue } - LogDebug "Branch [ $branch ] in repo [ $RepoId ] has a last commit date $commitDate that is older than $LastCommitOlderThan. " + LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has a last commit date [ $commitDate ] that is older than $LastCommitOlderThan. " } catch { LogError "Get-GithubReferenceCommitDate failed with exception:`n$_" @@ -75,7 +75,7 @@ foreach ($res in $responses) } try { Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken - LogDebug "The branch $branch in $RepoId has been deleted." + LogDebug "The branch [ $branchName ] in [ $RepoId ] has been deleted." } catch { LogError "Remove-GitHubSourceReferences failed with exception:`n$_" From 03c4132deba2c068a54ec2230904a81faec2db2a Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Thu, 28 Apr 2022 11:14:54 -0700 Subject: [PATCH 12/12] Update Delete-RemoteBranches.ps1 --- eng/common/scripts/Delete-RemoteBranches.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/eng/common/scripts/Delete-RemoteBranches.ps1 b/eng/common/scripts/Delete-RemoteBranches.ps1 index f9e73e75405..4ee3f1b05dc 100644 --- a/eng/common/scripts/Delete-RemoteBranches.ps1 +++ b/eng/common/scripts/Delete-RemoteBranches.ps1 @@ -61,8 +61,13 @@ foreach ($res in $responses) } try { $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken - if (!$commitDate -or ($commitDate -gt $LastCommitOlderThan)) { - LogDebug "No last commit date or the branch $branch last commit date [ $commitDate ] is newer than the date $LastCommitOlderThan. Skipping." + if (!$commitDate) + { + LogDebug "No last commit date found. Skipping." + continue + } + if ($commitDate -gt $LastCommitOlderThan) { + LogDebug "The branch $branch last commit date [ $commitDate ] is newer than the date $LastCommitOlderThan. Skipping." continue }