Skip to content

Instantly share code, notes, and snippets.

@ivansnag
Created May 18, 2023 22:20
Show Gist options
  • Save ivansnag/2abb9f08c635038cf1a3c637e341171e to your computer and use it in GitHub Desktop.
Save ivansnag/2abb9f08c635038cf1a3c637e341171e to your computer and use it in GitHub Desktop.
Grabs JIRA integrations and their configurations from one project and copies them to another
// TODO: Create the trigger configs (ticket creation automation)
// Script will take JIRA integration configurations from a template project and copy them to another project you specify
const fetch = require('node-fetch');
const url = 'https://api.bugsnag.com/'
const auth_token = 'YOUR AUTH TOKEN'
const project_name_to_copy_from = ''
const project_name_to_copy_to = ''
const copy_jira_integrations = async () => {
const org_id = await get_organization_id();
const project_id = await get_project_info(org_id,project_name_to_copy_from);
const project_id_to_copy_to = await get_project_info(org_id, project_name_to_copy_to);
get_configured_integrations(org_id, project_id, project_id_to_copy_to);
}
let headers = {
headers: {
'Authorization': `token ${auth_token}`,
'Content-Type': 'application/json',
'X-Version': '2',
}
}
const get_organization_id = async () => {
let path = "user/organizations/";
let response = await fetch(url + path, Object.assign({ method: "GET" }, headers));
if (response.ok) {
let org_data = await response.json();
return org_data[0]['id']
} else {
console.error("HTTP-Error: " + response.status + " " + path);
}
}
const get_project_info = async (org_id, project_name) => {
let filter = "?q=" + project_name;
let path = "/organizations/" +org_id+ "/projects" + filter;
let response = await fetch(url + path, Object.assign({ method: "GET" }, headers));
if (response.ok) {
let project = await response.json();
return project[0].id
} else {
console.error("HTTP-Error: " + response.status + " " + path);
}
}
const get_configured_integrations = async (org_id, project_id, project_id_to_copy_to) => {
let added_path ="/configured_integration_summaries"
let path = `/projects/${project_id}/${added_path}`;
let response = await fetch(url + path, Object.assign({ method: "GET" }, headers));
if (response.ok) {
let integration_summaries = await response.json();
for(const integration of integration_summaries) {
if(integration.integration_key == 'jira'){
let integration_to_copy = await get_configured_integration(integration.notification_id);
let integration_request_body = remove_unneeded_props(integration_to_copy);
create_integration_copy(integration_request_body, project_id_to_copy_to);
}
}
} else {
console.error("HTTP-Error: " + response.status+ " " + path);
}
}
const get_configured_integration = async (integration_id) => {
let path = "configured_integrations/" + integration_id;
let response = await fetch(url + path, Object.assign({ method: "GET" }, headers));
if (response.ok) {
let configured_jira_integration = await response.json();
return configured_jira_integration
} else {
console.error("HTTP-Error: " + response.status+ " " + path);
}
}
const remove_unneeded_props = (integration) => {
delete integration.trigger_config;
delete integration.id;
delete integration.project_id;
delete integration.description;
delete integration.last_failure_message
delete integration.total_rate_limits
delete integration.last_successful_usage_at
delete integration.status
delete integration.additional_setup_required
return integration;
}
const create_integration_copy = async (request_body, project_id) => {
let path = "/projects/" + project_id + "/configured_integrations";
console.log("Creating Integration: ");
console.log(request_body.configuration.projectKey);
let response = await fetch(url + path, Object.assign({ method: "POST" , body: JSON.stringify(request_body) }, headers));
if (response.ok) {
let integration = await response.json();
console.log("Integration created:" + integration.id)
return integration
} else {
console.error("HTTP-Error: " + response.status + " " + path);
}
}
copy_jira_integrations()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment