JIRA Bulk Link via API

JIRA can’t natively perform bulk actions on issue links. I needed to move some links to a different link type. Here’s a quick script in Javascript that runs with NodeJS.

It querys for all the relevant issues using JQL and then iterates over each issue and link that matches the link type.

'use strict';

const request = require('request');
const async = require('async');

const user = 'youruser';
const pass = 'yourpass';
const url = 'http://yourjira';
const api = url + '/rest/api/2/';

// find all issues of type 'work package'
request({
  auth: {
    user: user,
    pass: pass
  },
  url: api + 'search',
  method: 'post',
  json: true,
  body: {
    jql: 'project = TEST AND issuetype = "Work Package"',
    maxResults: 100,
    fields: ['issuelinks']
  }
}, (err, res, body) => {
  // console.log(JSON.stringify(body, null, 4));

  // for each link type of acceptance criteria, delete, relink as '
  async.eachSeries(body.issues, (issue, doneIssue) => {
    console.log('processing ' + issue.key);
    async.eachSeries(issue.fields.issuelinks, (link, doneLink) => {
      if (link.outwardIssue && link.outwardIssue.fields.issuetype.name === 'Acceptance Criteria' && link.type.name === 'Contains') {
        console.log('processing link '+ link.id);
        async.series([
          function (done) {
            request({
              auth: {
                user: user,
                pass: pass
              },
              url: api + 'issueLink' + '/' + link.id,
              method: 'delete',
              json: true
            }, done);
          },
          function (done) {
            request({
              auth: {
                user: user,
                pass: pass
              },
              url: api + 'issueLink',
              method: 'post',
              json: true,
              body: {
                type: {
                  name: 'Requirements'
                },
                inwardIssue: {
                  'key': issue.key
                },
                outwardIssue: {
                  'key': link.outwardIssue.key
                }
              }
            }, done);
          }
        ], doneLink);
      } else {
        doneLink();
      }
    }, doneIssue);
  }, (err) => {
    console.log('complete');
  });
});

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *