JIRA Cloud REST API: Create, Get, Update and Delete an JIRA ISSUE using node.js

The post is about how can we consume JIRA Cloud REST API to create an issue, retrieve details of an issue, update the fields of an issue, and how to delete an issue in JIRA without actually logging in the JIRA cloud instance.

We are using node.js for writing the program here.

We will be JIRA Cloud REST API provide by Atlassian here.

  1. Create an Issue

Http POST method is used to post data via REST API.


function httpPost(){
var options = {
hostname:'myaccount.atlassian.net',
port:443,
path:'/rest/api/2/issue',
method:'POST',
headers: {
'Authorization':'Basic '+new Buffer("username"+':'+"password").toString('base64'),
'Content-Type':'application/json',
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (body) {
console.log('Body: '+body);
});
});
req.on('error', function(e) {
console.log('problem with request: '+e.message);
});

var postdata = {
“fields”: {
“project”:
{
“key”: “projectid”
},
“summary”: “issue summary”,
“description”:”issue description”,
“issuetype”: {
“name”: “Story”
},
“customfield_10027”: “custom filed description”

}
}

var jirapostString = JSON.stringify(postdata);
req.write(jirapostString);
req.end();
}
module.exports.httpPost = httpPost;

 

2. Retrieve issue details
Here, we will send a GET request to JIRA API


module.exports = {
httpGetIssue: () => {
var https = require('https');
return new Promise((resolve, reject) => {
var body = "";
var issueDesc = [];
var options = {
hostname:'myaccount.atlassian.net',
port:443,
path:'/rest/api/2/issue/issueid',
method:'GET',
headers: {
'Authorization':'Basic '+ new Buffer("username"+':'+"password").toString('base64'),
'Content-Type':'application/json',
}
};
https.get(options, function (res) {
res.on('data', function (data) {
body += data;
});
res.on('end', function () {
var response = JSON.parse(body);

resolve(response);
});
res.on(‘error’, (err) => {
reject(err);
})
});
});
}
}

3. Update an issue


var https = require('https');

function httpUpdate(){
var options = {
hostname:’accountname.atlassian.net’,
port:443,
path:’/rest/api/2/issueissueid’,
method:’PUT’,
headers: {
‘Authorization’:’Basic ‘+new Buffer(“username”+’:’+”password”).toString(‘base64’),
‘Content-Type’:’application/json’,
}
};
var req = https.request(options, function(res) {
res.setEncoding(‘utf8’);
res.on(‘data’, function (body) {
console.log(‘Body: ‘+body);
});
});
req.on(‘error’, function(e) {
console.log(‘problem with request: ‘+e.message);
});

var postdata = {
“fields”: {
“project”:
{
“key”:”projectId”
},

“issuetype”: {
“name”: “Story”
},
“summary” : “fieldvalue”

}
}

var jirapostString = JSON.stringify(postdata);
req.write(jirapostString);
req.end();
}
module.exports.httpUpdate = httpUpdate;

 

4. Delete an issue

var https = require(‘https’);

function httpDelete(i){
var options = {
hostname:’myaccount.atlassian.net’,
port:443,
path:’/rest/api/2/issue/issueid’,
method:’DELETE’,
headers: {
‘Authorization’:’Basic ‘+new Buffer(“username”+’:’+”password”).toString(‘base64’),
‘Content-Type’:’application/json’,
}
};
var req = https.request(options, function(res) {
res.setEncoding(‘utf8’);
res.on(‘data’, function (body) {
console.log(“Delete request STATUS: ” + res.statusCode);
});
});
req.on(‘error’, function(e) {
console.log(‘problem with request: ‘+e.message);
});

req.end();
}
module.exports.httpDelete = httpDelete;

Leave a comment