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.
- 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;
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);
})
});
});
}
}
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;
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;