From 8c329360890fb9fbd31be9da8745bb39c2c2b363 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 fe6a7754104d6098617a265ec0b61a2ebdb57187 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 fe0ef6cb12cc97f79def0aca28a85e91476ef358 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 a9989d21cf912e2aeb06e430222c188c9e5dcb14 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 d6dd1ea7250624ff26c7171f3c1262c6826fb5f2 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 b2cabe1cae6cd8db728f93eaf7f6d7ca6b4e59c6 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 b25f2a806bc167c6bfb33113418cd22f38241d47 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 6acb97920020f180754d3ea41ad04c8a775c0ce3 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 8f31a87307e87c3789a98e8555fcc70e75ac6710 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 c68796ed0220149bb0b5471735189cd555dcd0e9 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 1c26ef786ec66efabd3125d38338fadb266bbd0a 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 8e84bcf0bab5e65f36d9ea2f16942b99e261fc43 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 }