Project

General

Profile

Rest api with powershell » History » Version 4

Seung Soo Mun, 2020-10-19 18:24

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
Edit-RedmineResource Issue 39773 -description 'Testing redmine-api 2'
97
98
Get-RedmineResource Issue 39773
99
100
Remove-RedmineResource Issue 39773
101
</pre>
102
}}