Last active
September 9, 2022 10:25
-
-
Save techdecline/6ce2603064aef20e90ae702a45fef3c2 to your computer and use it in GitHub Desktop.
Get all internal Terraform dependencies from GitLab Projects
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
$env:GITLAB_URL = "<git address>" | |
$env:GITLAB_ACCESS_TOKEN = "<Gitlab PAT>" | |
$GLOBAL:filterStr = "(\.tf|\.hcl|\.gitlab-ci\.yml)$" | |
$GLOBAL:GroupId = "<root group id>" | |
$GLOBAL:refArr = [System.Collections.ArrayList]@() | |
function GetGitLinks { | |
param ( | |
$ProjectId | |
) | |
$projectObj = Get-GitlabProject -ProjectId $ProjectId | |
Get-GitlabRepositoryTree -ProjectId $projectObj.Id -Recurse | Where-Object { $_.Name -match $filterStr } | Select-Object -Property *, @{Name = 'RepositoryName'; Expression = { $projectObj.Name } }, @{Name = 'ProjectId'; Expression = { $projectObj.Id } } | ForEach-Object { | |
$fileObj = $_ | |
$gitLinkArr = Get-GitlabRepositoryFileContent -ProjectId $fileObj.ProjectId -FilePath $fileObj.Path | Select-String -Pattern "$((Get-GitlabConfiguration).Sites[0].Url).*\.git" -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value } | Sort-Object -Unique | |
foreach ($gitLinkObj in $gitLinkArr) { | |
$refObj = 1 | Select-Object -Property @{Name = 'ProjectName'; Expression = { $projectObj.Name } }, ` | |
@{Name = 'ProjectUrl'; Expression = { $projectObj.Url } }, ` | |
@{Name = 'ProjectId'; Expression = { $projectObj.Id } }, ` | |
@{Name = 'FileName'; Expression = { $fileObj.Name } }, ` | |
@{Name = 'FilePath'; Expression = { $fileObj.Path } }, ` | |
@{Name = 'GitRef'; Expression = { $gitLinkObj } } | |
$null = $GLOBAL:refArr.Add($refObj) | |
Write-Output $refObj | |
$nextProjectObj = Get-GitlabProject -Recurse -GroupId $GLOBAL:GroupId | Where-Object { $_.Url -eq ($gitLinkObj).Replace(".git", "") } | |
if ($nextProjectObj) { | |
GetGitLinks -ProjectId $nextProjectObj.Id | |
} | |
else { | |
"Could not get Reference on: $gitLinkObj" | |
} | |
} | |
} | |
} | |
Get-GitlabProject -GroupId $GLOBAL:GroupId | ForEach-Object { | |
write-Host "current Project is $($_.Name)" | |
GetGitLinks -ProjectId $_.Id | |
} | |
return $GLOBAL:refArr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment