Rest api with powershell » History » Version 6
Seung Soo Mun, 2020-10-24 20:57
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 | 6 | Seung Soo Mun | $Redmine.GetType().GetMethod("GetObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$params)) |
81 | 3 | Seung Soo Mun | } |
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 | 5 | Seung Soo Mun | Function New-IdentifiableName ($id) { |
96 | $IdentifiableName = New-Object -TypeName Redmine.Net.Api.Types.IdentifiableName |
||
97 | $IdentifiableName.GetType().GetMethod("Create").MakeGenericMethod("Redmine.Net.Api.Types.IdentifiableName").Invoke($IdentifiableName, @($id -as [Int])) |
||
98 | } |
||
99 | 1 | Seung Soo Mun | |
100 | 5 | Seung Soo Mun | Function New-RedmineResource { |
101 | Param( |
||
102 | [Parameter(Mandatory=$true)][String]$type, |
||
103 | [String]$project_id, |
||
104 | [String]$identifier, |
||
105 | [String]$name, |
||
106 | [String]$description, |
||
107 | [Int]$default_version_id, |
||
108 | [Int]$issue_id, |
||
109 | [Int]$tracker_id, |
||
110 | [String]$status_id, |
||
111 | [Int]$version_id, |
||
112 | [String]$subject, |
||
113 | [String]$notes, |
||
114 | [Datetime]$due_date, |
||
115 | [String]$status, |
||
116 | [String]$sharing |
||
117 | ) |
||
118 | $hash = @{} |
||
119 | 1 | Seung Soo Mun | |
120 | 5 | Seung Soo Mun | foreach ($boundparam in $PSBoundParameters.GetEnumerator()) { |
121 | Switch ($boundparam.Key) { |
||
122 | 'type' { continue } |
||
123 | 'project_id' { $hash.Project = New-IdentifiableName $boundparam.Value } |
||
124 | 'tracker_id' { $hash.Tracker = New-IdentifiableName $boundparam.Value } |
||
125 | 'status_id' { $hash.Status = New-IdentifiableName $boundparam.Value } |
||
126 | 'version_id' { $hash.Version = New-IdentifiableName $boundparam.Value } |
||
127 | default { $hash.$($boundparam.Key) = $boundparam.Value } |
||
128 | } |
||
129 | } |
||
130 | 1 | Seung Soo Mun | #$hash = @{ Name = $name; Identifier = $name; Description = 'Test' } |
131 | $resource = New-Object -TypeName Redmine.Net.Api.Types.$type -Property $hash |
||
132 | 6 | Seung Soo Mun | ($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]))) |
133 | 1 | Seung Soo Mun | } |
134 | |||
135 | 5 | Seung Soo Mun | $Project = New-RedmineResource Project -Identifier 'test_api' -Name 'test_api' -Description 'Testing Redmine-API' |
136 | 6 | Seung Soo Mun | $Issue = New-RedmineResource Issue -project_id $Project.Id -Subject 'test_api' |
137 | 5 | Seung Soo Mun | |
138 | 6 | Seung Soo Mun | Get-RedmineResource Issue $Issue.Id |
139 | 5 | Seung Soo Mun | |
140 | 6 | Seung Soo Mun | Edit-RedmineResource Issue $Issue.Id -Description 'Testing Redmine-API' |
141 | Get-RedmineResource Issue $Issue.Id |
||
142 | 5 | Seung Soo Mun | |
143 | 6 | Seung Soo Mun | Remove-RedmineResource Issue $Issue.Id |
144 | 3 | Seung Soo Mun | </pre> |
145 | }} |