From 7e7b55aa95d00fff5d0e9e4b06947bf38a52b802 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 6b5e170309..5b1b1efa1a 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 2355001ca953ff1a1d484474d6b7aa72b825a140 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 5b1b1efa1a..2ad63f6625 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 858e2ace938937f57cbf0ab69b89d5159584cf86 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 2ad63f6625..7edade4348 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 f37a0a3ba0730766febc24ec5b9dccdc20b06c91 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 7edade4348..4437a9ee4e 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 6f02d2edc6..0bc2e871c8 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 faeabe1492653fabc8d850715089a34fa08aa390 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 4437a9ee4e..4c6cc0c0a0 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 de6709ec512656aa67d0cd5b15d53934a3e5eb2b 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 4c6cc0c0a0..73c3dc1b2b 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 a65abeadb6806d3b1bc9ff9b3f5947deae18163d 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 73c3dc1b2b..7a37058178 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 69b0f908173645741d69b75fd38bdf79f14d8571 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 7a37058178..d8e03690dd 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 fd739bd25e677ffc3a5d09d15cc253300ee4949e 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 d8e03690dd..31f04d5b9a 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 58d842a1083f57e449fe01d367f5db6ac4d5266d 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 31f04d5b9a..6b2e434722 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 a2e31aa98d2edf864f0102d322e8143c99c3fd03 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 6b2e434722..f9e73e7540 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 f76d824eb78e2551c110f730c139cdf97d955c97 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 f9e73e7540..4ee3f1b05d 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 }