Created
January 22, 2023 15:36
-
-
Save vigonotion/d2cac8d2a81f7998e141eee62e8010b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Find JIRA ticket | |
// Shortcode: j | |
import "@johnlindquist/kit" | |
const JIRA_URL = await env("JIRA_URL"); | |
const JIRA_USERNAME = await env("JIRA_USERNAME"); | |
const JIRA_PASSWORD = await env("JIRA_PASSWORD"); | |
const JIRA_PROJECT_KEY = await env("JIRA_PROJECT_KEY"); | |
function base64encode(input: string) { | |
const buf = Buffer.from(input, "utf-8"); | |
return buf.toString("base64"); | |
} | |
const url = new URL(JIRA_URL + "/rest/api/2/search") | |
async function search(query: string) { | |
let jql = "text ~ \"" + query + "\""; | |
if(/((?<!([A-Z]{1,10})-?)[A-Z]+-\d+)/.test(query)) | |
{ | |
jql = "key = " + query; | |
} | |
const params = new URLSearchParams({ | |
jql: "project = " + JIRA_PROJECT_KEY + " and " + jql + " ORDER BY statusCategory ASC", | |
maxResults: "10" | |
}); | |
const endpoint = url.toString() + "?" + params.toString(); | |
const options = { | |
headers: { | |
"Authorization": "Basic " + base64encode(JIRA_USERNAME + ":" + JIRA_PASSWORD) | |
} | |
}; | |
const result = await get(endpoint, options); | |
const data = result?.data?.issues; | |
return data; | |
} | |
const colorClass = (category: string) => { | |
if(category == "done") return "bg-green-900"; | |
if(category == "indeterminate") return "bg-yellow-900"; | |
return "bg-blue-900"; | |
} | |
const issue = await arg("Search issues", async (input) => { | |
const issues = await search(input); | |
return issues.map(x => ({ | |
value: x.key, | |
html: `<div> | |
<div class="flex align-center" style="gap: 4px;"> | |
<img src="${x.fields.issuetype.iconUrl}" /> | |
<span class="text-sm" style="opacity: .8;">${x.key}</span> | |
</div> | |
<div> | |
${x.fields.summary} | |
</div> | |
</div>`, | |
preview: () => `<div class="m-4"> | |
<h1>${x.key}: ${x.fields.summary}</h1> | |
<span class="uppercase p-1 mt-2 text-xs rounded text-white ${colorClass(x.fields.status.statusCategory.key)}">${x.fields.status.name}</span> | |
</div> | |
${md(x.fields.description)}` | |
})); | |
}); | |
await browse(JIRA_URL + "/browse/" + issue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment