Last active
August 29, 2015 14:12
-
-
Save jeriley/79818b46a0385980a855 to your computer and use it in GitHub Desktop.
V1Api example : powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $username = "admin" | |
| $password = "admin" | |
| $instance = "http://localhost/VersionOne.Web/" | |
| $base64ed = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($($username + ":" + $password))) | |
| $storyId = "S-01001" | |
| $story = GetStoryByID $storyId | |
| UpdateAsset $story "<Asset><Attribute name='Description' act='set'>Descript Insert!</Attribute></Asset>" | |
| #disabled because this will update a pile of stories -- be careful! | |
| #$stories = GetStoriesAssignedtoProjectIteration "New Project" "Iteration 1" | |
| #SetStoryEstimatesTo1 $stories | |
| # if you get a script error, run the first script, just make sure to run the 2nd when you're done (security!) | |
| # Set-ExecutionPolicy Unrestricted -Scope CurrentUser | |
| # Set-ExecutionPolicy AllSigned -Scope CurrentUser | |
| function SetStoryEstimatesTo1(){ | |
| param([array] $stories) | |
| $postData = "<Asset><Attribute name='Estimate' act='set'>1</Attribute></Asset>" | |
| $stories | % { UpdateAsset $_ $postData } | |
| } | |
| function GetStoriesAssignedtoProjectIteration(){ | |
| param([string]$scope, [string]$iteration) | |
| $endpoint = "rest-1.v1/Data/Story?sel=Name&where=Scope.Name='" + $scope + "';Timebox.Name='" + $iteration + "'" | |
| $xml = [xml](GetRequest $endpoint) | |
| $stories = @() | |
| $xml.Assets.Asset | % {$stories += $_.id.replace(":","/")} | |
| Write-Host "Stories To Update :" $stories | |
| return $stories | |
| } | |
| function UpdateAsset(){ | |
| param([string] $assetTypeAndId, [string] $post) | |
| $endpoint = "rest-1.v1/Data/" + $assetTypeAndId | |
| Write-Host $endpoint | |
| Write-Host $post | |
| $webResponse = POSTRequest $endpoint $post | |
| $web | |
| } | |
| function GetStoryByID(){ | |
| param([string] $storyId) | |
| #note: inside the system, an ID is Story:1037 but the "ID" displayed on the grids (ID.Number) is S-01574 | |
| $endpoint = "rest-1.v1/Data/Story?sel=ID.Number&where=AssetState!='Closed';ID.Number='" + $storyId + "'" | |
| $response = GetRequest $endpoint | |
| $xml = [xml]$response | |
| if ($xml.Assets.total -eq "0"){ | |
| Write-Host "no story found!" | |
| Exit | |
| } | |
| $storyId = $xml.Assets.Asset.id.replace(":","/") | |
| Write-Host "Story To Update :" $storyId | |
| return $storyId | |
| } | |
| function GetRequest(){ | |
| param([string] $endpoint) | |
| $versionOneInstance = $instance + $endpoint | |
| Write-Host $versionOneInstance | |
| $webRequest = [System.Net.WebRequest]::Create($versionOneInstance) | |
| $webRequest.ContentType = "application/xml"; | |
| $webRequest.Headers.Add("Authorization", "Basic " + $base64ed) | |
| $webRequest.Method = "GET" | |
| [System.Net.WebResponse] $resp = $webRequest.GetResponse(); | |
| $rs = $resp.GetResponseStream(); | |
| [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs; | |
| [string] $results = $sr.ReadToEnd(); | |
| #Write-Host $results | |
| return $results | |
| } | |
| function POSTRequest(){ | |
| param([string] $endpoint, [string] $post) | |
| $versionOneInstance = $instance + $endpoint | |
| Write-Host $versioneOneInstance | |
| $postData = [System.Text.Encoding]::UTF8.GetBytes($post) | |
| $webRequest = [System.Net.WebRequest]::Create($versionOneInstance) | |
| $webRequest.ContentType = "application/xml"; | |
| $webRequest.Headers.Add("Authorization", "Basic " + $base64ed) | |
| $webRequest.Method = "POST" | |
| $webRequest.ContentLength = $postdata.Length | |
| $requestStream = $webRequest.GetRequestStream() | |
| $requestStream.Write($postData, 0,$postData.length) | |
| $requestStream.Close() | |
| [System.Net.WebResponse] $response = $webRequest.GetResponse(); | |
| Write-Host ([int]$response.StatusCode) $response.StatusCode | |
| $responseStream = $response.GetResponseStream(); | |
| [System.IO.StreamReader] $streamReader = New-Object System.IO.StreamReader -argumentList $responseStream; | |
| [string] $results = $streamReader.ReadToEnd(); | |
| Write-Host $results | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment