Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicholas-a-clark/e239dc6ec2db489dce19ac2318d9525c to your computer and use it in GitHub Desktop.
Save nicholas-a-clark/e239dc6ec2db489dce19ac2318d9525c to your computer and use it in GitHub Desktop.
var request = require('request');
var dateFormat = require('dateformat');
var today = new Date();
/* Format the date to be todays date in mm-dd-yyy format for later use */
today = dateFormat(today, "mm-dd-yyyy");
onCallDateFormat = dateFormat(today, "yyyy-mm-dd");
var jiraUrl = "";
var jiraSubTasks = [
{
"jiraTicketName": "Bacula - verify all backup sets were backed up less than 24 hours ago",
"jiraTicketMessage": "Verify that all backup sets in bacula were last backed up less than 24 hours ago. Also make sure every client has a successful full in the last month. \\n "
},
{
"jiraTicketName": "Jenkins/Methode health check for ${today}",
"jiraTicketMessage": "Look at methode jenkins instance and address any discovered problems. \\n Status page is at: "
},
{
"jiraTicketName": "Jenkins/cron health check for ${today}",
"jiraTicketMessage": "Look at cron jenkins instance and address any discovered problems. \\n Status page is at: "
},
{
"jiraTicketName": "Logic Monitor health check for ${today}",
"jiraTicketMessage": "Look at current alert list in logic monitor and address any discovered problems. \\n Status page is at: "
},
{
"jiraTicketName": "Imperva - verify normal operation",
"jiraTicketMessage": "Make sure we are not flushing too much traffic. \\n Status page is at: "
},
{
"jiraTicketName": "Methode volumes - check tier-netapp1-b volume size and purge snapshots",
"jiraTicketMessage": "Check volume size and purge snapshots (if needed).\\n Information on purging: "
},
];
whoIsOncall();
createJiraTicket();
createJiraChildTickets();
function whoIsOncall()
{
//Get who is onCall
var http = require("https");
var options = {
"method": "GET",
"hostname": "api.opsgenie.com",
"port": null,
"path": "/v2/schedules/ServerOps_schedule/on-calls?scheduleIdentifierType=name&flat=true&date="+ onCallDateFormat +"T19%3A01%3A00-04%3A00",
"headers": {
"content-length": "0",
"authorization": "GenieKey "
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var jsonOuput = JSON.parse(body);
console.log("JSON output: "+JSON.stringify(jsonOuput.data.onCallRecipients).split("@")[0].replace('[','').replace('"',''))
//Split up response to be simply Firstname.LastName intead of full email address
console.log(JSON.stringify(jsonOuput.data.onCallRecipients).split("@")[0].replace('[','').replace('"',''));
callback (JSON.stringify(jsonOuput.data.onCallRecipients).split("@")[0].replace('[','').replace('"',''));
});
});
req.end();
}
function createJiraTicket(emailAddress) {
console.log("Email address" +emailAddress)
var jiraPost = {
"fields": {
"project":
{
"key": "OPSEC"
},
"assignee":
{
"name": emailAddress
},
"summary": "Night time time health check for "+today,
"description": "Main ticket for Operations night time on call daily health check tasks",
"issuetype": {
"name": "Alerts"
},
"priority": {
"name": "Medium"
}
}
};
request({
url: jiraUrl,
method: "POST",
json: true,
body: jiraPost,
}, function (error, response, body) {
if (!error && response.statusCode === 204)
{
console.log("OK");
}
else if (error)
{
console.log(error);
}
else
{
console.log("JIRA id is " +response.body.key);
console.log(JSON.stringify(response, null, 2))
return response.body.key;
}
});
}
function createJiraChildTickets()
{
// Create subticket Jira Sub Ticket tasks
// Loop over all the items in the JSON array for the sub-tickets needed
for( var i = 0, length = Object.keys(jiraSubTasks).length; i < length; i++ ) {
var jiraPostSubTicket = {
"fields": {
"project":
{
"key": "OPSEC"
},
"assignee":
{
"name": emailAddress
},
"parent":
{
"parent": parentJiraKey
},
"summary": jiraSubTasks.jiraSubTicket.SubTask.jiraTicketName,
"description": jiraSubTasks.jiraSubTicket.SubTask.jiraTicketMessage,
"issuetype":
{
"name": "Alerts"
},
"priority":
{
"name": "Medium"
}
} //end fields
}; //end jiraPostSubTicket function
request({
url: jiraUrl,
method: "POST",
json: true,
body: jiraPostSubTicket,
auth: {user: "otto", pass: "0tt0"}
}, function (error, response, body) {
if (!error && response.statusCode === 204)
{
console.log("OK");
}
else if (error)
{
console.log(error);
}
else
{
console.log("JIRA id is " +response.body.key);
console.log(JSON.stringify(response, null, 2))
}
});
} //end for loop
} //end createJiraChildTickets() function
@earthboundkid
Copy link

earthboundkid commented Aug 30, 2019

var request = require("request");
var dateFormat = require("dateformat");

var now = new Date();

/* Format the date to be todays date in mm-dd-yyy format for later use */
let today = dateFormat(now, "mm-dd-yyyy");
let onCallDateFormat = dateFormat(now, "yyyy-mm-dd");

var jiraUrl = "";

var jiraSubTasks = [
  {
    jiraTicketName:
      "Bacula - verify all backup sets were backed up less than 24 hours ago",
    jiraTicketMessage:
      "Verify that all backup sets in bacula were last backed up less than 24 hours ago.  Also make sure every client has a successful full in the last month. \\n "
  },
  {
    jiraTicketName: `Jenkins/Methode health check for ${today}`,
    jiraTicketMessage:
      "Look at methode jenkins instance and address any discovered problems. \\n Status page is at: "
  },
  {
    jiraTicketName: `Jenkins/cron health check for ${today}`,
    jiraTicketMessage:
      "Look at cron jenkins instance and address any discovered problems. \\n Status page is at: "
  },
  {
    jiraTicketName: `Logic Monitor health check for ${today}`,
    jiraTicketMessage:
      "Look at current alert list in logic monitor and address any discovered problems. \\n Status page is at: "
  },
  {
    jiraTicketName: "Imperva - verify normal operation",
    jiraTicketMessage:
      "Make sure we are not flushing too much traffic.  \\n Status page is at: "
  },
  {
    jiraTicketName:
      "Methode volumes - check tier-netapp1-b volume size and purge snapshots",
    jiraTicketMessage:
      "Check  volume size and purge snapshots (if needed).\\n Information on purging: "
  }
];

function promisedRequests(options) {
  return new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) {
        reject(error);
      } else {
        resolve({ response: response, body: body });
      }
    });
  });
}

async function main() {
  let emailAddress = await whoIsOncall();
  let parentJiraKey = await createJiraTicket(emailAddress);
  await createJiraChildTickets(emailAddress, parentJiraKey);
  console.log("done.");
}

main().catch(error => {
  console.error("failed:", error);
});

async function whoIsOncall() {
  var options = {
    method: "GET",
    hostname: "api.opsgenie.com",
    port: null,
    path:
      "/v2/schedules/ServerOps_schedule/on-calls?scheduleIdentifierType=name&flat=true&date=" +
      onCallDateFormat +
      "T19%3A01%3A00-04%3A00",
    headers: {
      "content-length": "0",
      authorization: "GenieKey "
    }
  };
  let { response, body } = await promisedRequests(options);
  return emailaddressFromResponse(response, body); // TODO TKTK???
}

async function createJiraTicket(emailAddress) {
  console.log("Email address" + emailAddress);
  var jiraPost = {
    fields: {
      project: {
        key: "OPSEC"
      },
      assignee: {
        name: emailAddress
      },
      summary: "Night time time health check for " + today,
      description:
        "Main ticket for Operations night time on call daily health check tasks",
      issuetype: {
        name: "Alerts"
      },
      priority: {
        name: "Medium"
      }
    }
  };

  let { response, body } = await promisedRequests({
    url: jiraUrl,
    method: "POST",
    json: true,
    body: jiraPost
  });
  return response.body.key;
}

async function createJiraChildTickets(emailAddress, parentJiraKey) {
  // Create subticket Jira Sub Ticket tasks
  // Loop over all the items in the JSON array for the sub-tickets needed

  for (let task of jiraSubTasks) {
    var jiraPostSubTicket = {
      fields: {
        project: {
          key: "OPSEC"
        },
        assignee: {
          name: emailAddress
        },
        parent: {
          parent: parentJiraKey
        },
        summary: task.jiraTicketName,
        description: task.jiraTicketMessage,
        issuetype: {
          name: "Alerts"
        },
        priority: {
          name: "Medium"
        }
      } //end fields
    }; //end jiraPostSubTicket function
    let { response, body } = await promisedRequests({
      url: jiraUrl,
      method: "POST",
      json: true,
      body: jiraPostSubTicket,
      auth: { user: "otto", pass: "0tt0" }
    });
    if (response.statusCode === 204) {
      console.log("OK");
    } else {
      console.log("JIRA id is " + response.body.key);
      console.log(JSON.stringify(response, null, 2));
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment