microsoft / PowerShellForGitHub

Microsoft PowerShell wrapper for GitHub API
Other
584 stars 184 forks source link

Feature Request: Artifacts API #315

Open JustinGrote opened 3 years ago

JustinGrote commented 3 years ago

Feature Idea Summary

Implement the artifacts API for Github Actions primarily to download artifacts. This is useful for grabbing commit builds, etc. quickly using scripts, or updating a test to the latest commit

Feature Idea Additional Details

https://docs.github.com/en/rest/reference/actions#artifacts Conventions would match other cmdlets. Of primary interest are list, get, download

Requested Assignment

If tagged as up-for-grabs I will attempt to implement this.

HowardWolosky commented 3 years ago

Thanks for the request, @JustinGrote! The end goal is to fully support the entire API surface in this module. The only limiting factor has been time. You are more then welcome to begin work on this part of the API if it interests you. Just please respond back to let me know you are starting work on it so that I can change the tags to prevent duplicated efforts from anyone else seeing them.

JustinGrote commented 3 years ago

A basic POC, obviously it needs to be boilerplated out to the format used by the other functions

function get-GHWorkflow {
    param(
        [ValidateNotNullOrEmpty()]$User = 'twpayne',
        [ValidateNotNullOrEmpty()]$Repository = 'chezmoi',
        [uri]$Uri = 'https://api.github.com/repos'
    )

    (Invoke-GHRestMethod -Method Get -UriFragment "$uri/$User/$Repository/actions/workflows").workflows
}

function get-GHWorkflowRun {
    param(
        [PSObject][Parameter(ValueFromPipeline)]$Workflow,
        [Switch]$Successful
    )

    process {
        $irmParams = @{
            UriFragment     = $Workflow.url + '/runs'
            Method          = 'GET'
        }
        if ($Successful) { $irmParams.Body = @{status = 'success'} }
        (Invoke-GHRestMethod @irmParams).workflow_runs
    }
}

function get-GHWorkflowArtifactUrl {
    param(
        [PSObject][Parameter(ValueFromPipeline)]$Run,
        [Switch]$Successful
    )

    process {
        $irmParams = @{
            UriFragment     = $Run.artifacts_url
            Method = 'GET'
        }
        $artifacts = (Invoke-GHRestMethod @irmParams).artifacts
        $baseUri = 'https://github.com/twpayne/chezmoi/suites/' + $Run.check_suite_id + '/artifacts/'

        $artifacts.foreach{
            $PSItem | FL | out-string | WRite-Host -fore magenta
            [PSCustomObject][ordered]@{
                Name = $PSItem.name
                Uri = [String]($baseUri + $PSItem.id)
            }
        }
    }
}

function Get-GHLatestArtifact {
    param(
        #Name of the workflow
        [Parameter(Mandatory)]$Name
    )

    Get-GHWorkflow 
    | Where-Object name -EQ $Name 
    | get-GHWorkflowRun -Successful 
    | Sort-Object created_at -Descending
    | Select-Object -First 1
    | get-GHWorkflowArtifactUrl
}

Example result of Get-GHLatestArtifact

Name                      Uri
----                      ---
chezmoi2-linux-amd64      https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406762
chezmoi2-linux-musl-amd64 https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406763
chezmoi2-macos-amd64      https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406764
chezmoi2-windows-amd64    https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406765
chezmoi-linux-amd64       https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406766
chezmoi-linux-musl-amd64  https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406767
chezmoi-macos-amd64       https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406768
chezmoi-windows-amd64     https://github.com/twpayne/chezmoi/suites/1905681601/artifacts/37406769