Project

General

Profile

Rest api with powershell » History » Version 7

Seung Soo Mun, 2022-03-08 15:17

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