test

# GitLab API setup
$gitlabToken = “your-gitlab-access-token”
$gitlabApiBaseUrl = “https://gitlab.com/api/v4”
$projectId = “your-project-id”

# Headers for API requests
$headers = @{
“Private-Token” = $gitlabToken
}

# Hashtable to cache iteration details
$iterationCache = @{}

# Function to get issue details from GitLab
function Get-IssueDetails {
param (
[int]$issueId
)

$issueUrl = “$gitlabApiBaseUrl/projects/$projectId/issues/$issueId”
$issue = Invoke-RestMethod -Uri $issueUrl -Headers $headers

return $issue
}

# Function to get iteration details by iteration ID, with caching
function Get-IterationDetails {
param (
[int]$iterationId
)

# Check if the iteration is already in the cache
if ($iterationCache.ContainsKey($iterationId)) {
return $iterationCache[$iterationId]
}

# Fetch iteration details if not in the cache
$iterationUrl = “$gitlabApiBaseUrl/projects/$projectId/iterations/$iterationId”
$iteration = Invoke-RestMethod -Uri $iterationUrl -Headers $headers

# Store the iteration details in the cache
$iterationCache[$iterationId] = $iteration

return $iteration
}

# Function to get issue events (for labels and closing user)
function Get-IssueEvents {
param (
[int]$issueId
)

$eventsUrl = “$gitlabApiBaseUrl/projects/$projectId/issues/$issueId/resource_label_events”
$events = Invoke-RestMethod -Uri $eventsUrl -Headers $headers

return $events
}

# Function to fetch all issues with pagination
function Get-AllIssues {
$allIssues = @()
$page = 1
$perPage = 100
$hasMorePages = $true

while ($hasMorePages) {
$issuesUrl = “$gitlabApiBaseUrl/projects/$projectId/issues?per_page=$perPage&page=$page”
$issues = Invoke-RestMethod -Uri $issuesUrl -Headers $headers

if ($issues.Count -eq 0) {
$hasMorePages = $false
} else {
$allIssues += $issues
$page++
}
}

return $allIssues
}

# Prepare output array
$output = @()

# Fetch all issues
$issues = Get-AllIssues

# Loop through each issue
foreach ($issue in $issues) {
$issueDetails = Get-IssueDetails -issueId $issue.id

# Check if the issue has an iteration ID
if ($issueDetails.iteration.id) {
$iteration = Get-IterationDetails -iterationId $issueDetails.iteration.id

# Filter by iteration name starting with “A”
if ($iteration.title -like “A*”) {

# Get issue events for label and closing details
$events = Get-IssueEvents -issueId $issue.id

# Find the label assignments and closing event
$labelAEvent = $events | Where-Object { $_.label.name -eq “A” }
$labelBEvent = $events | Where-Object { $_.label.name -eq “B” }
$closedEvent = $issueDetails.closed_by

# Prepare the CSV row with checks for missing data
$output += [pscustomobject]@{
“Ticket ID” = $issue.iid
“Created Date” = $issue.created_at
“Iteration Name” = $iteration.title
“Label A Assigned Date” = if ($labelAEvent) { $labelAEvent.created_at } else { “N/A” }
“Label A Assigned By” = if ($labelAEvent) { $labelAEvent.user.username } else { “N/A” }
“Label B Assigned Date” = if ($labelBEvent) { $labelBEvent.created_at } else { “N/A” }
“Label B Assigned By” = if ($labelBEvent) { $labelBEvent.user.username } else { “N/A” }
“Closed Date” = if ($issue.closed_at) { $issue.closed_at } else { “N/A” }
“Closed By” = if ($closedEvent) { $closedEvent.username } else { “N/A” }
}
}
}
}

# Export the result to CSV
$output | Export-Csv -Path “gitlab_issues_report.csv” -NoTypeInformation

Write-Host “Report generated: gitlab_issues_report.csv”