Skip to content

Instantly share code, notes, and snippets.

@grazor
Last active March 8, 2023 07:17
Show Gist options
  • Save grazor/6f482c459a4079fde92463388c60bc57 to your computer and use it in GitHub Desktop.
Save grazor/6f482c459a4079fde92463388c60bc57 to your computer and use it in GitHub Desktop.
Obsidian Templater insert Jira issue to the note

Insert jira issue into obsidian document

This is an Templater userscript to store jira ticket data as Obsidian note. Discussion is here.

This template requires go-jira tool to be installed and configured and provides three ways to load jira issues:

  • Inserting ticket contents for prompted issue id
  • Inserting ticket contents extracting issue id from note title
  • Inserting current active sprint overview note and populating its tasks as separate documents

Configuration (Linux, Mac)

  1. Install go-jira tool
  2. Configure go-jira to work with your jira server
    • Create ~/.jira.d/config.yml file, example contents:
      endpoint: https://jira.example.com/
      user: username
      project: TEST
    • Execute jira login to check configuration and retrieve cookies
  3. Install Templater plugin
  4. Configure Templater
    • Set Template folder location
    • Set Script files folder location
    • Turn on Enable System Comands option
    • Optionally set Shell binary location to /bin/sh
    • Add jira user function with value jira view -t debug $id
    • Optionally (for sprint template support) add user function jira_tasks with value jira list -t debug -q 'assignee = currentUser() AND Sprint in openSprints() AND Sprint not in futureSprints()'
  5. Place this template's .js (scripts_*.js) and .md (templates_*.md) files without prefixes (e.g. scripts_j2m.js goes to <Scripts>/j2m.js and templates_Expand Jira Issue.md goes to <Templates/>Expand Jira Issue.md) into configured Templater Script and Template directories respectively

Usage

Use Templater Insert Template (Alt + E) command to insert one of templates.

If script fails to load data, try running jira login from terminal to update your cookies.

Modification

Feel free to modify any part of template to suit your needs. You might want to add your jira installation specific fields like Estimation which are usually named as customfield_11111. Use jira -t denug to explore issue hierarchy.

Sprint template uses clumsy logic to compute sprint number in form of y<YEAR>q<QUARTER>s<SPRINT> assuming two weeks sprints. Tune it the way you want it to work.

Example

Sprint example

---
starts_at: Mon Dec 20 2021 00:00:00 UTC
ends_at: Mon Jan 03 2022 00:00:00 UTC
issues: 2
points: 7
name: y21q4s6
tags:
    - work
    - sprint
    - y21q4
---

# Sprint y21q4s6

## TODO

