Last active
February 3, 2020 11:53
-
-
Save btodts/4f404a6000e5e9ed5ecf4583f247df97 to your computer and use it in GitHub Desktop.
CreateCRGitLabCloudflare.ps1
This file contains 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
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$Description, | |
[Parameter(Mandatory = $True)] | |
[string]$Subject, | |
[Parameter(Mandatory = $True)] | |
[string]$JiraAuthKey | |
) | |
$DueDate = (Get-Date).ToString("yyyy-MM-dd'T'HH:mm:ss'.000+0100'") | |
$Project = 'MHCH' | |
$SolvingGroup = 'Webops' | |
$IssueType = "Change Type 2" | |
$Reporter = "svc_gitlab_jira@core.local" | |
$Issue = New-JiraIssue -AuthKey $JiraAuthKey ` | |
-SolvingGroup $SolvingGroup ` | |
-Project $Project ` | |
-IssueType $IssueType ` | |
-Description $($Description | Out-String | ConvertTo-Json) ` | |
-Summary $Subject ` | |
-Reporter $Reporter ` | |
-DueDate $DueDate ` | |
-Acc $true | |
Resolve-JiraIssue -AuthKey $JiraAuthKey ` | |
-IssueId $Issue.Key ` | |
-Acc $true | |
function ConvertTo-Base64($string) { | |
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string); | |
$encoded = [System.Convert]::ToBase64String($bytes); | |
return $encoded; | |
} | |
function ConvertTo-JiraBase64 { | |
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$username, | |
[Parameter(Mandatory = $True)] | |
[string]$pw | |
) | |
$b64 = ConvertTo-Base64($username + ":" + $pw); | |
$auth = "Basic " + $b64; | |
$auth | |
} | |
function Invoke-JiraApi { | |
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$AuthKey, | |
[Parameter(Mandatory = $True)] | |
[string]$Method, | |
[Parameter(Mandatory = $True)] | |
[string]$Uri, | |
[String]$Body | |
) | |
if (($Method -like 'Post' -and $Body -like '') -or ($Method -like 'Put' -and $Body -like '')) { | |
Write-Host "Unable to $($Method) body to jira --> Body is empty" | |
Exit | |
} | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } | |
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($Uri) | |
$auth = $AuthKey | |
$webRequest = [System.Net.WebRequest]::Create($Uri) | |
$webRequest.ContentType = "application/json" | |
$webRequest.ServicePoint.Expect100Continue = $false | |
$webRequest.Headers.Add("Authorization", $auth) | |
$webRequest.PreAuthenticate = $true | |
$webRequest.Method = $Method | |
if (($Method -like 'Post') -or ($Method -like 'Put')) { | |
$BodyStr = [System.Text.Encoding]::UTF8.GetBytes($body) | |
$webrequest.ContentLength = $BodyStr.Length | |
$requestStream = $webRequest.GetRequestStream() | |
$requestStream.Write($BodyStr, 0, $BodyStr.length) | |
$requestStream.Flush() | |
$requestStream.Close() | |
} | |
[System.Net.WebResponse] $resp = $webRequest.GetResponse() | |
if ($null -eq $resp) { | |
write-host "No valid response from Api" | |
exit | |
} | |
$rs = $resp.GetResponseStream() | |
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs | |
[string] $results = $sr.ReadToEnd() | |
$ServicePoint.CloseConnectionGroup("") | |
$output = ConvertFrom-Json -InputObject $results | |
$output | |
} | |
function Get-JiraIssue { | |
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$AuthKey, | |
[Parameter(Mandatory = $True)] | |
[string]$SolvingGroup, | |
[Parameter(Mandatory = $True)] | |
[string]$Project, | |
[string]$Status, | |
[string]$Assignee, | |
[string]$GetDays | |
) | |
$rawtarget = "https://jira.tmg.nl/rest/api/latest/search?jql=project = $($Project) AND ""Solving Group"" = ""$($SolvingGroup)""" | |
if ($Assignee -eq 'Unassigned') { | |
$rawtarget = "$rawtarget AND assignee in (EMPTY)" | |
} | |
elseif ($Assignee -eq 'Assigned') { | |
$rawtarget = "$rawtarget AND assignee = ""$Assignee""" | |
} | |
if ($Status) { | |
$rawtarget = "$rawtarget AND status in (" + $Status + ")" | |
} | |
if ($GetDays) { | |
$rawtarget = "$rawtarget AND created >= -" + $GetDays + "d" | |
} | |
$target = [uri]::EscapeUriString($rawtarget) | |
if ($SolvingGroup -match ' - ') { | |
$target = $target -replace '%20-%20', '%20%E2%80%93%20' | |
} | |
$output = Invoke-JiraApi -AuthKey $AuthKey ` | |
-Method 'Get' ` | |
-Uri $target | |
foreach ($issue in $output.issues) { | |
$JiraContent = [Array]$JiraContent + $(New-Object -TypeName PSObject -Property @{ | |
key = $issue.key | |
LastUpdated = $issue.fields.updated | |
summary = $issue.fields.summary | |
}) | |
} | |
Write-Output $JiraContent | |
} | |
function Get-JiraId { | |
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$AuthKey, | |
[Parameter(Mandatory = $True)] | |
[string]$SolvingGroup, | |
[Parameter(Mandatory = $True)] | |
[string]$Project, | |
[Parameter(Mandatory = $True)] | |
[string]$IssueType | |
) | |
if ($SolvingGroup -match ' - ') { | |
Write-Warning "Unable to resolve Solving group id, due to an unknown dash ('-') in the provided Solving group name" | |
Write-Warning "Aborting script" | |
Exit | |
} | |
else { | |
$Uri = "https://jira.tmg.nl/rest/api/2/issue/createmeta?projectKeys=$($project)&expand=projects.issuetypes.fields" | |
$output = Invoke-JiraApi -AuthKey $AuthKey ` | |
-Method 'Get' ` | |
-Uri $Uri | |
$ProjectId = ($output.projects | where-object key -like $project).id | |
$IssueTypeInfo = $output.projects.issuetypes | Where-Object Name -like $IssueType | |
$IssueTypeId = $IssueTypeInfo.ID | |
$CustomFields = ($IssueTypeInfo).fields | |
$AllowedValues = $CustomFields.customfield_10301.allowedValues | Select-object Value, ID | |
$SolvingGroupId = ($AllowedValues | where-object value -like $SolvingGroup).ID | |
$JiraInfo = @() | |
$JiraInfo += New-Object -TypeName psobject -Property @{ | |
ProjectID = $ProjectId; | |
IssueTypeID = $IssueTypeId; | |
SolvingGroupID = $SolvingGroupId | |
} | |
$JiraInfo | |
} | |
} | |
function New-JiraIssue { | |
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$AuthKey, | |
[Parameter(Mandatory = $True)] | |
[string]$SolvingGroup, | |
[String]$Assignee, | |
[Parameter(Mandatory = $True)] | |
[string]$Project, | |
[Parameter(Mandatory = $True)] | |
[string]$IssueType, | |
[Parameter(Mandatory = $True)] | |
[String]$Summary, | |
[Parameter(Mandatory = $True)] | |
[String]$Description, | |
[Parameter(Mandatory = $True)] | |
[String]$Reporter, | |
[Parameter(Mandatory = $True)] | |
[String]$DueDate, | |
$Acc | |
) | |
#Acc? | |
if ($Acc -eq $true) { | |
$Uri = 'https://jira-acc.tmg.nl/rest/api/2/issue/' | |
} | |
else { | |
$Uri = 'https://jira.tmg.nl/rest/api/2/issue/' | |
} | |
#Get JiraID's | |
$JiraIDs = Get-JiraId -AuthKey $AuthKey ` | |
-SolvingGroup $SolvingGroup ` | |
-IssueType $IssueType ` | |
-Project $Project | |
if (!(($Project -eq "MHCH") -and ($IssueType -like "Change Type 2"))) { | |
Write-Warning "Unable to create Jira issue: Currently this Jira Module only supports the creation of issues for project 'MHCH' and type 'Change Type 2'" | |
Exit | |
} | |
else { | |
<# if ($description -notlike '"*"') { | |
$Description = "`"$($description)`"" | |
} #> | |
#Create Jira Issue body | |
$body = ' | |
{ | |
"fields": { | |
"project": { | |
"id": "' + $JiraIDs.ProjectID + '" | |
}, | |
"summary": "' + $Summary + '", | |
"description": ' + $Description + ', | |
"issuetype": { | |
"id": "' + $JiraIDs.IssueTypeId + '" | |
}, | |
"reporter": { | |
"name": "' + $Reporter + '" | |
}, | |
"customfield_10301": { | |
"id": "' + $JiraIDs.SolvingGroupId + '" | |
}, | |
"customfield_12600": "' + $DueDate + '" | |
} | |
}' | |
#troubleshooting | |
<# $body | Out-File -Filepath 'C:\temp\body_Json.txt' | |
$AuthKey | Out-File -Filepath 'C:\temp\autkey.txt' | |
$Uri | Out-File -Filepath 'C:\temp\Uri.txt' #> | |
#Create Jira Issue | |
$output = Invoke-JiraApi -AuthKey $AuthKey ` | |
-Method 'Post' ` | |
-Uri $Uri ` | |
-Body $body | |
#(Un)assign issue to Assignee | |
if (!$Assignee) { | |
$Assignee = "-1" | |
$body = ' | |
{ | |
"fields": { | |
"assignee": { | |
"name": "' + $Assignee + '" | |
} | |
} | |
}' | |
Invoke-JiraApi -AuthKey $AuthKey ` | |
-Method 'Put' ` | |
-Uri "$Uri$($output.key)" ` | |
-Body $body | |
} | |
Write-Output $output | |
} | |
} | |
function Resolve-JiraIssue { | |
param ( | |
[Parameter(Mandatory = $True)] | |
[string]$AuthKey, | |
[Parameter(Mandatory = $True)] | |
[string]$IssueID, | |
$Acc | |
) | |
#Acc? | |
if ($Acc -eq $true) { | |
$Uri = "https://jira-acc.tmg.nl/rest/api/2/issue/$($IssueID)/transitions?expand=transitions.fields" | |
} | |
else { | |
$Uri = "https://jira.tmg.nl/rest/api/2/issue/$($IssueID)/transitions?expand=transitions.fields" | |
} | |
if (!($IssueID -like 'MHCH-*')) { | |
Write-Host "For this project Resolve-JiraIssue is not supported yet!" | |
Exit | |
} | |
else { | |
$TransitionsMHCH = @() | |
$TransitionsMHCH += New-Object -TypeName psobject -Property @{ | |
Name = "Under Development"; | |
Id = "181"; | |
SleepSeconds = "0" | |
} | |
$TransitionsMHCH += New-Object -TypeName psobject -Property @{ | |
name = "Implementation"; | |
Id = "171"; | |
SleepSeconds = "2" | |
} | |
$TransitionsMHCH += New-Object -TypeName psobject -Property @{ | |
name = "Waiting for Decharge"; | |
Id = "81"; | |
SleepSeconds = "2" | |
} | |
$TransitionsMHCH += New-Object -TypeName psobject -Property @{ | |
name = "Closed"; | |
Id = "91"; | |
SleepSeconds = "2" | |
} | |
foreach ($Transition in $TransitionsMHCH) { | |
Write-host "Change status to $($Transition.Name) for $($IssueID)" | |
Start-Sleep -Seconds $Transition.SleepSeconds | |
$Body = ' | |
{ | |
"transition": { | |
"id": "' + $Transition.id + '" | |
} | |
}' | |
Write-host "Body = $($Body)" | |
Invoke-JiraApi -AuthKey $AuthKey ` | |
-Method 'Post' ` | |
-Uri $Uri ` | |
-Body $Body | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment