Search.psm1

$global:GitlabSearchResultsDefaultLimit = 100

function Search-Gitlab {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter()]
        [ValidateSet('blobs', 'merge_requests', 'projects')]
        [string]
        $Scope = 'blobs',

        [Parameter(Position=0, Mandatory)]
        [string]
        $Search,

        [Parameter()]
        [uint]
        $MaxResults = $global:GitlabSearchResultsDefaultLimit,

        [Parameter()]
        [switch]
        $All,

        [Parameter()]
        [string]
        $Select,

        [Parameter()]
        [switch]
        $OpenInBrowser,

        [Parameter()]
        [string]
        $SiteUrl
    )

    if ($All) {
        if ($MaxResults -ne $global:GitlabSearchResultsDefaultLimit) {
            Write-Warning -Message "Ignoring -MaxResults in favor of -All"
        }
        $MaxResults = [uint]::MaxValue
    }

    $PageSize = 20
    $MaxPages = [Math]::Max(1, $MaxResults / $PageSize)
    $Query = @{
        scope = $Scope
        per_page = $PageSize
        search = $Search
    }

    switch ($Scope) {
        blobs {
            $DisplayType = 'Gitlab.SearchResult.Blob'
        }
        merge_requests {
            $DisplayType = 'Gitlab.SearchResult.MergeRequest'
        }
        projects {
            $DisplayType = 'Gitlab.SearchResult.Project'
        }
    }

    if ($PSCmdlet.ShouldProcess("search", "$($Query | ConvertTo-Json)")) {
        $Results = Invoke-GitlabApi GET 'search' $Query -MaxPages $MaxPages -SiteUrl $SiteUrl | New-WrapperObject $DisplayType

        if ($Scope -eq 'blobs') {
            # the response object is too anemic to be useful. enrich with project data
            $Projects = $Results.ProjectId | Select-Object -Unique | ForEach-Object { @{Id=$_; Project=Get-GitlabProject $_ } }
            $Results | ForEach-Object {
                $_ | Add-Member -MemberType 'NoteProperty' -Name 'Project' -Value $($Projects | Where-Object Id -eq $_.ProjectId | Select-Object -ExpandProperty Project)
            }
        }

        if ($OpenInBrowser) {
            $Results | Where-Object Url | ForEach-Object {
                $_ | Open-InBrowser
            }
        }

        $Results | Get-FilteredObject $Select | Sort-Object ProjectPath
    }
}

# https://docs.gitlab.com/ee/api/search.html#project-search-api
function Search-GitlabProject {
    param(
        [Parameter(Mandatory=$false)]
        [string]
        $ProjectId = '.',

        [Parameter(Mandatory=$false)]
        [string]
        $Search,

        [Parameter(Mandatory=$false)]
        [string]
        $Filename,

        [Parameter(Mandatory=$false)]
        [string]
        $SiteUrl,

        [switch]
        [Parameter(Mandatory=$false)]
        $WhatIf
    )

    $Query = @{
        scope = 'blobs'
    }

    $Project = Get-GitlabProject $ProjectId
    
    if ($Filename) {
        $Query.search = "filename:$Filename"
    } else {
        $Query.search = $Search
    }
    
    $Resource = "projects/$($ProjectId | ConvertTo-UrlEncoded)/search"
    Invoke-GitlabApi GET $Resource $Query -SiteUrl $SiteUrl -WhatIf:$WhatIf |
        New-WrapperObject 'Gitlab.SearchResult.Blob' |
        Add-Member -MemberType 'NoteProperty' -Name 'Project' -Value $Project -PassThru
}