Project

General

Profile

Rest api with powershell » History » Version 8

Seung Soo Mun, 2022-03-10 20:54

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 8 Seung Soo Mun
Invoke-RestMethod http://demo.redmine.org/issues/12345.json?key=a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0
9
Invoke-RestMethod http://demo.redmine.org/projects/12.json?key=a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0
10
Invoke-RestMethod http://demo.redmine.org/versions/123.json?key=a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0
11
12 7 Seung Soo Mun
Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/issues/12345.json
13
Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/projects/12.json
14
Invoke-RestMethod -Headers @{'X-Redmine-API-Key'='a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0'} http://demo.redmine.org/versions/123.json
15
16 1 Seung Soo Mun
$Cred = Get-Credential
17
18
Invoke-RestMethod http://demo.redmine.org/issues/12345.json -Credential $Cred
19
Invoke-RestMethod http://demo.redmine.org/projects/12.json -Credential $Cred
20
Invoke-RestMethod http://demo.redmine.org/versions/123.json -Credential $Cred
21
22
(Invoke-RestMethod http://demo.redmine.org/issues.json -Credential $Cred).issues
23
(Invoke-RestMethod http://demo.redmine.org/projects.json -Credential $Cred).projects
24
(Invoke-RestMethod http://demo.redmine.org/projects/12/versions.json -Credential $Cred).versions
25
26
Invoke-RestMethod http://demo.redmine.org/projects/12.json -Credential $Cred -Method PUT -ContentType 'application/json' -Body '{"project": {"default_version_id": "398"}}'
27
Invoke-RestMethod http://demo.redmine.org/projects/testproject.json -Credential $Cred -Method PUT -ContentType 'application/json' -Body '{"project": {"default_version_id": "398"}}'
28 2 Seung Soo Mun
29
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"}}'
30 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]}}'
31
</pre>
32
33 3 Seung Soo Mun
h2. using PSRedmine module
34 1 Seung Soo Mun
35
https://github.com/hamletmun/PSRedmine
36
37 3 Seung Soo Mun
{{collapse(Example)
38 1 Seung Soo Mun
<pre>
39
Connect-Redmine demo.redmine.org
40
41
New-RedmineResource project -identifier test99 -name testproject
42
New-RedmineResource version -project_id 475 -name testversion
43
New-RedmineResource issue -project_id test99 -subject testissue
44
45
Search-RedmineResource project -keyword testproject
46
Search-RedmineResource membership -project_id test99
47
Search-RedmineResource version -project_id test99 -keyword testversion 
48
Search-RedmineResource issue -keyword testissue
49
Search-RedmineResource user -keyword testuser # Administrator only
50
51
Get-RedmineResource project test99
52
Get-RedmineResource project 475
53
Get-RedmineResource membership 74
54
Get-RedmineResource version 408
55
Get-RedmineResource issue 29552
56
Get-RedmineResource user 20 # Administrator only
57
58
Edit-RedmineResource project -id test99 -description 'change description'
59
Edit-RedmineResource version -id 408 -description 'add desc' -due_date 2018-09-29
60
Edit-RedmineResource issue -id 29552 -version_id 406
61
62
Remove-RedmineResource issue 29552
63
Remove-RedmineResource version 408
64
Remove-RedmineResource project test99 # Administrator only
65
Remove-RedmineResource user 20 # Administrator only
66
67
Disconnect-Redmine
68
</pre>
69 3 Seung Soo Mun
}}
70
71
h2. using Redmine-net-api dll
72
73 4 Seung Soo Mun
https://github.com/JamesNK/Newtonsoft.Json
74 3 Seung Soo Mun
https://github.com/zapadi/redmine-net-api
75
76
{{collapse(Example)
77
<pre>
78
[Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\Newtonsoft.Json.dll")
79
[Reflection.Assembly]::LoadFile("C:\Absolute\Path\to\redmine-net-api.dll")
80
81
$Redmine = [Redmine.Net.Api.RedmineManager]::new('http://demo.redmine.org', 'a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8i9j0')
82
$Redmine.GetCurrentUser()
83
84
[System.Collections.Specialized.NameValueCollection]$params
85
86
Function Get-RedmineResource ($type,$id) {
87
    $id = $id -as [String]
88 6 Seung Soo Mun
    $Redmine.GetType().GetMethod("GetObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$params))
89 3 Seung Soo Mun
}
90
91
Function Edit-RedmineResource ($type,$id,$description) {
92
    $id = $id -as [String]
93
    $resource = Get-RedmineResource $type $id
94
    $resource.description = $description
95
    ($Redmine.GetType().GetMethods() | where {$_.Name -eq "UpdateObject" -and $_.IsGenericMethod})[0].MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$resource))
96
}
97
98
Function Remove-RedmineResource ($type,$id) {
99
    $id = $id -as [String]
100
    $Redmine.GetType().GetMethod("DeleteObject").MakeGenericMethod("Redmine.Net.Api.Types.$type").Invoke($Redmine, @($id,$null))
101
}
102
103 5 Seung Soo Mun
Function New-IdentifiableName ($id) {
104
    $IdentifiableName = New-Object -TypeName Redmine.Net.Api.Types.IdentifiableName
105
    $IdentifiableName.GetType().GetMethod("Create").MakeGenericMethod("Redmine.Net.Api.Types.IdentifiableName").Invoke($IdentifiableName, @($id -as [Int]))
106
}
107 1 Seung Soo Mun
108 5 Seung Soo Mun
Function New-RedmineResource {
109
    Param(
110
        [Parameter(Mandatory=$true)][String]$type,
111
        [String]$project_id,
112
        [String]$identifier,
113
        [String]$name,
114
        [String]$description,
115
        [Int]$default_version_id,
116
        [Int]$issue_id,
117
        [Int]$tracker_id,
118
        [String]$status_id,
119
        [Int]$version_id,
120
        [String]$subject,
121
        [String]$notes,
122
        [Datetime]$due_date,
123
        [String]$status,
124
        [String]$sharing
125
    )
126
    $hash = @{}
127 1 Seung Soo Mun
128 5 Seung Soo Mun
    foreach ($boundparam in $PSBoundParameters.GetEnumerator()) {
129
        Switch ($boundparam.Key) {
130
            'type' { continue }
131
            'project_id' { $hash.Project = New-IdentifiableName $boundparam.Value }
132
            'tracker_id' { $hash.Tracker = New-IdentifiableName $boundparam.Value }
133
            'status_id' { $hash.Status = New-IdentifiableName $boundparam.Value }
134
            'version_id' { $hash.Version = New-IdentifiableName $boundparam.Value }
135
            default { $hash.$($boundparam.Key) = $boundparam.Value }
136
        }
137
    }
138 1 Seung Soo Mun
    #$hash = @{ Name = $name; Identifier = $name; Description = 'Test' }
139
    $resource = New-Object -TypeName Redmine.Net.Api.Types.$type -Property $hash
140 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])))
141 1 Seung Soo Mun
}
142
143 5 Seung Soo Mun
$Project = New-RedmineResource Project -Identifier 'test_api' -Name 'test_api' -Description 'Testing Redmine-API'
144 6 Seung Soo Mun
$Issue = New-RedmineResource Issue -project_id $Project.Id -Subject 'test_api'
145 5 Seung Soo Mun
146 6 Seung Soo Mun
Get-RedmineResource Issue $Issue.Id
147 5 Seung Soo Mun
148 6 Seung Soo Mun
Edit-RedmineResource Issue $Issue.Id -Description 'Testing Redmine-API'
149
Get-RedmineResource Issue $Issue.Id
150 5 Seung Soo Mun
151 6 Seung Soo Mun
Remove-RedmineResource Issue $Issue.Id
152 3 Seung Soo Mun
</pre>
153
}}