2020-05-19 15:08:04 +00:00
|
|
|
import { last } from 'lodash';
|
2022-01-08 03:14:00 +00:00
|
|
|
import { JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY } from './constants';
|
2020-05-19 15:08:04 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
export const IMPORT_STATE = {
|
|
|
|
FAILED: 'failed',
|
|
|
|
FINISHED: 'finished',
|
|
|
|
NONE: 'none',
|
|
|
|
SCHEDULED: 'scheduled',
|
|
|
|
STARTED: 'started',
|
|
|
|
};
|
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
export const isInProgress = (state) =>
|
2020-04-21 15:21:10 +00:00
|
|
|
state === IMPORT_STATE.SCHEDULED || state === IMPORT_STATE.STARTED;
|
2020-05-19 15:08:04 +00:00
|
|
|
|
2020-12-24 00:10:25 +00:00
|
|
|
export const isFinished = (state) => state === IMPORT_STATE.FINISHED;
|
2020-05-19 15:08:04 +00:00
|
|
|
|
2020-05-29 18:08:26 +00:00
|
|
|
/**
|
|
|
|
* Converts the list of Jira projects into a format consumable by GlFormSelect.
|
|
|
|
*
|
|
|
|
* @param {Object[]} projects - List of Jira projects
|
|
|
|
* @param {string} projects[].key - Jira project key
|
|
|
|
* @param {string} projects[].name - Jira project name
|
|
|
|
* @returns {Object[]} - List of Jira projects in a format consumable by GlFormSelect
|
|
|
|
*/
|
2020-12-24 00:10:25 +00:00
|
|
|
export const extractJiraProjectsOptions = (projects) =>
|
2020-05-29 18:08:26 +00:00
|
|
|
projects.map(({ key, name }) => ({ text: `${name} (${key})`, value: key }));
|
|
|
|
|
2020-05-19 15:08:04 +00:00
|
|
|
/**
|
|
|
|
* Calculates the label title for the most recent Jira import.
|
|
|
|
*
|
|
|
|
* @param {Object[]} jiraImports - List of Jira imports
|
|
|
|
* @param {string} jiraImports[].jiraProjectKey - Jira project key
|
|
|
|
* @returns {string} - A label title
|
|
|
|
*/
|
2020-12-24 00:10:25 +00:00
|
|
|
const calculateJiraImportLabelTitle = (jiraImports) => {
|
2020-05-19 15:08:04 +00:00
|
|
|
const mostRecentJiraProjectKey = last(jiraImports)?.jiraProjectKey;
|
|
|
|
const jiraProjectImportCount = jiraImports.filter(
|
2020-12-24 00:10:25 +00:00
|
|
|
(jiraImport) => jiraImport.jiraProjectKey === mostRecentJiraProjectKey,
|
2020-05-19 15:08:04 +00:00
|
|
|
).length;
|
|
|
|
return `jira-import::${mostRecentJiraProjectKey}-${jiraProjectImportCount}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the label color from a list of labels.
|
|
|
|
*
|
|
|
|
* @param {string} labelTitle - Label title
|
|
|
|
* @param {Object[]} labels - List of labels
|
|
|
|
* @param {string} labels[].title - Label title
|
|
|
|
* @param {string} labels[].color - Label color
|
|
|
|
* @returns {string} - The label color associated with the given labelTitle
|
|
|
|
*/
|
|
|
|
const calculateJiraImportLabelColor = (labelTitle, labels) =>
|
2020-12-24 00:10:25 +00:00
|
|
|
labels.find((label) => label.title === labelTitle)?.color;
|
2020-05-19 15:08:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the label for the most recent Jira import.
|
|
|
|
*
|
|
|
|
* @param {Object[]} jiraImports - List of Jira imports
|
|
|
|
* @param {string} jiraImports[].jiraProjectKey - Jira project key
|
|
|
|
* @param {Object[]} labels - List of labels
|
|
|
|
* @param {string} labels[].title - Label title
|
|
|
|
* @param {string} labels[].color - Label color
|
|
|
|
* @returns {{color: string, title: string}} - A label object containing a label color and title
|
|
|
|
*/
|
|
|
|
export const calculateJiraImportLabel = (jiraImports, labels) => {
|
|
|
|
const title = calculateJiraImportLabelTitle(jiraImports);
|
|
|
|
return {
|
|
|
|
color: calculateJiraImportLabelColor(title, labels),
|
|
|
|
title,
|
|
|
|
};
|
|
|
|
};
|
2020-07-13 18:09:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates whether the Jira import success alert should be shown.
|
|
|
|
*
|
|
|
|
* @param {string} labelTitle - Jira import label, for checking localStorage
|
|
|
|
* @param {string} importStatus - Jira import status
|
|
|
|
* @returns {boolean} - A boolean indicating whether to show the success alert
|
|
|
|
*/
|
|
|
|
export const shouldShowFinishedAlert = (labelTitle, importStatus) => {
|
|
|
|
const finishedAlertHideMap =
|
|
|
|
JSON.parse(localStorage.getItem(JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY)) || {};
|
|
|
|
|
|
|
|
const shouldHide = finishedAlertHideMap[labelTitle];
|
|
|
|
|
|
|
|
return !shouldHide && isFinished(importStatus);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the localStorage map to permanently hide the Jira import success alert
|
|
|
|
*
|
|
|
|
* @param {string} labelTitle - Jira import label, for checking localStorage
|
|
|
|
*/
|
2020-12-24 00:10:25 +00:00
|
|
|
export const setFinishedAlertHideMap = (labelTitle) => {
|
2020-07-13 18:09:16 +00:00
|
|
|
const finishedAlertHideMap =
|
|
|
|
JSON.parse(localStorage.getItem(JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY)) || {};
|
|
|
|
|
|
|
|
finishedAlertHideMap[labelTitle] = true;
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY,
|
|
|
|
JSON.stringify(finishedAlertHideMap),
|
|
|
|
);
|
|
|
|
};
|