...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/scripts/Delete-RemoteBranches.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/scripts

     1[CmdletBinding(SupportsShouldProcess)]
     2param(
     3  # The repo owner: e.g. Azure
     4  $RepoOwner,
     5  # The repo name. E.g. azure-sdk-for-java
     6  $RepoName,
     7  # Please use the RepoOwner/RepoName format: e.g. Azure/azure-sdk-for-java
     8  $RepoId="$RepoOwner/$RepoName",
     9  # CentralRepoId the original PR to generate sync PR. E.g Azure/azure-sdk-tools for eng/common
    10  $CentralRepoId,
    11  # We start from the sync PRs, use the branch name to get the PR number of central repo. E.g. sync-eng/common-(<branchName>)-(<PrNumber>). Have group name on PR number.
    12  # For sync-eng/common work, we use regex as "^sync-eng/common.*-(?<PrNumber>\d+).*$".
    13  $BranchRegex,
    14  # Date format: e.g. Tuesday, April 12, 2022 1:36:02 PM. Allow to use other date format.
    15  [AllowNull()]
    16  [DateTime]$LastCommitOlderThan,
    17  [Parameter(Mandatory = $true)]
    18  $AuthToken
    19)
    20
    21. (Join-Path $PSScriptRoot common.ps1)
    22
    23LogDebug "Operating on Repo [ $RepoId ]"
    24
    25try{
    26  # pull all branches.
    27  $responses = Get-GitHubSourceReferences -RepoId $RepoId -Ref "heads" -AuthToken $AuthToken
    28}
    29catch {
    30  LogError "Get-GitHubSourceReferences failed with exception:`n$_"
    31  exit 1
    32}
    33
    34foreach ($res in $responses)
    35{
    36  if (!$res -or !$res.ref) {
    37    LogDebug "No branch returned from the branch prefix $BranchRegex on $Repo. Skipping..."
    38    continue
    39  }
    40  $branch = $res.ref
    41  $branchName = $branch.Replace("refs/heads/","")
    42  if (!($branchName -match $BranchRegex)) {
    43    continue
    44  }
    45
    46  # Get all open sync PRs associate with branch.
    47  try {
    48    $head = "${RepoId}:${branchName}"
    49    LogDebug "Operating on branch [ $branchName ]"
    50    $pullRequests = Get-GitHubPullRequests -RepoId $RepoId -State "all" -Head $head -AuthToken $AuthToken
    51  }
    52  catch
    53  {
    54    LogError "Get-GitHubPullRequests failed with exception:`n$_"
    55    exit 1
    56  }
    57  $openPullRequests = $pullRequests | ? { $_.State -eq "open" }
    58
    59  if (!$CentralRepoId -and $openPullRequests.Count -gt 0) {
    60    LogDebug "CentralRepoId is not configured and found open PRs associate with branch [ $branchName ]. Skipping..."
    61    continue
    62  }
    63
    64  # check central PR
    65  if ($CentralRepoId) {
    66    $pullRequestNumber = $Matches["PrNumber"]
    67    # If central PR number found, then skip
    68    if (!$pullRequestNumber) {
    69      LogError "No PR number found in the branch name. Please check the branch name [ $branchName ]. Skipping..."
    70      continue
    71    }
    72      
    73    try {
    74      $centralPR = Get-GitHubPullRequest -RepoId $CentralRepoId -PullRequestNumber $pullRequestNumber -AuthToken $AuthToken
    75      LogDebug "Found central PR pull request: $($centralPR.html_url)"
    76      if ($centralPR.state -ne "closed") {
    77        # Skipping if there open central PR number for the branch.
    78        continue
    79      }
    80    }
    81    catch 
    82    {
    83      # If there is no central PR for the PR number, log error and skip.
    84      LogError "Get-GitHubPullRequests failed with exception:`n$_"
    85      LogError "Not found PR number [ $pullRequestNumber ] from [ $CentralRepoId ]. Skipping..."
    86      continue
    87    }
    88  }
    89
    90  foreach ($openPullRequest in $openPullRequests) {
    91    Write-Host "Open pull Request [ $($openPullRequest.html_url) ] will be closed after branch deletion."
    92  }
    93
    94  # If there is date filter, then check if branch last commit older than the date.
    95  if ($LastCommitOlderThan) {
    96    if (!$res.object -or !$res.object.url) {
    97      LogWarning "No commit url returned from response. Skipping... "
    98      continue
    99    }
   100    try {
   101      $commitDate = Get-GithubReferenceCommitDate -commitUrl $res.object.url -AuthToken $AuthToken
   102      if (!$commitDate) {
   103        LogDebug "No last commit date found. Skipping."
   104        continue
   105      }
   106      if ($commitDate -gt $LastCommitOlderThan) {
   107        LogDebug "The branch $branch last commit date [ $commitDate ] is newer than the date $LastCommitOlderThan. Skipping."
   108        continue
   109      }
   110      
   111      LogDebug "Branch [ $branchName ] in repo [ $RepoId ] has a last commit date [ $commitDate ] that is older than $LastCommitOlderThan. "
   112    }
   113    catch {
   114      LogError "Get-GithubReferenceCommitDate failed with exception:`n$_"
   115      exit 1
   116    }
   117  } 
   118  
   119  try {
   120    if ($PSCmdlet.ShouldProcess("[ $branchName ] in [ $RepoId ]", "Deleting branches on cleanup script")) {
   121      Remove-GitHubSourceReferences -RepoId $RepoId -Ref $branch -AuthToken $AuthToken
   122      Write-Host "The branch [ $branchName ] with sha [$($res.object.sha)] in [ $RepoId ] has been deleted."
   123    }
   124  }
   125  catch {
   126    LogError "Remove-GitHubSourceReferences failed with exception:`n$_"
   127    exit 1
   128  }
   129}

View as plain text