Project

General

Profile

Rest api with powershell » History » Version 10

Miodrag Milic, 2023-02-22 20:56

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