Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀[Feature]: Add a context validator, so we can add condition into highlevel functions #186

Open
1 of 4 tasks
MariusStorhaug opened this issue Dec 5, 2024 · 0 comments
Labels
feature New feature

Comments

@MariusStorhaug
Copy link
Contributor

MariusStorhaug commented Dec 5, 2024

Description

Want to be able so say that a certain type of functions can only be called with JWT (App) some can only be called as a user, functions requires specific permissions... etc. Many things to be able to check... !

  • Assert-GitHubContext
function Assert-GitHubContext {
    <#
    .SYNOPSIS
    Check if the context meets the requirements for the command.

    .DESCRIPTION
    This function checks if the context meets the requirements for the command.
    If the context does not meet the requirements, an error is thrown.

    .EXAMPLE
    Assert-GitHubContext -Context 'github.com/Octocat' -TokenType ''

    .NOTES
    General notes
    #>
    [CmdletBinding()]
    param(
        # The context to run the command in.
        [Parameter(Mandatory)]
        [GitHubContext] $Context,

        # The command that is being checked.
        [Parameter(Mandatory)]
        [string] $Command,

        # The required authtypes for the command.
        [Parameter(Mandatory)]
        [string[]] $AuthType
    )

    #TODO: Add an assertion for if the context is organization or user. Needed for the stuff that has to be an org (teams) and not a user.
    if ($Context.AuthType -notin $AuthType) {
        throw "The context '$($Context.Name)' does not match the required AuthTypes [$AuthType] for [$Command]."
    }
}
  • Implement in Invoke-GitHubAPI
function Invoke-GitHubAPI {
    param(

        # The requirements for the function call. This is used to determine if the context is valid for the API call.
        # Keys:
        # - name: AuthType
        #   description:  The type of token required for the command.
        #   type: [string[]]
        #   allowedValues: ['UAT', 'PAT', 'App', 'IAT']
        [Parameter()]
        [hashtable] $Requires,

    )

    # Validate if the requirements for the API call are met
    $callingFunction = (Get-PSCallStack)[1].Command
    if ($callingFunction -ne '<ScriptBlock>' -and $PSBoundParameters.ContainsKey('Requires')) {
        $check = @{
            Command  = $callingFunction
            Requires = $Requires
            Context  = $Context
        }
        Assert-GitHubContext @check
    }
}
  • Implement in functions
  • Implement in functiontemplate

For functions:

    $inputObject = @{
        Context     = $Context
        APIEndpoint = '/app/installations'
        Method      = 'GET'
        Requires    = @{
            AuthType = 'App'
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
Status: Todo
Development

No branches or pull requests

1 participant