Skip to content

Instantly share code, notes, and snippets.

@ryan-nauman
Created February 2, 2024 13:38
Show Gist options
  • Save ryan-nauman/a78654556f9b29eebffb5a724c9bdce5 to your computer and use it in GitHub Desktop.
Save ryan-nauman/a78654556f9b29eebffb5a724c9bdce5 to your computer and use it in GitHub Desktop.
// Name: Jira Search
import '@johnlindquist/kit';
import { adfConvert } from '../lib/adf-convert';
import { encodeToBase64Node } from '../lib/base64';
import { Jira } from '../lib/jira';
const ATLASSIAN_EMAIL = await env('ATLASSIAN_EMAIL', async () => {
return await arg('Enter your jira email address');
});
const ATLASSIAN_TOKEN = await env('ATLASSIAN_TOKEN', async () => {
return await arg('Enter your jira api token');
});
const BROWSE_ISSUE_URL_PREFIX = 'https://studio-x.atlassian.net/browse/';
const SEARCH_API_PREFIX = 'https://studio-x.atlassian.net/rest/api/3/search';
const searchMethod = await arg('How would you like to search jira?', [
{ name: 'Text search', value: 'text' },
{ name: 'Key (e.g. VOLT-1)', value: 'key' },
{ name: 'JQL (e.g. project = VOLT)', value: 'jql' },
]);
const searchQuery = await arg('Enter the search value');
let jql;
switch (searchMethod) {
case 'text':
jql = `text ~ "${searchQuery}"`;
break;
case 'key':
jql = `key = ${searchQuery}`;
break;
case 'jql':
jql = searchQuery;
break;
}
let { data } = await post<Jira>(
`${SEARCH_API_PREFIX}`,
{
jql,
maxResults: 10,
fields: ['summary', 'assignee', 'creator', 'description'],
fieldsByKeys: false,
},
{
headers: {
Authorization: `Basic ${encodeToBase64Node(
`${ATLASSIAN_EMAIL}:${ATLASSIAN_TOKEN}`,
)}`,
},
},
);
let issueKey = await arg(
`Select Issue:`,
data.issues.map(({ fields, key }) => {
return {
name: key,
description: fields.summary,
value: key,
preview: () => {
const adf = adfConvert(fields?.description);
return md(`# ${key} ${fields?.summary}
🧑‍🍳 ${fields?.assignee?.displayName || 'Unassigned'}
📖 ${fields?.creator?.displayName || 'Unknown'}
#### Description
${adf}
`);
},
};
}),
);
await $`open ${BROWSE_ISSUE_URL_PREFIX}${issueKey}`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment