Rest api with powershell » History » Version 3
Seung Soo Mun, 2020-10-19 18:05
| 1 | 1 | Seung Soo Mun | h1. Using the REST API with Powershell |
|---|---|---|---|
| 2 | |||
| 3 | 3 | Seung Soo Mun | h2. Powershell |
| 4 | 1 | Seung Soo Mun | |
| 5 | 3 | Seung Soo Mun | "Invoke-RestMethod":https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod cmdlet |
| 6 | |||
| 7 | 1 | Seung Soo Mun | <pre> |
| 8 | $Cred = Get-Credential |
||
| 9 | |||
| 10 | Invoke-RestMethod http://demo.redmine.org/issues/12345.json -Credential $Cred |
||
| 11 | Invoke-RestMethod http://demo.redmine.org/projects/12.json -Credential $Cred |
||
| 12 | Invoke-RestMethod http://demo.redmine.org/versions/123.json -Credential $Cred |
||
| 13 | |||
| 14 | (Invoke-RestMethod http://demo.redmine.org/issues.json -Credential $Cred).issues |
||
| 15 | (Invoke-RestMethod http://demo.redmine.org/projects.json -Credential $Cred).projects |
||
| 16 | (Invoke-RestMethod http://demo.redmine.org/projects/12/versions.json -Credential $Cred).versions |
||
| 17 | |||
| 18 | Invoke-RestMethod http://demo.redmine.org/projects/12.json -Credential $Cred -Method PUT -ContentType 'application/json' -Body '{"project": {"default_version_id": "398"}}' |
||
| 19 | Invoke-RestMethod http://demo.redmine.org/projects/testproject.json -Credential $Cred -Method PUT -ContentType 'application/json' -Body '{"project": {"default_version_id": "398"}}' |
||
| 20 | 2 | Seung Soo Mun | |
| 21 | 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"}}' |
||
| 22 | 1 | Seung Soo Mun | 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]}}' |
| 23 | </pre> |
||
| 24 | |||
| 25 | 3 | Seung Soo Mun | h2. using PSRedmine module |
| 26 | 1 | Seung Soo Mun | |
| 27 | https://github.com/hamletmun/PSRedmine |
||
| 28 | |||
| 29 | 3 | Seung Soo Mun | {{collapse(Example) |
| 30 | 1 | Seung Soo Mun | <pre> |
| 31 | Connect-Redmine demo.redmine.org |
||
| 32 | |||
| 33 | New-RedmineResource project -identifier test99 -name testproject |
||
| 34 | New-RedmineResource version -project_id 475 -name testversion |
||
| 35 | New-RedmineResource issue -project_id test99 -subject testissue |
||
| 36 | |||
| 37 | Search-RedmineResource project -keyword testproject |
||
| 38 | Search-RedmineResource membership -project_id test99 |
||
| 39 | Search-RedmineResource version -project_id test99 -keyword testversion |
||
| 40 | Search-RedmineResource issue -keyword testissue |
||
| 41 | Search-RedmineResource user -keyword testuser # Administrator only |
||
| 42 | |||
| 43 | Get-RedmineResource project test99 |
||
| 44 | Get-RedmineResource project 475 |
||
| 45 | Get-RedmineResource membership 74 |
||
| 46 | Get-RedmineResource version 408 |
||
| 47 | Get-RedmineResource issue 29552 |
||
| 48 | Get-RedmineResource user 20 # Administrator only |
||
| 49 | |||
| 50 | Edit-RedmineResource project -id test99 -description 'change description' |
||
| 51 | Edit-RedmineResource version -id 408 -description 'add desc' -due_date 2018-09-29 |
||
| 52 | Edit-RedmineResource issue -id 29552 -version_id 406 |
||
| 53 | |||
| 54 | Remove-RedmineResource issue 29552 |
||
| 55 | Remove-RedmineResource version 408 |
||
| 56 | Remove-RedmineResource project test99 # Administrator only |
||
| 57 | Remove-RedmineResource user 20 # Administrator only |
||
| 58 | |||
| 59 | Disconnect-Redmine |
||
| 60 | </pre> |
||
| 61 | 3 | Seung Soo Mun | }} |
| 62 | |||
| 63 | h2. using Redmine-net-api dll |
||
| 64 | |||
| 65 | https://github.com/zapadi/redmine-net-api |
||
| 66 | |||
| 67 | {{collapse(Example) |
||
| 68 | <pre> |
||
| 69 | [Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\Newtonsoft.Json.dll") |
||
| 70 | [Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\redmine-net-api.dll") |
||
| 71 | |||
| 72 | $Redmine = [Redmine.Net.Api.RedmineManager]::new('http://demo.redmine.org', 'a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0') |
||
| 73 | $Redmine.GetCurrentUser() |
||
| 74 | |||
| 75 | [System.Collections.Specialized.NameValueCollection]$params |
||
| 76 | |||
| 77 | Function Get-RedmineResource ($type,$id) { |
||
| 78 | $id = $id -as [String] |
||
| 79 | $resource = $Redmine.GetType().GetMethod("GetObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$params)) |
||
| 80 | $resource |
||
| 81 | } |
||
| 82 | |||
| 83 | Function Edit-RedmineResource ($type,$id,$description) { |
||
| 84 | $id = $id -as [String] |
||
| 85 | $resource = Get-RedmineResource $type $id |
||
| 86 | $resource.description = $description |
||
| 87 | ($Redmine.GetType().GetMethods() | where {$_.Name -eq "UpdateObject" -and $_.IsGenericMethod})[0].MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$resource)) |
||
| 88 | } |
||
| 89 | |||
| 90 | Function Remove-RedmineResource ($type,$id) { |
||
| 91 | $id = $id -as [String] |
||
| 92 | $Redmine.GetType().GetMethod("DeleteObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$null)) |
||
| 93 | } |
||
| 94 | |||
| 95 | Edit-RedmineResource Issue 39773 -description 'Testing redmine-api 2' |
||
| 96 | |||
| 97 | Get-RedmineResource Issue 39773 |
||
| 98 | |||
| 99 | Remove-RedmineResource Issue 39773 |
||
| 100 | </pre> |
||
| 101 | }} |