2018-07-25 07:05:40 -04:00
|
|
|
import $ from 'jquery';
|
2020-02-17 13:09:00 -05:00
|
|
|
import { debounce } from 'lodash';
|
2021-06-18 02:10:20 -04:00
|
|
|
import createFlash from '~/flash';
|
2018-07-25 07:05:40 -04:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { __ } from '~/locale';
|
2020-10-16 14:09:04 -04:00
|
|
|
import { hide } from '~/tooltips';
|
2021-02-01 10:08:56 -05:00
|
|
|
import SSHMirror from './ssh_mirror';
|
2018-07-25 07:05:40 -04:00
|
|
|
|
2018-08-01 19:08:22 -04:00
|
|
|
export default class MirrorRepos {
|
2018-07-25 07:05:40 -04:00
|
|
|
constructor(container) {
|
|
|
|
this.$container = $(container);
|
2018-11-12 05:52:48 -05:00
|
|
|
this.$password = null;
|
2018-07-25 07:05:40 -04:00
|
|
|
this.$form = $('.js-mirror-form', this.$container);
|
|
|
|
this.$urlInput = $('.js-mirror-url', this.$form);
|
|
|
|
this.$protectedBranchesInput = $('.js-mirror-protected', this.$form);
|
|
|
|
this.$table = $('.js-mirrors-table-body', this.$container);
|
2018-08-01 19:08:22 -04:00
|
|
|
this.mirrorEndpoint = this.$form.data('projectMirrorEndpoint');
|
2018-07-25 07:05:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2018-08-01 19:08:22 -04:00
|
|
|
this.initMirrorPush();
|
2018-08-03 15:34:14 -04:00
|
|
|
this.registerUpdateListeners();
|
|
|
|
}
|
2018-07-25 07:05:40 -04:00
|
|
|
|
2018-08-03 15:34:14 -04:00
|
|
|
initMirrorPush() {
|
2020-03-30 11:07:51 -04:00
|
|
|
this.$keepDivergentRefsInput = $('.js-mirror-keep-divergent-refs', this.$form);
|
2018-08-03 15:34:14 -04:00
|
|
|
this.$passwordGroup = $('.js-password-group', this.$container);
|
|
|
|
this.$password = $('.js-password', this.$passwordGroup);
|
|
|
|
this.$authMethod = $('.js-auth-method', this.$form);
|
|
|
|
|
2020-03-30 11:07:51 -04:00
|
|
|
this.$keepDivergentRefsInput.on('change', () => this.updateKeepDivergentRefs());
|
2018-08-03 15:34:14 -04:00
|
|
|
this.$authMethod.on('change', () => this.togglePassword());
|
|
|
|
this.$password.on('input.updateUrl', () => this.debouncedUpdateUrl());
|
2018-11-12 05:52:48 -05:00
|
|
|
|
|
|
|
this.initMirrorSSH();
|
2018-11-28 06:04:11 -05:00
|
|
|
this.updateProtectedBranches();
|
2020-03-30 11:07:51 -04:00
|
|
|
this.updateKeepDivergentRefs();
|
2018-11-12 05:52:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
initMirrorSSH() {
|
|
|
|
if (this.$password) {
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2018-11-12 05:52:48 -05:00
|
|
|
this.$password.off('input.updateUrl');
|
|
|
|
}
|
|
|
|
this.$password = undefined;
|
|
|
|
|
|
|
|
this.sshMirror = new SSHMirror('.js-mirror-form');
|
|
|
|
this.sshMirror.init();
|
2018-07-25 07:05:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updateUrl() {
|
2018-08-01 19:08:22 -04:00
|
|
|
let val = this.$urlInput.val();
|
|
|
|
|
|
|
|
if (this.$password) {
|
|
|
|
const password = this.$password.val();
|
|
|
|
if (password) val = val.replace('@', `:${password}@`);
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.js-mirror-url-hidden', this.$form).val(val);
|
2018-07-25 07:05:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updateProtectedBranches() {
|
|
|
|
const val = this.$protectedBranchesInput.get(0).checked
|
|
|
|
? this.$protectedBranchesInput.val()
|
|
|
|
: '0';
|
|
|
|
$('.js-mirror-protected-hidden', this.$form).val(val);
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:07:51 -04:00
|
|
|
updateKeepDivergentRefs() {
|
|
|
|
const field = this.$keepDivergentRefsInput.get(0);
|
|
|
|
|
|
|
|
// This field only exists after the form is switched to 'Push' mode
|
|
|
|
if (field) {
|
|
|
|
const val = field.checked ? this.$keepDivergentRefsInput.val() : '0';
|
|
|
|
$('.js-mirror-keep-divergent-refs-hidden', this.$form).val(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 07:05:40 -04:00
|
|
|
registerUpdateListeners() {
|
2020-02-17 13:09:00 -05:00
|
|
|
this.debouncedUpdateUrl = debounce(() => this.updateUrl(), 200);
|
2018-08-01 19:08:22 -04:00
|
|
|
this.$urlInput.on('input', () => this.debouncedUpdateUrl());
|
2018-07-25 07:05:40 -04:00
|
|
|
this.$protectedBranchesInput.on('change', () => this.updateProtectedBranches());
|
2020-12-23 16:10:24 -05:00
|
|
|
this.$table.on('click', '.js-delete-mirror', (event) => this.deleteMirror(event));
|
2018-07-25 07:05:40 -04:00
|
|
|
}
|
|
|
|
|
2018-08-03 15:34:14 -04:00
|
|
|
togglePassword() {
|
|
|
|
const isPassword = this.$authMethod.val() === 'password';
|
|
|
|
|
|
|
|
if (!isPassword) {
|
|
|
|
this.$password.val('');
|
|
|
|
this.updateUrl();
|
|
|
|
}
|
|
|
|
this.$passwordGroup.collapse(isPassword ? 'show' : 'hide');
|
2018-08-01 19:08:22 -04:00
|
|
|
}
|
|
|
|
|
2018-07-25 07:05:40 -04:00
|
|
|
deleteMirror(event, existingPayload) {
|
|
|
|
const $target = $(event.currentTarget);
|
|
|
|
let payload = existingPayload;
|
|
|
|
|
|
|
|
if (!payload) {
|
|
|
|
payload = {
|
|
|
|
project: {
|
|
|
|
remote_mirrors_attributes: {
|
|
|
|
id: $target.data('mirrorId'),
|
2019-05-06 09:15:03 -04:00
|
|
|
_destroy: 1,
|
2018-07-25 07:05:40 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return axios
|
|
|
|
.put(this.mirrorEndpoint, payload)
|
|
|
|
.then(() => this.removeRow($target))
|
2021-06-18 02:10:20 -04:00
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('Failed to remove mirror.'),
|
|
|
|
}),
|
|
|
|
);
|
2018-07-25 07:05:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
removeRow($target) {
|
|
|
|
const row = $target.closest('tr');
|
2020-10-16 14:09:04 -04:00
|
|
|
hide($('.js-delete-mirror', row));
|
2018-07-25 07:05:40 -04:00
|
|
|
row.remove();
|
|
|
|
}
|
|
|
|
/* eslint-enable class-methods-use-this */
|
|
|
|
}
|