Last active
April 8, 2022 19:16
-
-
Save brian-akong/a1fb5d9998b2b228f879bf8bc3c0c1ff to your computer and use it in GitHub Desktop.
Node.js template script for linking an arbitrary number of Jira issues to a single Jira issue
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
// This is a template that can be used to link whatever Jira issues you'd like to a single Jira issue | |
// Please pass valid values to the following variables | |
const username = '' // Your Jira username - your email (most likey) e.g. jane.doe@xyz.com | |
const apiToken = '' // An API token for your Jira account. See here for more details on obtaining one - https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/ | |
const jiraBaseUrl = '' // Base URL for your Jira instance (do not include a trailing "/") e.g. https://my-organization.atlassian.net | |
const outwardIssue = '' // The KEY of the Jira issue you'd like to link TO (think "parent" issue) e.g. ABC-100 | |
const jiraIssues = [] // An array that contains the list of Jira issue keys that you want to link e.g. ['ABC-200', 'ABC-300', 'ABC-400', ...] | |
const linkType = 'Relates' // The type of linking you'd like e.g. Relates, Duplicates, etc. | |
// No user-input needed beyond this point | |
// Function to obtain link types in your Jira instance | |
function getLinkTypes() { | |
fetch(`${jiraBaseUrl}/rest/api/3/issueLinkType`, { | |
method: 'GET', | |
headers: { | |
'Authorization': `Basic ${Buffer.from(`${username}:${apiToken}`).toString('base64')}`, | |
'Accept': `application/json` | |
} | |
}) | |
.then(response => { | |
console.log( | |
`Response: ${response.status} ${response.statusText}` | |
); | |
return response.text(); | |
}) | |
.then(text => console.log(JSON.parse(text))) | |
.catch(err => console.error(err)); | |
} | |
function createRequestBody(outwardIssue, inwardIssue) { | |
let requestBody = `{ | |
"outwardIssue": { | |
"key": "${outwardIssue}" | |
}, | |
"comment": { | |
"body": { | |
"type": "doc", | |
"version": 1, | |
"content": [ | |
{ | |
"type": "paragraph", | |
"content": [ | |
{ | |
"text": "Linked related issue!", | |
"type": "text" | |
} | |
] | |
} | |
] | |
} | |
}, | |
"inwardIssue": { | |
"key": "${inwardIssue}" | |
}, | |
"type": { | |
"name": "${linkType}" | |
} | |
}`; | |
return requestBody | |
} | |
// Function that actually performs the linking | |
function linkJiraTicket(bodyData) { | |
fetch(`${jiraBaseUrl}/rest/api/3/issueLink`, { | |
method: 'POST', | |
headers: { | |
'Authorization': `Basic ${Buffer.from(`${username}:${apiToken}`).toString('base64')}`, | |
'Accept': `application/json`, | |
'Content-Type': 'application/json' | |
}, | |
body: bodyData | |
}).then(response => { | |
console.log(`Response = ${response}`); | |
return response.text(); | |
}).then(text => { | |
console.log(text); | |
}).catch(err => { | |
console.error(err); | |
}); | |
} | |
// Un-comment the below line to get link types in your Jira instance when you run this | |
// getLinkTypes() | |
for (const issue of jiraIssues) { | |
let linkBody = makeLinkBodyData(issue); | |
linkJiraTicket(linkBody); | |
console.log(`Done linking ${issue} to ${outwardIssue}!`) | |
await new Promise(resolve => setTimeout(resolve, 1000)); // this is here to artifically limit the rate of API requests to not cause a HTTP 429 error from occurring | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment