I have been playing a lot with Drafts actions lately, and I decided to see if I could use it to create Jira tickets. I was happy to see that their was an action available for it in the Actions directory, and with a few quick changes to the Json payload, I was able to get it up and running. The only problem that I have is that I use two different Jira instances and three different projects, so the hard coding of some of the values was not going to work for me.
I decided that the best route for me was to use front matter YAML similar to Jekyll. I created a template in my Drafts that had front matter that I could use to define my ticket in a way that I can manually define project/issuetype/jira. I could create a pop-up driven menu that would let me chose, but I decided to stay in the text.
---
uid: [[date|%Y%m%d%H%M%S]]
title: <The Summary for Jira>
project: <Project Key>
issuetype: <Issue Type>
jira: <Jira Instance Label>
---
The uid
is not needed, but since I put that in all my Drafts notes, I decided to leave it. With the template created, I needed to update the template to deal with my front matter. I added the following constants to my action to process my front matter:
const myTitle = draft.processTemplate("[[line|3]]").replace("title: ", "");
const myProject = draft.processTemplate("[[line|4]]").replace("project: ", "");
const myIssueType = draft.processTemplate("[[line|5]]").replace("issuetype: ", "");
const myJira = draft.processTemplate("[[line|6]]").replace("jira: ", "");
const body = draft.processTemplate("[[line|8..]]");
I updated the credentials section to remove the project since I want to be able to choose the project I create the issue in:
// First run will prompt for host, user, password to JIRA server stored as <JIRA Instance Label>
var credential = Credential.createWithHostUsernamePassword(myJira, "JIRA credential");
credential.addTextField("host", "JIRA Domain");
credential.addTextField("username", "JIRA Username");
credential.addPasswordField("password", "JIRA Password");
// make sure we have credential info
credential.authorize();
Finally, I updated the JSON payload. I created the fields variable with all the fields necessary for all the Jira nodes, and then added an if
statement to add more fields if it is my work Jira.
// build up common Jira issue properties
var fields = {
"project": { "key": myProject },
"issuetype": { "name": myIssueType },
"summary": myTitle,
"description": myBody
};
// Work Jira needs additional fields
if ( myJira == "work" ) {
fields["assignee"] = { "name": credential.getValue("username") };
fields["reporter"] = { "name": credential.getValue("username") };
fields["customfield_16600"] = { "id": "19429" };
}
I’ve pretty much kept the rest of the file as it was, although I rearranged some of the log bits at the end. If you are interested, you can see the complete version in my GitHub repo. If you have suggestions on how to make it better, I would love to hear them.
Comments