All files / libs/tools/plugin/src/executors/support-weather jira-tickets.ts

0% Statements 0/89
0% Branches 0/1
0% Functions 0/1
0% Lines 0/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                                                                                                                                                                   
import { dayjs } from '@amalia/ext/dayjs';
import { assert } from '@amalia/ext/typescript';

export interface JiraIssue {
  id: string;
  key: string;
  fields: {
    summary?: string;
    labels?: string[];
    status?: { name?: string };
    created?: string;
    priority?: { name?: string };
    reporter?: { displayName?: string; emailAddress?: string };
    assignee?: { displayName?: string; emailAddress?: string };
  };
}

function getPriority(ticket: JiraIssue) {
  switch (ticket.fields.priority!.name) {
    case 'Highest':
      return 'P0';
    case 'High':
      return 'P1';
    case 'Medium':
      return 'P2';
    case 'Low':
      return 'P3';
    case 'Lowest':
      return 'P4';
    default:
      return '';
  }
}

export const getJiraSupportTickets = async (): Promise<string[]> => {
  const token = process.env['JIRA_API_TOKEN'];
  assert(token && token.split(':').length === 2, 'Token should be <email>:<access token>');

  const base64encode = Buffer.from(token).toString('base64');

  const JIRA_BASE = 'https://allshares.atlassian.net';

  const issues: JiraIssue[] = [];
  const maxResults = 50;
  let nextPageToken: string | undefined;
  let isLast = false;

  const jql = `project = LS AND labels = form-68 AND status != Shipped ORDER BY severity DESC, created ASC`;

  do {
    const body = {
      jql,
      maxResults,
      nextPageToken,
      // request created date and priority so callers can access creation date and severity
      fields: ['summary', 'labels', 'status', 'reporter', 'created', 'priority', 'assignee'],
    };

    const res = await fetch(`${JIRA_BASE}/rest/api/3/search/jql`, {
      method: 'POST',
      headers: {
        Authorization: `Basic ${base64encode}`,
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: JSON.stringify(body),
    });

    if (!res.ok) {
      const text = await res.text().catch(() => '');
      throw new Error(`Jira API error ${res.status}: ${text}`);
    }

    const payload = (await res.json()) as {
      issues: JiraIssue[];
      nextPageToken?: string;
      isLast?: boolean;
    };

    issues.push(...payload.issues);
    nextPageToken = payload.nextPageToken;
    isLast = payload.isLast ?? !nextPageToken;
  } while (!isLast);

  return issues.map((i) => {
    const status = i.fields.status?.name === 'Selected for Development' ? '' : ` -> ${i.fields.status?.name}`;
    return `[${getPriority(i)}] ${i.fields.summary} _(${dayjs(i.fields.created).fromNow()})_${status}`;
  });
};