Skip to content

Instantly share code, notes, and snippets.

@believer
Last active May 22, 2024 02:28
Show Gist options
  • Save believer/714883e6ea8a691a56c7fd070cdcc4bb to your computer and use it in GitHub Desktop.
Save believer/714883e6ea8a691a56c7fd070cdcc4bb to your computer and use it in GitHub Desktop.
Script for Raycast that takes a Jira link, fetches information from Jira's REST API, and converts it to Tana's paste format
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Jira fetcher
// @raycast.mode silent
// Optional parameters:
// @raycast.icon 🤖
// @raycast.argument1 { "type": "text", "placeholder": "Jira URL" }
// Documentation:
// @raycast.description Fetch information about Jira ticket for Tana
// @raycast.author Rickard Natt och Dag
// @raycast.authorURL https://willcodefor.beer
import { spawn } from 'node:child_process'
// Link from Raycast input
const url = process.argv.at(2)
// URL to Jira REST API
// https://developer.atlassian.com/server/jira/platform/rest-apis/
const baseUrl = '<url-to-your-jira-rest-api>'
// Extract ticket number from URL
// Change to regex to match your ticket number format
const ticketNumber = url.match(/CNX-\d*/)
if (!ticketNumber) {
console.log('No ticket number found')
return
}
// Copy to clipboard
function pbcopy(data) {
const proc = spawn('pbcopy')
proc.stdin.write(data)
proc.stdin.end()
}
// Function that cleans up the description
// Your mileage may vary here :D
function cleanDescription(description) {
return (
description
// Remove smart link tags
.replaceAll('|smart-link', '')
// Fix links
.replace(/\[(.+?)\|(.+?)(?=[\|\]])\]/gm, '[$1]($2)')
// Fix code blocks
.replaceAll('{{', '`')
.replaceAll('}}', '`')
// Fix apostrophes
.replaceAll('’', "'")
// Split by line
.split('\n')
// Remove empty lines
.filter(Boolean)
// Add indentation
.map((d) => ` - ${d}`)
.join('\n')
)
}
try {
const response = await fetch(`${baseUrl}${ticketNumber}`, {
method: 'GET',
headers: {
'Accept-Language': 'en-US',
'Content-Type': 'application/json',
// Find the Jira cookie in your browser and paste it here
Cookie: '<cookie-value-from-jira-page-in-your-browser',
},
})
const data = await response.json()
let paste = '%%tana%%\n'
paste += `- ${data.fields.summary} #issue\n`
paste += ` - URL:: [Ticket](${url})\n`
paste += ` - Ticket Number:: ${ticketNumber}\n`
// New line is important below to get correct outline levels
paste += ` - Description::
${cleanDescription(data.fields.description)}\n`
pbcopy(paste)
console.log('Ticket copied')
} catch (e) {
console.log('Error', e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment