Last updated on 7 października 2021
When I wanted to automate simple repeatable tasks from day to day work in a corporation environment, I stumbled on some issues with configuring Axios. This is the solution that let me consume intranet APIs for self-hosted tools You probably use every day.
First create httpsAgent that will let You consume data from Jira/Confluence/Sonar APIs.
const axios = require('axios');
const https = require('https');
const instance = axios.create({
httpsAgent: new https.Agent({ rejectUnauthorized: false })
});
Without this unsafe (or safe alternative, showed for example here) axios will throw “Error: self-signed certificate in certificate chain” and won’t let You run further.
This is only for dev-mode, eventually if You are creating a tool for team/personal automation of work it should be okay. But don’t go with this to production. For prod friendly configuration, have a look at this stack overflow thread.
Confluence/Jira
const options = {
method: 'post',
url: url,
auth: {
username: username,
password: password
},
data: 'TEMP',
headers: { 'Content-Type': 'application/json; charset=utf-8' }
}
function postToConfluence() {
//... Create Data, for example get existing page through api and modify it for Your usecase
options.data = JSON.stringify(jsonData);
instance(options).then((res) => { /* Do something with response */ }).catch(...);
}
For Confluence/JIRA, You can use basic auth with Your username and password for authentication of Your calls.
Sonar
const options = {
method: 'get',
auth: {
username: config.sonar.token
}
For Sonar authorization, You need to generate an access token for Your tool. In my organization, it’s done by:
Going to the sonar page → hover over Your avatar in right upper corner → 'My Account’ → 'Security’ Tab → 'generate token’ button.
You need to save this token, as You won’t be able to see it again after generating.
Bonus tips for working with JIRA APIs
- Occasionally, Your company will only tell You about one open API endpoints of JIRA, like “…/jira/rest/api/2/…” but check out all historical APIs You can! Like “…/jira/rest/agile/1.0/…” or “…/jira/rest/greenhopper/1.0/…”. Every so often, it’s easier to achieve something with older APIs.
- I couldn’t find this on the internet. But if You want to get an active sprint ID for Your team on JIRA, and Your team is using a custom board, You can do something like this to get the ID:
const teamBoardId = 123; - You can get this from url of Your teams board
const activeSprintUrl = 'https://jira.url.here/jira/rest/agile/1.0/board/'+ teamBoardId +'/sprint?state=active';
//Config for jira calls from the previous part of the article
...
instance.get(activeSprintUrl, options).then(response => {
sprintId = response.data.values[0].id;
sprintName = response.data.values[0].name;
}).catch(/*...*/)