Project

General

Profile

Rest api with powershell » History » Revision 9

Revision 8 (Seung Soo Mun, 2022-03-10 20:54) → Revision 9/10 (Seung Soo Mun, 2022-04-02 03:34)

h1. Using the REST API with Powershell 

 h2. Powershell 

 "Invoke-RestMethod":https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod cmdlet 

 <pre> 
 Invoke-RestMethod http://demo.redmine.org/issues/12345.json?key=a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0 
 Invoke-RestMethod http://demo.redmine.org/projects/12.json?key=a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0 
 Invoke-RestMethod http://demo.redmine.org/versions/123.json?key=a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0 

 Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/issues/12345.json 
 Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/projects/12.json 
 Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/versions/123.json 

 # Attach a file to an issue 

 Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} -Method POST -ContentType 'application/octet-stream' -Infile C:\image.jpg http://demo.redmine.org/uploads.json?filename=image.jpg 

 upload 
 ------ 
 @{id=234567; token=234567.a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0a1b2} 

 Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} -Method PUT -ContentType 'application/json' -Body '{"issue":{"uploads":[{"token": "234567.a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0a1b2", "filename": "image.jpg", "content_type": "image/jpg"}]}}' http://demo.redmine.org/issues/123456.json 
 Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/issues/123456.json?include=attachments | ConvertTo-Json 

 $Cred = Get-Credential 

 Invoke-RestMethod http://demo.redmine.org/issues/12345.json -Credential $Cred 
 Invoke-RestMethod http://demo.redmine.org/projects/12.json -Credential $Cred 
 Invoke-RestMethod http://demo.redmine.org/versions/123.json -Credential $Cred 

 (Invoke-RestMethod http://demo.redmine.org/issues.json -Credential $Cred).issues 
 (Invoke-RestMethod http://demo.redmine.org/projects.json -Credential $Cred).projects 
 (Invoke-RestMethod http://demo.redmine.org/projects/12/versions.json -Credential $Cred).versions 

 Invoke-RestMethod http://demo.redmine.org/projects/12.json -Credential $Cred -Method PUT -ContentType 'application/json' -Body '{"project": {"default_version_id": "398"}}' 
 Invoke-RestMethod http://demo.redmine.org/projects/testproject.json -Credential $Cred -Method PUT -ContentType 'application/json' -Body '{"project": {"default_version_id": "398"}}' 

 Invoke-RestMethod http://demo.redmine.org/projects/12/versions.json -Credential $Cred -Method POST -ContentType 'application/json' -Body '{"version": {"name": "Test ver", "description": "Test version desc"}}' 
 Invoke-RestMethod http://demo.redmine.org/issues.json -Credential $Cred -Method POST -ContentType 'application/json' -Body '{"issue": {"project_id": 438, "subject": "test watchers", "watcher_user_ids": [7,11,110]}}' 
 </pre> 

 h2. using PSRedmine module 

 https://github.com/hamletmun/PSRedmine 

 {{collapse(Example) 
 <pre> 
 Connect-Redmine demo.redmine.org 

 New-RedmineResource project -identifier test99 -name testproject 
 New-RedmineResource version -project_id 475 -name testversion 
 New-RedmineResource issue -project_id test99 -subject testissue 

 Search-RedmineResource project -keyword testproject 
 Search-RedmineResource membership -project_id test99 
 Search-RedmineResource version -project_id test99 -keyword testversion  
 Search-RedmineResource issue -keyword testissue 
 Search-RedmineResource user -keyword testuser # Administrator only 

 Get-RedmineResource project test99 
 Get-RedmineResource project 475 
 Get-RedmineResource membership 74 
 Get-RedmineResource version 408 
 Get-RedmineResource issue 29552 
 Get-RedmineResource user 20 # Administrator only 

 Edit-RedmineResource project -id test99 -description 'change description' 
 Edit-RedmineResource version -id 408 -description 'add desc' -due_date 2018-09-29 
 Edit-RedmineResource issue -id 29552 -version_id 406 

 Remove-RedmineResource issue 29552 
 Remove-RedmineResource version 408 
 Remove-RedmineResource project test99 # Administrator only 
 Remove-RedmineResource user 20 # Administrator only 

 Disconnect-Redmine 
 </pre> 
 }} 

 h2. using Redmine-net-api dll 

 https://github.com/JamesNK/Newtonsoft.Json 
 https://github.com/zapadi/redmine-net-api 

 {{collapse(Example) 
 <pre> 
 [Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\Newtonsoft.Json.dll") 
 [Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\redmine-net-api.dll") 

 $Redmine = [Redmine.Net.Api.RedmineManager]::new('http://demo.redmine.org', 'a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0') 
 $Redmine.GetCurrentUser() 

 [System.Collections.Specialized.NameValueCollection]$params 

 Function Get-RedmineResource ($type,$id) { 
     $id = $id -as [String] 
     $Redmine.GetType().GetMethod("GetObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$params)) 
 } 

 Function Edit-RedmineResource ($type,$id,$description) { 
     $id = $id -as [String] 
     $resource = Get-RedmineResource $type $id 
     $resource.description = $description 
     ($Redmine.GetType().GetMethods() | where {$_.Name -eq "UpdateObject" -and $_.IsGenericMethod})[0].MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$resource)) 
 } 

 Function Remove-RedmineResource ($type,$id) { 
     $id = $id -as [String] 
     $Redmine.GetType().GetMethod("DeleteObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$null)) 
 } 

 Function New-IdentifiableName ($id) { 
     $IdentifiableName = New-Object -TypeName Redmine.Net.Api.Types.IdentifiableName 
     $IdentifiableName.GetType().GetMethod("Create").MakeGenericMethod("Redmine.Net.Api.Types.IdentifiableName").Invoke($IdentifiableName, @($id -as [Int])) 
 } 

 Function New-RedmineResource { 
     Param( 
         [Parameter(Mandatory=$true)][String]$type, 
         [String]$project_id, 
         [String]$identifier, 
         [String]$name, 
         [String]$description, 
         [Int]$default_version_id, 
         [Int]$issue_id, 
         [Int]$tracker_id, 
         [String]$status_id, 
         [Int]$version_id, 
         [String]$subject, 
         [String]$notes, 
         [Datetime]$due_date, 
         [String]$status, 
         [String]$sharing 
     ) 
     $hash = @{} 

     foreach ($boundparam in $PSBoundParameters.GetEnumerator()) { 
         Switch ($boundparam.Key) { 
             'type' { continue } 
             'project_id' { $hash.Project = New-IdentifiableName $boundparam.Value } 
             'tracker_id' { $hash.Tracker = New-IdentifiableName $boundparam.Value } 
             'status_id' { $hash.Status = New-IdentifiableName $boundparam.Value } 
             'version_id' { $hash.Version = New-IdentifiableName $boundparam.Value } 
             default { $hash.$($boundparam.Key) = $boundparam.Value } 
         } 
     } 
     #$hash = @{ Name = $name; Identifier = $name; Description = 'Test' } 
     $resource = New-Object -TypeName Redmine.Net.Api.Types.$type -Property $hash 
     ($Redmine.GetType().GetMethods() | where {$_.Name -eq "CreateObject" -and $_.IsGenericMethod})[0].MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($resource -as ("Redmine.Net.Api.Types.$type" -as [type]))) 
 } 

 $Project = New-RedmineResource Project -Identifier 'test_api' -Name 'test_api' -Description 'Testing Redmine-API' 
 $Issue = New-RedmineResource Issue -project_id $Project.Id -Subject 'test_api' 

 Get-RedmineResource Issue $Issue.Id 

 Edit-RedmineResource Issue $Issue.Id -Description 'Testing Redmine-API' 
 Get-RedmineResource Issue $Issue.Id 

 Remove-RedmineResource Issue $Issue.Id 
 </pre> 
 }}