Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Test/private/MockCall_Project700.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Get-Mock_Project_700 {
$project.projectFile = "invoke-GitHubOrgProjectWithFields-octodemo-700.json"
$project.projectFile_skipitems = "invoke-GitHubOrgProjectWithFields-octodemo-700-skipitems.json"
$project.projectFile_WrongField = "invoke-GitHubOrgProjectWithFields-octodemo-700-skipitems-WrongField.json"
$project.repoFile = "invoke-repository-rulasg-dev-1.json"

# Version of the project file modified manually to have two items with same id case sensitive
# this is used to test case sensitivity of item ids in hashtables
Expand All @@ -26,6 +27,12 @@ function Get-Mock_Project_700 {
$fielddate = $pActual.fields.nodes | Where-Object { $_.name -eq "field-date" }
$fieldsingleselect = $pActual.fields.nodes | Where-Object { $_.name -eq "field-singleselect" }

# Repository Info
$repoContent = Get-MockFileContentJson -fileName $project.repofile -AsHashtable
$project.repository = $repoContent.data.repository
$project.repository.owner = $repoContent.data.repository.owner.login
$project.repository.Remove('parent')

# Project info
$project.id = $pActual.id
$project.owner = $pActual.owner.login
Expand All @@ -47,6 +54,13 @@ function Get-Mock_Project_700 {
$project.items.totalCount = $pActual.items.nodes.count
$project.items.doneCount = 6 # too complicated to read from structure

# Create issue in repo
$project.createIssueInRepo = @{
name = $project.repository.name
owner = $project.repository.owner
id = $project.repository.id
issueUrl = "https://github.com/octodemo/rulasg-dev-1/issues/30"
}
# Issues to find
$project.issueToFind = @{}
$project.issueToFind.Ids = ($pActual.items.nodes | Where-Object { $_.content.title -eq "Issue to find" }).Id
Expand Down
9 changes: 8 additions & 1 deletion Test/private/mockdatabase.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

$folderName = "test_database_path"
function Mock_DatabaseRoot([switch]$NotReset){

$folderName = "test_database_path"
# Check if the database path exists
if (-Not (Test-Path -Path $folderName -PathType Container)) {
New-Item -Path $folderName -ItemType Directory -Force | Out-Null
Expand All @@ -16,3 +17,9 @@ function Mock_DatabaseRoot([switch]$NotReset){
}
}

function Get-Mock_DatabaseRootPath{

return $folderName | Convert-Path

}

10 changes: 10 additions & 0 deletions Test/private/mocks/invoke-createissue-R_kgDOPrRnkQ.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"data": {
"createIssue": {
"issue": {
"id": "I_kwDOPrRnkc7Sj4N0",
"url": "https://github.com/octodemo/rulasg-dev-1/issues/30"
}
}
}
}
14 changes: 14 additions & 0 deletions Test/private/mocks/invoke-repository-rulasg-dev-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"data": {
"repository": {
"id": "R_kgDOPrRnkQ",
"name": "rulasg-dev-1",
"owner": {
"login": "octodemo"
},
"hasIssuesEnabled": true,
"description": "development repo 1",
"parent": null
}
}
}
26 changes: 26 additions & 0 deletions Test/public/issues/New-ProjectIssue.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Test_NewProjectIssueDirect{

Reset-InvokeCommandMock
Mock_DatabaseRoot

$p = Get-Mock_Project_700 ;
$r = $p.createIssueInRepo

$title = "Test Issue from New-ProjectIssueDirect"
$body = "This is a test issue created by New-ProjectIssueDirect test"
$command = "Invoke-CreateIssue -RepositoryId $($r.id) -Title ""$title"" -Body ""$body"""

MockCallJson -Command "Invoke-Repository -Owner $($r.owner) -Name $($r.name)" -FileName $p.repoFile
MockCallJson -command $command -FileName "invoke-createissue-$($r.id).json"


$result = New-ProjectIssueDirect -RepoOwner $r.owner -RepoName $r.name -Title "Test Issue from New-ProjectIssueDirect" -Body "This is a test issue created by New-ProjectIssueDirect test"

# Invoke-CreateIssue -RepositoryId $result.Id -Title "Test Issue from New-ProjectIssueDirect" -Body "This is a test issue created by New-ProjectIssueDirect test" -ProjectIds @($p.id)

# Assert
Assert-AreEqual -Expected $r.issueUrl -Presented $result

}

test
32 changes: 32 additions & 0 deletions Test/public/repository/get-repository.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function Test_GetRepository{

Reset-InvokeCommandMock
Mock_DatabaseRoot

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$p = Get-Mock_Project_700 ;
$r = $p.repository

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
MockCallJson -Command "Invoke-Repository -Owner $($r.owner) -Name $($r.name)" -FileName $p.repoFile

$result = Get-Repository -Owner $r.owner -Name $r.name

#Assert
foreach ( $key in $r.Keys ){
Assert-AreEqual -Expected:$r.$key -Presented:$result.$key
}

# Assert repo cache created
$dbpath = get-Mock_DatabaseRootPath
$dbname = "$($r.owner)-$($r.name).json"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
Assert-ItemExist -Path (Join-Path -Path $dbpath -ChildPath $dbname)

# reset mocks and get repo to use cache
Reset-InvokeCommandMock
Mock_DatabaseRoot -NotReset

$result = Get-Repository -Owner $r.owner -Name $r.name

#Assert
Assert-AreEqual -Expected:$r.id -Presented:$result.id
}
218 changes: 218 additions & 0 deletions include/callAPI.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@

# INCLUDE CALL API
#
# This module provides functions to call the GitHub API
#

function Invoke-GraphQL {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-GraphQL' does not have a help comment. Note

The cmdlet 'Invoke-GraphQL' does not have a help comment.
param(
[Parameter(Mandatory=$true)] [string]$Query,
[Parameter(Mandatory=$true)] [object]$Variables,
[Parameter()] [string]$Token,
[Parameter()] [string]$ApiHost,
[Parameter()] [string]$OutFile
)

">>>" | Write-MyVerbose

$ApiHost = Get-ApiHost -ApiHost:$ApiHost
$token = Get-ApiToken -Token:$Token -ApiHost:$ApiHost

try {
$apiUri = "https://api.$ApiHost/graphql"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Define the headers for the request
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
"GraphQL-Features" = $GRAPHQL_FEATURES
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Define the body for the request
$body = @{
query = $Query
variables = $Variables
} | ConvertTo-Json -Depth 100

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Trace request
"[[QUERY]]" | Write-MyVerbose
$Query | Write-MyVerbose

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
"[[VARIABLES]]" | Write-MyVerbose
$Variables | ConvertTo-Json -Depth 100 | Write-MyVerbose

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Send the request
$start = Get-Date
">>> Invoke-RestMethod - $apiUri" | Write-MyVerbose
$response = Invoke-RestMethod -Uri $apiUri -Method Post -Body $body -Headers $headers -OutFile $OutFile
"<<< Invoke-RestMethod - $apiUri [ $(((Get-Date) - $start).TotalSeconds) seconds]" | Write-MyVerbose

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Trace response
"[[RESPONSE]]" | Write-MyVerbose
$response | ConvertTo-Json -Depth 100 | Write-MyVerbose

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
if($response.errors){
throw "GraphQL query return errors - Error: $($response.errors.message)"
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
return $response
}
catch {
throw New-Object system.Exception("Error calling GraphQL",$_.Exception)

}
} Export-ModuleMember -Function Invoke-GraphQL

function Invoke-RestAPI {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-RestAPI' does not have a help comment. Note

The cmdlet 'Invoke-RestAPI' does not have a help comment.
param(
[Parameter(Mandatory)][string]$Api,
[Parameter()][string]$Token,
[Parameter()] [string]$ApiHost,
[Parameter()] [string]$PageSize = 30
)

">>>" | Write-MyVerbose

$ApiHost = Get-ApiHost -ApiHost "$ApiHost"
$token = Get-ApiToken -ApiHost "$ApiHost"

try {
$apiHost = "api.$ApiHost"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Define the headers for the request
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$uriBuilder = New-Object System.UriBuilder
$uriBuilder.Scheme = "https"
$uriBuilder.Host = $apiHost
$uriBuilder.Path = $api
$uriBuilder.Query = "?per_page=$PageSize"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$url = $uriBuilder.Uri.AbsoluteUri

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Send the request
$start = Get-Date
">>> Invoke-RestMethod - $url" | Write-MyVerbose
$response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ResponseHeadersVariable responseHeaders

$responseHeaders | Write-MyVerbose

# Process paging
if($responseHeaders.link){$paging=$true}
while ($paging) {
$links = $responseHeaders.link -split ',\s*'

# Find the link with rel="next"
$nextLink = $links | Where-Object { $_ -match 'rel="next"' }
$nextUrl = $nextLink -match '<([^>]+)>; rel="next"'

if($nextUrl){
$nextUrl = $matches[1]
">>> Invoke-RestMethod - $nextUrl" | Write-MyVerbose
$nextResponse = Invoke-RestMethod -Uri $nextUrl -Method Get -Headers $headers -ResponseHeadersVariable responseHeaders
$response += $nextResponse
$paging = $null -ne $responseHeaders.link
} else {
$paging = $false
}
}

"<<< Invoke-RestMethod - $url [ $(((Get-Date) - $start).TotalSeconds) seconds]" | Write-MyVerbose

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Trace response
"[[RESPONSE]]" | Write-MyVerbose
$response | ConvertTo-Json -Depth 100 | Write-MyVerbose


return $response
}
catch {
throw
}
} Export-ModuleMember -Function Invoke-RestAPI

####################################################################################################

$ENV_VAR_HOST_NAME = "GH_HOST"
$DEFAULT_GH_HOST = "github.com"

function Get-ApiHost {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-ApiHost' does not have a help comment. Note

The cmdlet 'Get-ApiHost' does not have a help comment.
[CmdletBinding()]
param(
[Parameter(Position=0)][string]$ApiHost
)

if(![string]::IsNullOrWhiteSpace($ApiHost)){
"ApiHost provided" | Write-MyVerbose
return $ApiHost
}

$envValue = Get-EnvVariable -Name $ENV_VAR_HOST_NAME
if(![string]::IsNullOrWhiteSpace($envValue)){
"Host from env $envValue" | Write-MyVerbose
return $envValue
}

"Default host $DEFAULT_GH_HOST" | Write-MyVerbose
return $DEFAULT_GH_HOST
} Export-ModuleMember -Function Get-ApiHost


####################################################################################################


$ENV_VAR_TOKEN_NAME = "GH_TOKEN"

Set-MyInvokeCommandAlias -Alias GetToken -Command "gh auth token -h {host}"

function Get-ApiToken {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-ApiToken' does not have a help comment. Note

The cmdlet 'Get-ApiToken' does not have a help comment.
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$ApiHost,
[Parameter()] [string]$Token
)

if(![string]::IsNullOrWhiteSpace($Token)){
"Token provided" | Write-TraceApi
return $Token
}

$envValue = Get-EnvVariable -Name $ENV_VAR_TOKEN_NAME
if(![string]::IsNullOrWhiteSpace($envValue)){
"Token from env" | Write-MyVerbose
return $envValue
}

$params = @{
host = $ApiHost
}

"Token from GetToken for host [$ApiHost]" | Write-MyVerbose
$result = Invoke-MyCommand -Command "GetToken" -Parameters $params

if(-Not $result){
throw "No Api token found"
}

return $result
} Export-ModuleMember -Function Get-ApiToken

####################################################################################################

function Get-EnvVariable{
param(
[Parameter(Mandatory)][string]$Name
)

if(! (Test-Path -Path "Env:$Name") ){
return $null
}

$ret = "Env:$Name"

return $ret
}

15 changes: 15 additions & 0 deletions public/driver/invoke-getnode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function Invoke-GetNode {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-GetNode' does not have a help comment. Note

The cmdlet 'Invoke-GetNode' does not have a help comment.
param(
[Parameter(Mandatory=$true)][string]$id
)

$query = Get-GraphQLString "node.query"

$variables = @{
itemId = $id
}

$response = Invoke-GraphQL -Query $query -Variables $variables

return $response
} Export-ModuleMember -Function Invoke-GetNode
Loading