2021-03-08 18:12:59 +05:30
|
|
|
import { delay } from 'lodash';
|
2020-11-24 15:15:51 +05:30
|
|
|
import toast from '~/vue_shared/plugins/global_toast';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from '../lib/utils/axios_utils';
|
2020-04-22 19:07:51 +05:30
|
|
|
import initForm from './edit';
|
|
|
|
import eventHub from './edit/event_hub';
|
2021-11-18 22:05:49 +05:30
|
|
|
import {
|
|
|
|
TEST_INTEGRATION_EVENT,
|
|
|
|
SAVE_INTEGRATION_EVENT,
|
|
|
|
GET_JIRA_ISSUE_TYPES_EVENT,
|
|
|
|
TOGGLE_INTEGRATION_EVENT,
|
|
|
|
VALIDATE_INTEGRATION_FORM_EVENT,
|
2021-12-11 22:18:48 +05:30
|
|
|
I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE,
|
|
|
|
I18N_DEFAULT_ERROR_MESSAGE,
|
|
|
|
I18N_SUCCESSFUL_CONNECTION_MESSAGE,
|
2021-11-18 22:05:49 +05:30
|
|
|
} from './constants';
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
export default class IntegrationSettingsForm {
|
|
|
|
constructor(formSelector) {
|
2021-11-18 22:05:49 +05:30
|
|
|
this.$form = document.querySelector(formSelector);
|
2020-04-22 19:07:51 +05:30
|
|
|
this.formActive = false;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
this.vue = null;
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
// Form Metadata
|
2021-11-18 22:05:49 +05:30
|
|
|
this.testEndPoint = this.$form.dataset.testUrl;
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2020-04-22 19:07:51 +05:30
|
|
|
// Init Vue component
|
2020-11-24 15:15:51 +05:30
|
|
|
this.vue = initForm(
|
2020-07-28 23:09:34 +05:30
|
|
|
document.querySelector('.js-vue-integration-settings'),
|
2020-11-24 15:15:51 +05:30
|
|
|
document.querySelector('.js-vue-default-integration-settings'),
|
2020-07-28 23:09:34 +05:30
|
|
|
);
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$on(TOGGLE_INTEGRATION_EVENT, (active) => {
|
2020-04-22 19:07:51 +05:30
|
|
|
this.formActive = active;
|
2020-11-24 15:15:51 +05:30
|
|
|
this.toggleServiceState();
|
|
|
|
});
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$on(TEST_INTEGRATION_EVENT, () => {
|
2020-11-24 15:15:51 +05:30
|
|
|
this.testIntegration();
|
|
|
|
});
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$on(SAVE_INTEGRATION_EVENT, () => {
|
2020-11-24 15:15:51 +05:30
|
|
|
this.saveIntegration();
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$on(GET_JIRA_ISSUE_TYPES_EVENT, () => {
|
|
|
|
this.getJiraIssueTypes(new FormData(this.$form));
|
2021-03-11 19:13:27 +05:30
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
saveIntegration() {
|
2021-02-22 17:27:13 +05:30
|
|
|
// Save Service if not active and check the following if active;
|
2020-11-24 15:15:51 +05:30
|
|
|
// 1) If form contents are valid
|
|
|
|
// 2) If this service can be saved
|
|
|
|
// If both conditions are true, we override form submission
|
|
|
|
// and save the service using provided configuration.
|
2021-11-18 22:05:49 +05:30
|
|
|
const formValid = this.$form.checkValidity() || this.formActive === false;
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
if (formValid) {
|
2021-03-08 18:12:59 +05:30
|
|
|
delay(() => {
|
2021-11-18 22:05:49 +05:30
|
|
|
this.$form.submit();
|
2021-03-08 18:12:59 +05:30
|
|
|
}, 100);
|
2020-11-24 15:15:51 +05:30
|
|
|
} else {
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$emit(VALIDATE_INTEGRATION_FORM_EVENT);
|
2020-11-24 15:15:51 +05:30
|
|
|
this.vue.$store.dispatch('setIsSaving', false);
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
2020-11-24 15:15:51 +05:30
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
testIntegration() {
|
2017-09-10 17:25:29 +05:30
|
|
|
// Service was marked active so now we check;
|
|
|
|
// 1) If form contents are valid
|
|
|
|
// 2) If this service can be tested
|
|
|
|
// If both conditions are true, we override form submission
|
|
|
|
// and test the service using provided configuration.
|
2021-11-18 22:05:49 +05:30
|
|
|
if (this.$form.checkValidity()) {
|
|
|
|
this.testSettings(new FormData(this.$form));
|
2020-06-23 00:09:42 +05:30
|
|
|
} else {
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$emit(VALIDATE_INTEGRATION_FORM_EVENT);
|
2020-11-24 15:15:51 +05:30
|
|
|
this.vue.$store.dispatch('setIsTesting', false);
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change Form's validation enforcement based on service status (active/inactive)
|
|
|
|
*/
|
2020-04-22 19:07:51 +05:30
|
|
|
toggleServiceState() {
|
|
|
|
if (this.formActive) {
|
2021-11-18 22:05:49 +05:30
|
|
|
this.$form.removeAttribute('novalidate');
|
|
|
|
} else if (!this.$form.getAttribute('novalidate')) {
|
|
|
|
this.$form.setAttribute('novalidate', 'novalidate');
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
/**
|
|
|
|
* Get a list of Jira issue types for the currently configured project
|
|
|
|
*
|
|
|
|
* @param {string} formData - URL encoded string containing the form data
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
getJiraIssueTypes(formData) {
|
|
|
|
const {
|
|
|
|
$store: { dispatch },
|
|
|
|
} = this.vue;
|
|
|
|
|
|
|
|
dispatch('requestJiraIssueTypes');
|
|
|
|
|
|
|
|
return this.fetchTestSettings(formData)
|
|
|
|
.then(
|
|
|
|
({
|
2021-12-11 22:18:48 +05:30
|
|
|
data: { issuetypes, error, message = I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE },
|
2021-03-11 19:13:27 +05:30
|
|
|
}) => {
|
|
|
|
if (error || !issuetypes?.length) {
|
2021-11-18 22:05:49 +05:30
|
|
|
eventHub.$emit(VALIDATE_INTEGRATION_FORM_EVENT);
|
2021-03-11 19:13:27 +05:30
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch('receiveJiraIssueTypesSuccess', issuetypes);
|
|
|
|
},
|
|
|
|
)
|
2021-12-11 22:18:48 +05:30
|
|
|
.catch(({ message = I18N_DEFAULT_ERROR_MESSAGE }) => {
|
2021-03-11 19:13:27 +05:30
|
|
|
dispatch('receiveJiraIssueTypesError', message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send request to the test endpoint which checks if the current config is valid
|
|
|
|
*/
|
|
|
|
fetchTestSettings(formData) {
|
|
|
|
return axios.put(this.testEndPoint, formData);
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
/**
|
|
|
|
* Test Integration config
|
|
|
|
*/
|
|
|
|
testSettings(formData) {
|
2021-03-11 19:13:27 +05:30
|
|
|
return this.fetchTestSettings(formData)
|
2018-03-17 18:26:18 +05:30
|
|
|
.then(({ data }) => {
|
|
|
|
if (data.error) {
|
2020-11-24 15:15:51 +05:30
|
|
|
toast(`${data.message} ${data.service_response}`);
|
2018-03-17 18:26:18 +05:30
|
|
|
} else {
|
2021-03-11 19:13:27 +05:30
|
|
|
this.vue.$store.dispatch('receiveJiraIssueTypesSuccess', data.issuetypes);
|
2021-12-11 22:18:48 +05:30
|
|
|
toast(I18N_SUCCESSFUL_CONNECTION_MESSAGE);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2021-12-11 22:18:48 +05:30
|
|
|
toast(I18N_DEFAULT_ERROR_MESSAGE);
|
2020-11-24 15:15:51 +05:30
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.vue.$store.dispatch('setIsTesting', false);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
}
|