Last active
April 18, 2020 11:22
-
-
Save marcopierobon/2e2eb3edb7a513dab5b6c78e67aaaff9 to your computer and use it in GitHub Desktop.
Prevent long file paths in git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param([String] $projectId, | |
[String] $repositoryId, | |
[String] $prTargetBranch, | |
[String] $prSourceBranch, | |
[String] $BUILD_SOURCEVERSIONMESSAGE, | |
[decimal] $acceptedPathLength) | |
#Example usage: .\GetPrDiffsAndCheckForExceedinglyLongFilePaths.ps1 -projectId "daad9574-2d95-4363-a5e1-886ae9549690" -repositoryId "060b25a7-9d74-4897-9fdc-ac29ba8f3b8b" -prSourceBranch "myBranch/mpi_installation_guide_jobs" -prTargetBranch "dev_myBranch" -acceptedPathLength 1 | |
#To Get the projectid and repositoryId, open the devtools from the browser in one of the PR in the repo and search for "workitems". The request will use the params you need | |
#Example url for a manual execution to my_change_branch to dev | |
#https://mtTfsUrl/tfs/myCollection/daad9574-2d95-4363-a5e1-886ae9549690/_apis/git/repositories/060b25a7-9d74-4897-9fdc-ac29ba8f3b8b/diffs/commits?baseVersion=dev&targetVersion=my_change_branch&$top=1000&api-version=3.0-preview | |
#Example for a PR | |
#.\GetPrDiffsAndCheckForExceedinglyLongFilePaths.ps1 -projectId "daad9574-2d95-4363-a5e1-886ae9549690" -repositoryId "060b25a7-9d74-4897-9fdc-ac29ba8f3b8b" -BUILD_SOURCEVERSIONMESSAGE "Merge pull request 909 from myBranch_pod2/mpi_installation_guide_jobs into dev_myBranch" -acceptedPathLength 40 | |
if([string]::IsNullOrEmpty($projectId)){ | |
throw "The parameter $projectId is required"; | |
} | |
if([string]::IsNullOrEmpty($repositoryId)){ | |
throw "The parameter $repositoryId is required"; | |
} | |
if(([string]::IsNullOrEmpty($prSourceBranch)) -and ([string]::IsNullOrEmpty($BUILD_SOURCEVERSIONMESSAGE))){ | |
throw "You either have to pass prSourceBranch or $BUILD_SOURCEVERSIONMESSAGE which will be used to determine the branches."; | |
} | |
if([string]::IsNullOrEmpty($acceptedPathLength)){ | |
throw "The parameter $acceptedPathLength is required"; | |
} | |
$tfsUrlBit="https://mtTfsUrl/tfs/myCollection/" | |
##E.g. projectId | |
#daad9574-2d95-4363-a5e1-886ae9549690 | |
$apiUrlBit= "/_apis/git/repositories/" | |
##E.g. repositoryId | |
#060b25a7-9d74-4897-9fdc-ac29ba8f3b8b | |
$diffUrlBit = "/diffs/commits?baseVersion=" | |
##E.g. prTargetBranch | |
#dev_myBranch | |
$targetBranchBit = "&targetVersion=" | |
##E.g. prSourceBranch | |
#myBranch_pod2/mpi_installation_guide_jobs | |
$finalUrlBit = '&$top=1000&api-version=3.0-preview' | |
if([string]::IsNullOrEmpty($prSourceBranch)){ | |
#e.g. BUILD_SOURCEVERSIONMESSAGE= Merge pull request 909 from dev/myDevBranch into master | |
if($BUILD_SOURCEVERSIONMESSAGE.IndexOf("from") -gt -1 -and $BUILD_SOURCEVERSIONMESSAGE.IndexOf("into") -gt -1){ | |
# https://mtTfsUrl/tfs/myCollection/daad9574-2d95-4363-a5e1-886ae9549690/_apis/git/repositories/060b25a7-9d74-4897-9fdc-ac29ba8f3b8b/pullrequests/25702 | |
$fromOrIntoStringLengthPlusWhiteSpace =5; | |
$positionAfterFrom = $BUILD_SOURCEVERSIONMESSAGE.IndexOf("from") + $fromOrIntoStringLengthPlusWhiteSpace; | |
$positionBeforeInto = $BUILD_SOURCEVERSIONMESSAGE.IndexOf("into") -1; | |
$positionAfterInto = $BUILD_SOURCEVERSIONMESSAGE.IndexOf("into") + $fromOrIntoStringLengthPlusWhiteSpace; | |
$prTargetBranch = $BUILD_SOURCEVERSIONMESSAGE.Substring($positionAfterInto, $BUILD_SOURCEVERSIONMESSAGE.Length - $positionAfterInto); | |
$prSourceBranch = $BUILD_SOURCEVERSIONMESSAGE.Substring($positionAfterFrom, $positionBeforeInto - $positionAfterFrom); | |
} | |
} | |
elseif($prSourceBranch.IndexOf("refs/pull") -gt -1){ | |
$pullRequestId= $prSourceBranch.Replace("refs/pull/","").Replace("/merge",""); | |
$tfsPullRequestUrl = "$($tfsUrlBit)$($projectId)$($apiUrlBit)$($repositoryId)/pullrequests/$($pullRequestId)" | |
Write-Host $"Invoking the url ($tfsPullRequestUrl) to get the branches associated with the PR with id $($pullRequestId)" | |
$jsonPrResult = Invoke-RestMethod -UseDefaultCredentials -uri $tfsPullRequestUrl | |
$prSourceBranch = $jsonPrResult.sourceRefName.replace("refs/heads/", ""); | |
$prTargetBranch = $jsonPrResult.targetRefName.replace("refs/heads/", ""); | |
} | |
elseif($prSourceBranch.IndexOf("refs/heads/") -gt -1){ | |
$prSourceBranch = $prSourceBranch.replace("refs/heads/", ""); | |
$prTargetBranch = $prTargetBranch.replace("refs/heads/", ""); | |
} | |
if([string]::IsNullOrEmpty($prSourceBranch) -or ([string]::IsNullOrEmpty($prTargetBranch))){ | |
throw "Could not reliabily determine the source and target branch" | |
} | |
Write-Host "Using $($prTargetBranch) as the target branch and $($prSourceBranch) as the source branch" | |
$tfsRequestUrl = "$($tfsUrlBit)$($projectId)$($apiUrlBit)$($repositoryId)$($diffUrlBit)$($prTargetBranch)$($targetBranchBit)$($prSourceBranch)$($finalUrlBit)" | |
Write-Host $"Invoking the url ($tfsRequestUrl) to get all the changed files in this PR" | |
$jsonResult = Invoke-RestMethod -UseDefaultCredentials -uri $tfsRequestUrl | |
$fileChanges = $jsonResult.changes; | |
$filesWithFilePathTooLong = @(); | |
$fileChanges | ForEach-Object { | |
$currentItem = $_.item; | |
$currentItemPath = $currentItem.path; | |
if(($currentItemPath.Length -gt $acceptedPathLength) -and (-not ($_.changeType.Contains("delete")))){ | |
$filesWithFilePathTooLong += , "$($currentItemPath) with length $($currentItemPath.Length)`n" | |
} | |
} | |
if($filesWithFilePathTooLong.Length -gt 0){ | |
throw "The following $($filesWithFilePathTooLong.Length) files were found to have a path longer than the accepted value of $($acceptedPathLength): `n $($filesWithFilePathTooLong)" | |
} | |
else{ | |
Write-Host "No files were found to have a pathlonger than the accepted value of ($acceptedPathLength). Happy days." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment