File: git-helpers.ps1

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (362 lines) | stat: -rw-r--r-- 10,518 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# cSpell:ignore Committish
# cSpell:ignore PULLREQUEST
# cSpell:ignore TARGETBRANCH
# cSpell:ignore SOURCECOMMITID

function RequestGithubGraphQL {
  param (
    [string]$query,
    [object]$variables = @{}
  )

  $payload = @{
    query     = $query
    variables = $variables
  } | ConvertTo-Json -Depth 100

  $response = $payload | gh api graphql --input -
  $data = $response | ConvertFrom-Json

  if ($LASTEXITCODE) {
    LogWarning "Failed graphql operation:"
    LogWarning ($payload -replace '\\n', "`n")
    if ($data.errors) {
      LogWarning ($data.errors.message -join "`n")
    }
    throw "graphql operation failed ($LASTEXITCODE)"
  }

  return $data
}

function Get-ChangedFiles {
  param (
    [string]$SourceCommittish = "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}",
    [string]$TargetCommittish = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/"),
    [string]$DiffPath,
    [string]$DiffFilterType = "d"
  )
  # If ${env:SYSTEM_PULLREQUEST_TARGETBRANCH} is empty, then return empty.
  if (!$TargetCommittish -or ($TargetCommittish -eq "origin/")) {
    Write-Host "There is no target branch passed in. "
    return ""
  }

  # Add config to disable the quote and encoding on file name.
  # Ref: https://github.com/msysgit/msysgit/wiki/Git-for-Windows-Unicode-Support#disable-quoted-file-names
  # Ref: https://github.com/msysgit/msysgit/wiki/Git-for-Windows-Unicode-Support#disable-commit-message-transcoding
  # Git PR diff: https://docs.github.com/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-comparing-branches-in-pull-requests#three-dot-and-two-dot-git-diff-comparisons
  $command = "git -c core.quotepath=off -c i18n.logoutputencoding=utf-8 diff `"$TargetCommittish...$SourceCommittish`" --name-only --diff-filter=$DiffFilterType"
  if ($DiffPath) {
    $command = $command + " -- `'$DiffPath`'"
  }
  Write-Host $command
  $changedFiles = Invoke-Expression -Command $command
  if (!$changedFiles) {
    Write-Host "No changed files in git diff between $TargetCommittish and $SourceCommittish"
  }
  else {
    Write-Host "Here are the diff files:"
    foreach ($file in $changedFiles) {
      Write-Host "    $file"
    }
  }
  return $changedFiles
}

class ConflictedFile {
  [string]$LeftSource = ""
  [string]$RightSource = ""
  [string]$Content = ""
  [string]$Path = ""
  [boolean]$IsConflicted = $false

  ConflictedFile([string]$File = "") {
    if (!(Test-Path $File)) {
      throw "File $File does not exist, pass a valid file path to the constructor."
    }

    # Normally we would use Resolve-Path $file, but git only can handle relative paths using git show <commitsh>:<path>
    # Therefore, just maintain whatever the path is given to us. Left() and Right() should therefore be called from the same
    # directory as where we defined the relative path to the target file.
    $this.Path = $File
    $this.Content = Get-Content -Raw $File

    $this.ParseContent($this.Content)
  }

  [array] Left() {
    if ($this.IsConflicted) {
      # we are forced to get this line by line and reassemble via join because of how powershell is interacting with
      # git show --textconv commitsh:path
      # powershell ignores the newlines with and without --textconv, which results in a json file without original spacing.
      # by forcefully reading into the array line by line, the whitespace is preserved. we're relying on gits autoconverstion of clrf to lf
      # to ensure that the line endings are consistent.
      $toShow = "$($this.LeftSource):$($this.Path)" -replace "\\", "/"
      Write-Host "git show $toShow"
      $tempContent = git show $toShow
      return $tempContent -split "`r?`n"
    }
    else {
      return $this.Content
    }
  }

  [array] Right() {
    if ($this.IsConflicted) {
      $toShow = "$($this.RightSource):$($this.Path)" -replace "\\", "/"
      Write-Host "git show $toShow"
      $tempContent = git show $toShow
      return $tempContent -split "`r?`n"
    }
    else {
      return $this.Content
    }
  }

  [void] ParseContent([string]$IncomingContent) {
    $lines = $IncomingContent -split "`r?`n"
    $l = @()
    $r = @()

    foreach ($line in $lines) {
      if ($line -match "^<<<<<<<\s*(.+)") {
        $this.IsConflicted = $true
        $this.LeftSource = $matches[1]
        continue
      }
      elseif ($line -match "^>>>>>>>\s*(.+)") {
        $this.IsConflicted = $true
        $this.RightSource = $matches[1]
        continue
      }

      if ($this.LeftSource -and $this.RightSource) {
        break
      }
    }
  }
}

# The rate limit comes back in the following format:
# The top level "rate" object is deprecated and the resources->core object should be used
# in its place.
# {
#   "resources": {
#     "core": {
#       "limit": 5000,
#       "used": 1087,
#       "remaining": 3913,
#       "reset": 1722876411
#     },
#     "search": {
#       "limit": 30,
#       "used": 0,
#       "remaining": 30,
#       "reset": 1722875519
#     },
#     "graphql": {
#       "limit": 5000,
#       "used": 0,
#       "remaining": 5000,
#       "reset": 1722879059
#     },
#     "integration_manifest": {
#       "limit": 5000,
#       "used": 0,
#       "remaining": 5000,
#       "reset": 1722879059
#     },
#     "source_import": {
#       "limit": 100,
#       "used": 0,
#       "remaining": 100,
#       "reset": 1722875519
#     },
#     "code_scanning_upload": {
#       "limit": 1000,
#       "used": 0,
#       "remaining": 1000,
#       "reset": 1722879059
#     },
#     "actions_runner_registration": {
#       "limit": 10000,
#       "used": 0,
#       "remaining": 10000,
#       "reset": 1722879059
#     },
#     "scim": {
#       "limit": 15000,
#       "used": 0,
#       "remaining": 15000,
#       "reset": 1722879059
#     },
#     "dependency_snapshots": {
#       "limit": 100,
#       "used": 0,
#       "remaining": 100,
#       "reset": 1722875519
#     },
#     "audit_log": {
#       "limit": 1750,
#       "used": 0,
#       "remaining": 1750,
#       "reset": 1722879059
#     },
#     "audit_log_streaming": {
#       "limit": 15,
#       "used": 0,
#       "remaining": 15,
#       "reset": 1722879059
#     },
#     "code_search": {
#       "limit": 10,
#       "used": 0,
#       "remaining": 10,
#       "reset": 1722875519
#     }
#   },
#   "rate": {
#     "limit": 5000,
#     "used": 1087,
#     "remaining": 3913,
#     "reset": 1722876411
#   }
# }

# These are the rate limit types we care about. If others needed in the future they
# can be defined here. The reason these need to be defined is because Get-RateLimit
# call needs to select the particular property to return the right limit. This ensures
# that rate limit type being passed to the function will exist.
enum RateLimitTypes {
  core
  search
  graphql
}

# Fetch the rate limit for the given RateLimitType
function Get-RateLimit([RateLimitTypes]$RateLimitType) {
  $returnValue = gh api rate_limit
  if ($LASTEXITCODE) {
    LogError "Get-RateLimit::unable to get rate limit"
    exit $LASTEXITCODE
  }
  # All rate limits have the following fields: limit, used, remaning, reset.
  # Returning -AsHashtable allows easier access, eg. $rate_limit.remaining
  $rate_limit = $returnValue | ConvertFrom-Json -AsHashtable | Select-Object -ExpandProperty resources | Select-Object -ExpandProperty $RateLimitType
  # Add the limit type for convenance
  $rate_limit["type"] = $RateLimitType
  return $rate_limit
}

# Get the number of minutes until RateLimit reset rounded up to the nearest minute
# for the passed in RateLimit. This is more applicable to the core and graphql rate
# limits than search because the search rate limit resets every minute
function Get-MinutesUntilRateLimitReset($RateLimit) {
  $TimeSpan = [System.DateTimeOffset]::FromUnixTimeSeconds($rate.reset).UtcDateTime.Subtract([System.DateTime]::UtcNow)
  $MinutesRoundedUp = [Math]::Ceiling($TimeSpan.TotalMinutes)
  return $MinutesRoundedUp
}

# Output the rate limit
function Write-RateLimit {
  param (
    $RateLimit,
    [string]$PreMsg = $null
  )

  if ($PreMsg) {
    Write-Host $PreMsg
  }
  Write-Host "Limit Type=$($RateLimit.type)"
  Write-Host "    limit=$($RateLimit.limit)"
  Write-Host "    used=$($RateLimit.used)"
  Write-Host "    remaining=$($RateLimit.remaining)"
  Write-Host "    reset=$($RateLimit.reset)"
  Write-Host ""
}

function GetUnresolvedAIReviewThreads {
  param(
    [string]$repoOwner,
    [string]$repoName,
    [string]$prNumber,
    [array]$reviewers
  )

  $reviewers ??= @('copilot-pull-request-reviewer')

  $reviewThreadsQuery = @'
query ReviewThreads($owner: String!, $name: String!, $number: Int!) {
  repository(owner: $owner, name: $name) {
    pullRequest(number: $number) {
      reviewThreads(first: 100) {
        nodes {
          id
          isResolved
          comments(first: 100) {
            nodes {
              body
              author {
                login
              }
            }
          }
        }
      }
    }
  }
}
'@

  $variables = @{ owner = $repoOwner; name = $repoName; number = [int]$prNumber }
  $response = RequestGithubGraphQL -query $reviewThreadsQuery -variables $variables
  $reviews = $response.data.repository.pullRequest.reviewThreads.nodes

  $threadIds = @()
  # There should be only one threadId for copilot, but make it an array in case there
  # are more, or if we want to resolve threads from multiple ai authors in the future.
  # Don't mark threads from humans as resolved, as those may be real questions/blockers.
  foreach ($thread in $reviews) {
    if ($thread.comments.nodes | Where-Object { $_.author.login -in $reviewers }) {
      if (!$thread.isResolved) {
        $threadIds += $thread.id
      }
      continue
    }
  }

  return $threadIds
}

function TryResolveAIReviewThreads {
  param(
    [string]$repoOwner,
    [string]$repoName,
    [string]$prNumber,
    [array]$reviewers
  )

  $resolveThreadMutation = @'
mutation ResolveThread($id: ID!) {
  resolveReviewThread(input: { threadId: $id }) {
    thread {
      isResolved
    }
  }
}
'@

  $threadIds = GetUnresolvedAIReviewThreads -repoOwner $repoOwner -repoName $repoName -prNumber $prNumber -reviewers $reviewers

  if (!$threadIds) {
    return $false
  }

  foreach ($threadId in $threadIds) {
    LogInfo "Resolving review thread '$threadId' for '$repoName' PR '$prNumber'"
    RequestGithubGraphQL -query $resolveThreadMutation -variables @{ id = $threadId }
  }

  return $true
}