Skip to content

Instantly share code, notes, and snippets.

@ChendrayanV
Last active November 10, 2023 11:11
Show Gist options
  • Save ChendrayanV/5e37456abc0311d43add56101d1bcb77 to your computer and use it in GitHub Desktop.
Save ChendrayanV/5e37456abc0311d43add56101d1bcb77 to your computer and use it in GitHub Desktop.
A PowerShell Script to retrieve SCALAR properties value of the GitLab projects using GraphQL
param (
$Organization,
$PrivateToken,
$ProjectFullPath
)
$query = @{
query = '{
__type(name: "Project") {
name
fields {
name
type {
name
kind
ofType {
name
kind
}
}
}
}
}'
} | ConvertTo-Json -Compress
$response = Invoke-RestMethod -Uri "https://$($Organization)/api/graphql" -Headers @{Authorization = "Bearer $($PrivateToken)" } -Method Post -Body $query -ContentType 'application/json'
$fieldCollection = @()
foreach ($field in $response.data.__type.fields) {
$fieldCollection += [PSCustomObject]@{
FieldName = $($field.name)
FieldType = $($field.type.name)
Kind = $($field.type.kind)
}
}
$fields = ($fieldCollection | Where-Object { $_.kind -eq 'SCALAR' }).FieldName -join ','
$fragment = @{
query = @"
query {
project(fullPath: "$($ProjectFullPath)") {
...fields
}
}
fragment fields on Project {
$($fields)
}
"@
} | ConvertTo-Json
$project = Invoke-RestMethod -Uri "https://$($Organization)/api/graphql" -Headers @{Authorization = "Bearer $($PrivateToken)" } -Method Post -Body $fragment -ContentType 'application/json'
$project.data.project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment