Atlassian Jira Automation Scripts
These PowerShell scripts allow for automation of ticket creation, adding a comment to a ticket and creating actions based of a comment in a ticket using the Atlassian REST API. These scripts can be implemented into new and existing projects to assist with converting manual work to automation e.g. logging of activities or automating responses.
Before running the scripts below, you will need to change the variables to match yours:
- Jira Domain: this is listed in the section on the Jira URL e.g.
https://your-domain.atlassian.net
- Jira Project ID: enter
https://your-doman.atlassian.net/rest/api/3/issues/createmeta
, search for the project ID this will be location the ticket will be created, make a note of the project ID. - Jira Issue Type ID: using the project ID enter
https://your-domain.atlassian.net/rest/api/3/isues/createmeta?projectKeys={JiraProjectID}
search for the issue type ID that you want the ticket to be created in, make a note of the issue type ID. - Jira Username: username that can login to Atlassian and has the correct permissions.
- Jira API token: API token can be created here.
Do not share this key and keep it in a secure location.
You may be required to add mandatory custom fields for tickets to be created, the information can be found here:
https://your-domain.atlassian.net/rest/api/3/isues/createmeta?projectKeys={JiraProjectID}&expand=projects.issuetypes.fields
Creating Jira Ticket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Define your Jira URL and API endpoint
$jiraDomain = "domain.atlassian.net"
$jiraUrl = "https://$jiraDomain/rest/api/2/issue"
$jiraProjectId = "00001"
$jiraIssueTypeId = "00002"
# Define your Jira credentials (API token or password)
$jiraUsername = "jdoe@domain.com"
$jiraApiToken = "API token goes here" # Replace this with your Jira API token
# Prepare the authentication header (Basic Authentication)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${jiraUsername}:${jiraApiToken}"))
# Define the issue data
$body = @{
"fields" = @{
"project" = @{
"id" = $jiraProjectId
}
"issuetype" = @{
"id" = $jiraIssueTypeId
}
"summary" = "This is a sample issue created from PowerShell"
"description" = "Details about the issue go here."
}
}
# Convert the body to JSON
$bodyJson = $body | ConvertTo-Json -Depth 10
# Send the POST request to Jira API
$response = Invoke-RestMethod -Uri $jiraUrl -Method Post -Headers @{
Authorization = "Basic $base64AuthInfo"
"Content-Type" = "application/json"
} -Body $bodyJson
# Output the response
$response
Add Comment to Ticket using Summary Search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Define your Jira URL and API endpoint
$jiraDomain = "domain.atlassian.net"
$jiraSearchUrl = "https://$jiraDomain/rest/api/2/search"
$jiraIssueUrl = "https://$jiraDomain/rest/api/2/issue"
$jiraProjectId = "00001"
$jiraIssueTypeId = "00002"
# Define your Jira credentials (API token or password)
$jiraUsername = "jdoe@domain.com"
$jiraApiToken = "API token goes here" # Replace this with your Jira API token
# Prepare the authentication header (Basic Authentication)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${jiraUsername}:${jiraApiToken}"))
# Define the summary you're searching for
$issueSummary = "This is a sample issue created from PowerShell"
# Search for existing issues with the same summary within the specific project and issue type
$searchQuery = "?jql=project=$jiraProjectId AND issuetype=$jiraIssueTypeId AND summary~'$issueSummary'"
$response = Invoke-RestMethod -Uri "$jiraSearchUrl$searchQuery" -Method Get -Headers @{
Authorization = "Basic $base64AuthInfo"
"Content-Type" = "application/json"
}
# Check if an issue with the same summary exists
if ($response.issues.Count -gt 0) {
# Get the first matching issue's key
$issueKey = $response.issues[0].key
Write-Output "Issue already exists: $issueKey. Adding a comment."
# Define the comment data
$commentBody = @{
"body" = "Adding a comment to the existing issue."
}
# Convert the comment body to JSON
$commentBodyJson = $commentBody | ConvertTo-Json -Depth 10
# Send the POST request to add a comment to the existing issue
$commentResponse = Invoke-RestMethod -Uri "$jiraIssueUrl/$issueKey/comment" -Method Post -Headers @{
Authorization = "Basic $base64AuthInfo"
"Content-Type" = "application/json"
} -Body $commentBodyJson
# Output the response for the comment
$commentResponse
} else {
Write-Output "No existing issue found with the specified summary in the specified project and issue type. No action taken."
}
Respond to Comment in Ticket using Summary and Comment Search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Define your Jira URL and API endpoint
$jiraDomain = "domain.atlassian.net"
$jiraSearchUrl = "https://$jiraDomain/rest/api/2/search"
$jiraIssueUrl = "https://$jiraDomain/rest/api/2/issue"
$jiraProjectId = "00001"
$jiraIssueTypeId = "00002"
# Define your Jira credentials (API token or password)
$jiraUsername = "jdoe@domain.com"
$jiraApiToken = "API token goes here" # Replace this with your Jira API token
# Prepare the authentication header (Basic Authentication)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${jiraUsername}:${jiraApiToken}"))
# Define the summary and comment text you're searching for
$issueSummary = "This is a sample issue created from PowerShell"
$commentText = "This is the text of the comment we're searching for."
# Search for existing issues with the same summary within the specific project and issue type
$searchQuery = "?jql=project=$jiraProjectId AND issuetype=$jiraIssueTypeId AND summary~'$issueSummary'"
$response = Invoke-RestMethod -Uri "$jiraSearchUrl$searchQuery" -Method Get -Headers @{
Authorization = "Basic $base64AuthInfo"
"Content-Type" = "application/json"
}
# Check if an issue with the same summary exists
if ($response.issues.Count -gt 0) {
# Get the first matching issue's key
$issueKey = $response.issues[0].key
Write-Output "Issue found: $issueKey. Searching for the specified comment."
# Retrieve comments on the issue
$commentsResponse = Invoke-RestMethod -Uri "$jiraIssueUrl/$issueKey/comment" -Method Get -Headers @{
Authorization = "Basic $base64AuthInfo"
"Content-Type" = "application/json"
}
# Look for the specific comment text in the comments
$foundComment = $commentsResponse.comments | Where-Object { $_.body -match [regex]::Escape($commentText) }
if ($foundComment) {
$commentId = $foundComment.id
Write-Output "Comment found with ID $commentId. Responding to the comment."
# Define the reply data
$replyBody = @{
"body" = "This is a response to the existing comment."
}
# Convert the reply body to JSON
$replyBodyJson = $replyBody | ConvertTo-Json -Depth 10
# Send the POST request to reply to the found comment
$replyResponse = Invoke-RestMethod -Uri "$jiraIssueUrl/$issueKey/comment/$commentId" -Method Post -Headers @{
Authorization = "Basic $base64AuthInfo"
"Content-Type" = "application/json"
} -Body $replyBodyJson
# Output the response for the reply
$replyResponse
} else {
Write-Output "No comment found with the specified text. No action taken."
}
} else {
Write-Output "No existing issue found with the specified summary in the specified project and issue type. No action taken."
}
This post is licensed under CC BY 4.0 by the author.