* [ ] [[ISSUE-11111]] Issue title [2 sp](https://jira/browse/ISSUE-11111)
* [ ] [[ISSUE-22222]] Issue title [5 sp](https://jira/browse/ISSUE-22222)

## In progress

## Done

Issue example

---
issue: ISSUE-11111
url: https://jira/browse/ISSUE-11111
priority: Normal
points: 5
reporter: First Last (@username)
assignee: First Last (@username)
created_at: Wed Dec 15 2021 12:00:00 UTC
tags:
    - work
    - issue
    - y21q4
---

# [ISSUE-11111](https://jira/browse/ISSUE-11111) Issue title

Markdown formatted issue description goes here

## Worklog

* 

License

Copyright (c) 2021 Sergey Poryvaev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// J2M converts jira markup to markdown
// based on https://github.com/FokkeZB/J2M
function toM(input) {
input = input.replace(/^bq\.(.*)$/gm, function (match, content) {
return '> ' + content + "\n";
});
input = input.replace(/([*_])(.*)\1/g, function (match,wrapper,content) {
var to = (wrapper === '*') ? '**' : '*';
return to + content + to;
});
// multi-level numbered list
input = input.replace(/^\s*((?:#|-|\+|\*)+) (.*)$/gm, function (match, level, content) {
var len = 0;
var prefix = '1.';
if (level.length > 1) {
len = parseInt((level.length - 1) * 2);
}
// take the last character of the level to determine the replacement
var prefix = level[level.length - 1];
if (prefix == '#') prefix = '1.';
return Array(len > 0? len+1:0).join(" ") + prefix + ' ' + content.trim();
});
// headers, must be after numbered lists
input = input.replace(/^h([0-6])\.(.*)$/gm, function (match,level,content) {
return Array(parseInt(level) + 1).join('#') + content;
});
input = input.replace(/<~([^>]*)>/g, '$1');
input = input.replace(/\{\{([^}]+)\}\}/g, '`$1`');
input = input.replace(/\?\?((?:.[^?]|[^?].)+)\?\?/g, '<cite>$1</cite>');
input = input.replace(/\+([^+]*)\+/g, '<ins>$1</ins>');
input = input.replace(/\^([^^]*)\^/g, '<sup>$1</sup>');
input = input.replace(/~([^~]*)~/g, '<sub>$1</sub>');
input = input.replace(/-([^-]*)-/g, '-$1-');
input = input.replace(/\{code(:([a-z]+))?\}([^]*?)\{code\}/gm, '```$2$3```');
input = input.replace(/\{quote\}([^]*)\{quote\}/gm, function(match, content) {
lines = content.split(/\r?\n/gm);
for (var i = 0; i < lines.length; i++) {
lines[i] = '> ' + lines[i];
}
return lines.join("\n");
});
// Images with alt= among their parameters
input = input.replace(/!([^|\n\s]+)\|([^\n!]*)alt=([^\n!\,]+?)(,([^\n!]*))?!/g, '![$3]($1)');
// Images with just other parameters (ignore them)
input = input.replace(/!([^|\n\s]+)\|([^\n!]*)!/g, '![]($1)');
// Images without any parameters or alt
input = input.replace(/!([^\n\s!]+)!/g, '![]($1)');
input = input.replace(/\[([^|]+)\|(.+?)\]/g, '[$1]($2)');
input = input.replace(/\[(.+?)\]([^\(]+)/g, '<$1>$2');
input = input.replace(/{noformat}/g, '```');
input = input.replace(/{color:([^}]+)}([^]*?){color}/gm, '<span style="color:$1">$2</span>');
// Convert header rows of tables by splitting input on lines
lines = input.split(/\r?\n/gm);
lines_to_remove = []
for (var i = 0; i < lines.length; i++) {
line_content = lines[i];
seperators = line_content.match(/\|\|/g);
if (seperators != null) {
lines[i] = lines[i].replace(/\|\|/g, "|");
// Add a new line to mark the header in Markdown,
// we require that at least 3 - are between each |
header_line = "";
for (var j = 0; j < seperators.length-1; j++) {
header_line += "|---";
}
header_line += "|";
lines.splice(i+1, 0, header_line);
}
}
// Join the split lines back
input = ""
for (var i = 0; i < lines.length; i++) {
input += lines[i] + "\n"
}
return input;
};
module.exports = toM;
// jurl produces jira issue url from api url
function issueURL(url, id) {
let i = new URL(url);
i.pathname = `/browse/${id}`;
return i.toString();
}
module.exports = issueURL;
// sprint provides hepler methods to compute current sprint timeframes
Date.prototype.getQuarter = function() {
return Math.ceil((this.getMonth() + 1) / 3)
}
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
var today = new Date(this.getFullYear(),this.getMonth(),this.getDate());
var dayOfYear = ((today - onejan + 86400000)/86400000);
return Math.ceil(dayOfYear/7);
};
Date.prototype.getQWeek = function() {
var d = new Date(this.getTime());
var q = this.getQuarter();
var qs = new Date(Date.UTC(this.getFullYear(), (q-1)*3, 1));
if(qs.getDay() != 0){
qs.setDate(qs.getDate() - (qs.getDay()-1));
}
return Math.round((d-qs)/(1000*60*60*24*14));
};
function getSprint(d) {
var d = d || new Date();
var info = {y: d.getFullYear() % 100, q: d.getQuarter(), w: d.getWeek(), s: d.getQWeek()};
info.sprint = `y${info.y}q${info.q}s${info.s}`;
info.quarter = `y${info.y}q${info.q}`;
return info;
}
module.exports = getSprint;
<%*
// NOTE: for using RAW version of this file remove surrounding triple apostrophes
let ids = tp.file.title.match(/[A-Z]+-\d+/g);
if (!ids) {
	let err = "title does not contain issue id";
	console.log(err);
	throw err;
}

let id = ids[0];
let y = JSON.parse(await tp.user.jira({id}));
let comments = y.fields.comment.comments.map((c) => ({
	author: c.author.displayName,
	authorLogin: c.author.name,
	createdAt: c.created,
	body: c.body
}));

let issueURL = tp.user.jurl(y.self, y.key);
let created = new Date(y.fields.created);
let q = tp.user.sprint();

// Add your custome fields here like follows:
// points: ${y.fields.customfield_11111}
tR += `---
issue: ${y.key}
url: ${issueURL}
priority: ${y.fields.priority.name}
reporter: ${y.fields.reporter.displayName} (@${y.fields.reporter.name})
assignee: ${y.fields.assignee.displayName} (@${y.fields.assignee.name})
created_at: ${created}
tags:
    - work
    - issue
    - ${q.quarter}
---

# [${y.key}](${issueURL}) ${y.fields.summary}

${tp.user.j2m(y.fields.description).trim()}`;

if (Array.isArray(comments) && comments.length) {
	tR += '\n\n## Comments\n\n';
	tR += comments.reverse().map((c) => `${c.author} (@${c.authorLogin})
> ${tp.user.j2m(c.body).trim().split('\n').join('\n> ')}`).join('\n\n')
}

tR += '\n\n## Worklog\n\n* ';
%>
<%*
// NOTE: for using RAW version of this file remove surrounding triple apostrophes
let input = await tp.system.prompt("Issue ID", "", true);
let ids = input.match(/[A-Z]+-\d+/g);
if (!ids) {
	let err = "title does not contain issue id";
	console.log(err);
	throw err;
}

let id = ids[0];
let y = JSON.parse(await tp.user.jira({id}));
let comments = y.fields.comment.comments.map((c) => ({
	author: c.author.displayName,
	authorLogin: c.author.name,
	createdAt: c.created,
	body: c.body
}));

let issueURL = tp.user.jurl(y.self, y.key);
let created = new Date(y.fields.created);
let q = tp.user.sprint();

// Add your custome fields here like follows:
// points: ${y.fields.customfield_11111}
tR += `---
issue: ${y.key}
url: ${issueURL}
priority: ${y.fields.priority.name}
reporter: ${y.fields.reporter.displayName} (@${y.fields.reporter.name})
assignee: ${y.fields.assignee.displayName} (@${y.fields.assignee.name})
created_at: ${created}
tags:
    - work
    - issue
    - ${q.quarter}
---

# [${y.key}](${issueURL}) ${y.fields.summary}

${tp.user.j2m(y.fields.description).trim()}`;

if (Array.isArray(comments) && comments.length) {
	tR += '\n\n## Comments\n\n';
	tR += comments.reverse().map((c) => `${c.author} (@${c.authorLogin})
> ${tp.user.j2m(c.body).trim().split('\n').join('\n> ')}`).join('\n\n')
}

tR += '\n\n## Worklog\n\n* ';
%>
<%*
// NOTE: for using RAW version of this file remove surrounding triple apostrophes
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

async function issueToNote(data) {
	let path = `${tp.file.folder(true)}/${data.key}`;
	if (!tp.file.exists(path)) {
		await tp.file.create_new(tp.file.find_tfile("Expand Jira Issue"), path);
	}
		
	let detail = JSON.parse(await tp.user.jira({id: data.key}));
  
  // Add your custome fields here like follows:
  // points: detail.fields.customfield_11111,	
	return {
		id: data.key,
		points: 0,
		description: data.fields.summary,
		url: tp.user.jurl(data.self, data.key),
	}
}

let y = JSON.parse(await tp.user.jira_tasks()).issues;

let issues = [];
for (i in y) {
	issues.push(await issueToNote(y[i]));
}

// This logic assumes sprint starts the day script is executed
let now = new Date();
let start = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()));
let end = start.addDays(14);
let q = tp.user.sprint(now);
let totalPoints = issues.reduce((total, issue) => issue.points + total, 0);
issues.sort((a,b) => a.points - b.points);

tR += `---
starts_at: ${start}
ends_at: ${end}
issues: ${issues.length}
points: ${totalPoints}
name: ${q.sprint}
tags:
    - work
    - sprint
    - ${q.quarter}
---

# Sprint ${q.sprint}

## TODO

` + issues.map((i) => `* [ ] [[${i.id}]] ${i.description} [${i.points} sp](${i.url})`).join('\n') + `

## In progress

## Done
`;
%>
@slaughtr
Copy link

Making issue.js prompt user for input

I wanted to be able to insert an issue into a current note, so I came up with this:

Replace lines 102-110 with:

  // Getting jira issue
  let id = await tp.system.prompt("JIRA ticket ID", "SRE-125", false);
  let issueIDRegex = /[A-Z]+-\d+/g

  if (!id || !issueIDRegex.test(id)) {
    let err = "title does not contain issue ID or properly formatted issue ID";
    console.log(err);
    throw err;
  }

I compliment that by replacing lines 94-97 with input = lines.join('\n') to get rid of that last new line and making the output to something a little more minimal:

tR += `---
# [${y.key}](${issueURL}) ${y.fields.summary}
> Reporter: ${y.fields.reporter.displayName}
> Assignee: ${y.fields.assignee.displayName}
> Created: ${created}
> Description: ${toM(y.fields.description.substring(0, 100)) + '...'}
---
`;

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