Rest api with powershell » History » Version 5
Seung Soo Mun, 2020-10-24 20:36
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 | 4 | Seung Soo Mun | https://github.com/JamesNK/Newtonsoft.Json |
66 | 3 | Seung Soo Mun | https://github.com/zapadi/redmine-net-api |
67 | |||
68 | {{collapse(Example) |
||
69 | <pre> |
||
70 | [Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\Newtonsoft.Json.dll") |
||
71 | [Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\redmine-net-api.dll") |
||
72 | |||
73 | $Redmine = [Redmine.Net.Api.RedmineManager]::new('http://demo.redmine.org', 'a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0') |
||
74 | $Redmine.GetCurrentUser() |
||
75 | |||
76 | [System.Collections.Specialized.NameValueCollection]$params |
||
77 | |||
78 | Function Get-RedmineResource ($type,$id) { |
||
79 | $id = $id -as [String] |
||
80 | $resource = $Redmine.GetType().GetMethod("GetObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$params)) |
||
81 | $resource |
||
82 | } |
||
83 | |||
84 | Function Edit-RedmineResource ($type,$id,$description) { |
||
85 | $id = $id -as [String] |
||
86 | $resource = Get-RedmineResource $type $id |
||
87 | $resource.description = $description |
||
88 | ($Redmine.GetType().GetMethods() | where {$_.Name -eq "UpdateObject" -and $_.IsGenericMethod})[0].MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$resource)) |
||
89 | } |
||
90 | |||
91 | Function Remove-RedmineResource ($type,$id) { |
||
92 | $id = $id -as [String] |
||
93 | $Redmine.GetType().GetMethod("DeleteObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$null)) |
||
94 | } |
||
95 | |||
96 | 5 | Seung Soo Mun | Function New-IdentifiableName ($id) { |
97 | $IdentifiableName = New-Object -TypeName Redmine.Net.Api.Types.IdentifiableName |
||
98 | $IdentifiableName.GetType().GetMethod("Create").MakeGenericMethod("Redmine.Net.Api.Types.IdentifiableName").Invoke($IdentifiableName, @($id -as [Int])) |
||
99 | } |
||
100 | 1 | Seung Soo Mun | |
101 | 5 | Seung Soo Mun | Function New-RedmineResource { |
102 | Param( |
||
103 | [Parameter(Mandatory=$true)][String]$type, |
||
104 | [String]$project_id, |
||
105 | [String]$identifier, |
||
106 | [String]$name, |
||
107 | [String]$description, |
||
108 | [Int]$default_version_id, |
||
109 | [Int]$issue_id, |
||
110 | [Int]$tracker_id, |
||
111 | [String]$status_id, |
||
112 | [Int]$version_id, |
||
113 | [String]$subject, |
||
114 | [String]$notes, |
||
115 | [Datetime]$due_date, |
||
116 | [String]$status, |
||
117 | [String]$sharing |
||
118 | ) |
||
119 | $hash = @{} |
||
120 | 1 | Seung Soo Mun | |
121 | 5 | Seung Soo Mun | foreach ($boundparam in $PSBoundParameters.GetEnumerator()) { |
122 | Switch ($boundparam.Key) { |
||
123 | 'type' { continue } |
||
124 | 'project_id' { $hash.Project = New-IdentifiableName $boundparam.Value } |
||
125 | 'tracker_id' { $hash.Tracker = New-IdentifiableName $boundparam.Value } |
||
126 | 'status_id' { $hash.Status = New-IdentifiableName $boundparam.Value } |
||
127 | 'version_id' { $hash.Version = New-IdentifiableName $boundparam.Value } |
||
128 | default { $hash.$($boundparam.Key) = $boundparam.Value } |
||
129 | } |
||
130 | } |
||
131 | #$hash = @{ Name = $name; Identifier = $name; Description = 'Test' } |
||
132 | $resource = New-Object -TypeName Redmine.Net.Api.Types.$type -Property $hash |
||
133 | $resource |
||
134 | $Response = ($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]))) |
||
135 | $Response |
||
136 | } |
||
137 | |||
138 | $Project = New-RedmineResource Project -Identifier 'test_api' -Name 'test_api' -Description 'Testing Redmine-API' |
||
139 | $Issue = New-RedmineResource Issue -project_id $Project.Id[1] -Subject 'test_api' |
||
140 | |||
141 | Get-RedmineResource Issue $Issue.Id[1] |
||
142 | |||
143 | Edit-RedmineResource Issue $Issue.Id[1] -Description 'Testing Redmine-API' |
||
144 | Get-RedmineResource Issue $Issue.Id[1] |
||
145 | |||
146 | Remove-RedmineResource Issue $Issue.Id[1] |
||
147 | 3 | Seung Soo Mun | </pre> |
148 | }} |