- "Octopus.Action.Script.ScriptBody": "function Get-OctopusItems\n{\n # Define parameters\n param(\n $OctopusUri,\n $ApiKey,\n $SkipCount = 0\n )\n\n # Define working variables\n $items = @()\n $skipQueryString = \"\"\n $headers = @{\"X-Octopus-ApiKey\"=\"$ApiKey\"}\n\n # Check to see if there there is already a querystring\n if ($octopusUri.Contains(\"?\"))\n {\n $skipQueryString = \"&skip=\"\n }\n else\n {\n $skipQueryString = \"?skip=\"\n }\n\n $skipQueryString += $SkipCount\n\n # Get intial set\n $resultSet = Invoke-RestMethod -Uri \"$($OctopusUri)$skipQueryString\" -Method GET -Headers $headers\n\n # Check to see if it returned an item collection\n if ($resultSet.Items)\n {\n # Store call results\n $items += $resultSet.Items\n\n # Check to see if resultset is bigger than page amount\n if (($resultSet.Items.Count -gt 0) -and ($resultSet.Items.Count -eq $resultSet.ItemsPerPage))\n {\n # Increment skip count\n $SkipCount += $resultSet.ItemsPerPage\n\n # Recurse\n $items += Get-OctopusItems -OctopusUri $OctopusUri -ApiKey $ApiKey -SkipCount $SkipCount\n }\n }\n else\n {\n return $resultSet\n }\n\n\n # Return results\n return ,$items\n}\n\n$automaticResponseOctopusUrl = $OctopusParameters['AutomateResponse.Octopus.Url']\n$automaticResponseApiKey = $OctopusParameters['AutomateResponse.Api.Key']\n$automaticResponseReasonNotes = $OctopusParameters['AutomateResponse.Reason.Notes']\n$automaticResponseManualInterventionResponseType = $OctopusParameters['AutomateResponse.ManualIntervention']\n$automaticResponseGuidedFailureResponseType = $OctopusParameters['AutomateResponse.GuidedFailure']\n$header = @{ \"X-Octopus-ApiKey\" = $automaticResponseApiKey }\n\n# Validate response type input\nif (![string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and ![string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Cannot have both a Manual Intervention and Guided Failure selections.\"\n}\n\nif ([string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and [string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Please select either a Manual Intervention or Guidded Failure response type.\"\n}\n\n# Get space\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n\n# Get project\n$projectId = $OctopusParameters['Octopus.Project.Id']\n\n# Get the environemtn\n$environmentId = $OctopusParameters['Octopus.Environment.Id']\n\n# Get currently executing deployments for project\nWrite-Host \"Searching for executing deployments ...\"\n$executingDeployments = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/deployments?projects=$($projectId)&taskState=Executing&environments=$($environmentId)\" -ApiKey $automaticResponseApiKey\n\n# Check to see if anything was returned for the environment\nif ($executingDeployments -is [array])\n{\n # Loop through executing deployments\n foreach ($deployment in $executingDeployments)\n {\n # Get object for manual intervention\n Write-Host \"Checking $($deployment.Id) for manual interventions ...\"\n $manualIntervention = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions?regarding=$($deployment.Id)&pendingOnly=true\" -ApiKey $automaticResponseApiKey\n\n # Check to see if a manual intervention was returned\n if ($null -ne $manualIntervention.Id)\n {\n # Take responsibility\n Write-Host \"Auto taking resonsibility for manual intervention ...\"\n Invoke-RestMethod -Method Put -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/responsible\" -Headers $header\n\n # Create response object\n $jsonBody = @{\n Notes = $automaticResponseReasonNotes\n }\n\n # Check to see if manual intervention is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseManualInterventionResponseType))\n {\n # Add the manual intervention type\n Write-Host \"Submitting $automaticResponseManualInterventionResponseType as response ...\"\n $jsonBody.Add(\"Result\", $automaticResponseManualInterventionResponseType)\n }\n\n # Check to see if the guided failure is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseGuidedFailureResponseType))\n {\n # Add the guided failure response\n Write-Host \"Submitting $automaticResponseGuidedFailureResponseType as response ...\"\n $jsonBody.Add(\"Guidance\", $automaticResponseGuidedFailureResponseType)\n }\n\n # Post to server\n Invoke-RestMethod -Method Post -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/submit\" -Body ($jsonBody | ConvertTo-Json -Depth 10) -Headers $header\n }\n }\n}"
0 commit comments