From 018d962ef42149561ed525817aad60c483a35e4a 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 6b5e1703096f..5b1b1efa1a35 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 449f040eec72618407dd7a3f26af2f136cee131b 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 5b1b1efa1a35..2ad63f6625fd 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 010a6e30c4ee7a7eeeef08573069ae2c3fa3847f 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 2ad63f6625fd..7edade434822 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 437b5f3b5fbc4e530293f34fee98e0ddeaba5137 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 7edade434822..4437a9ee4e55 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 6f02d2edc61f..0bc2e871c831 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 e5c39db591ef82de862ae1c66fc8e4bbf5790668 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 4437a9ee4e55..4c6cc0c0a036 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 ae8d1dab5b53dc8b00cd1443582d675934a11d4f 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 4c6cc0c0a036..73c3dc1b2b93 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 c1a11cc2f5361aa0453ca58f01d179084fd563dc 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 73c3dc1b2b93..7a37058178da 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 1ba0898818ee4093797abfbc51e47ea8e2e29876 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 7a37058178da..d8e03690dd58 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 87e5159b4923765297f70bb2858ac40645b83c91 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 d8e03690dd58..31f04d5b9abe 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 4b30fa2b5d07ea04653861cdc8def56b30a5ab0a 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 31f04d5b9abe..6b2e4347224c 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 93d4352cb2dd3c532dc11a3fea847fffca2cda8c 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 6b2e4347224c..f9e73e75405d 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 bd38a17e34141cdefa4caca03f58098fd39f5773 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 f9e73e75405d..4ee3f1b05dcd 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 }