Commit c5e725e6 authored by Doug Stull's avatar Doug Stull Committed by James Fargher

Use shared runners constants from the backend for the frontend

parent f5b128eb
...@@ -3,13 +3,7 @@ import { GlToggle, GlLoadingIcon, GlTooltip, GlAlert } from '@gitlab/ui'; ...@@ -3,13 +3,7 @@ import { GlToggle, GlLoadingIcon, GlTooltip, GlAlert } from '@gitlab/ui';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { import { DEBOUNCE_TOGGLE_DELAY, ERROR_MESSAGE } from '../constants';
DEBOUNCE_TOGGLE_DELAY,
ERROR_MESSAGE,
ENABLED,
DISABLED,
ALLOW_OVERRIDE,
} from '../constants';
export default { export default {
components: { components: {
...@@ -18,21 +12,14 @@ export default { ...@@ -18,21 +12,14 @@ export default {
GlTooltip, GlTooltip,
GlAlert, GlAlert,
}, },
props: { inject: [
updatePath: { 'updatePath',
type: String, 'sharedRunnersAvailability',
required: true, 'parentSharedRunnersAvailability',
}, 'runnerEnabled',
sharedRunnersAvailability: { 'runnerDisabled',
type: String, 'runnerAllowOverride',
required: true, ],
},
parentSharedRunnersAvailability: {
type: String,
required: false,
default: '',
},
},
data() { data() {
return { return {
isLoading: false, isLoading: false,
...@@ -43,21 +30,21 @@ export default { ...@@ -43,21 +30,21 @@ export default {
}, },
computed: { computed: {
toggleDisabled() { toggleDisabled() {
return this.parentSharedRunnersAvailability === DISABLED || this.isLoading; return this.parentSharedRunnersAvailability === this.runnerDisabled || this.isLoading;
}, },
enabledOrDisabledSetting() { enabledOrDisabledSetting() {
return this.enabled ? ENABLED : DISABLED; return this.enabled ? this.runnerEnabled : this.runnerDisabled;
}, },
disabledWithOverrideSetting() { disabledWithOverrideSetting() {
return this.allowOverride ? ALLOW_OVERRIDE : DISABLED; return this.allowOverride ? this.runnerAllowOverride : this.runnerDisabled;
}, },
}, },
created() { created() {
if (this.sharedRunnersAvailability !== ENABLED) { if (this.sharedRunnersAvailability !== this.runnerEnabled) {
this.enabled = false; this.enabled = false;
} }
if (this.sharedRunnersAvailability === ALLOW_OVERRIDE) { if (this.sharedRunnersAvailability === this.runnerAllowOverride) {
this.allowOverride = true; this.allowOverride = true;
} }
}, },
......
...@@ -4,8 +4,3 @@ import { __ } from '~/locale'; ...@@ -4,8 +4,3 @@ import { __ } from '~/locale';
export const DEBOUNCE_TOGGLE_DELAY = 1000; export const DEBOUNCE_TOGGLE_DELAY = 1000;
export const ERROR_MESSAGE = __('Refresh the page and try again.'); export const ERROR_MESSAGE = __('Refresh the page and try again.');
// runner setting options
export const ENABLED = 'enabled';
export const DISABLED = 'disabled_and_unoverridable';
export const ALLOW_OVERRIDE = 'disabled_with_override';
...@@ -4,11 +4,27 @@ import UpdateSharedRunnersForm from './components/shared_runners_form.vue'; ...@@ -4,11 +4,27 @@ import UpdateSharedRunnersForm from './components/shared_runners_form.vue';
export default (containerId = 'update-shared-runners-form') => { export default (containerId = 'update-shared-runners-form') => {
const containerEl = document.getElementById(containerId); const containerEl = document.getElementById(containerId);
const {
updatePath,
sharedRunnersAvailability,
parentSharedRunnersAvailability,
runnerEnabled,
runnerDisabled,
runnerAllowOverride,
} = containerEl.dataset;
return new Vue({ return new Vue({
el: containerEl, el: containerEl,
render(createElement) { render(createElement) {
return createElement(UpdateSharedRunnersForm, { return createElement(UpdateSharedRunnersForm, {
props: containerEl.dataset, provide: {
updatePath,
sharedRunnersAvailability,
parentSharedRunnersAvailability,
runnerEnabled,
runnerDisabled,
runnerAllowOverride,
},
}); });
}, },
}); });
......
...@@ -61,7 +61,10 @@ module Ci ...@@ -61,7 +61,10 @@ module Ci
{ {
update_path: api_v4_groups_path(id: group.id), update_path: api_v4_groups_path(id: group.id),
shared_runners_availability: group.shared_runners_setting, shared_runners_availability: group.shared_runners_setting,
parent_shared_runners_availability: group.parent&.shared_runners_setting parent_shared_runners_availability: group.parent&.shared_runners_setting,
runner_enabled: Namespace::SR_ENABLED,
runner_disabled: Namespace::SR_DISABLED_AND_UNOVERRIDABLE,
runner_allow_override: Namespace::SR_DISABLED_WITH_OVERRIDE
} }
end end
......
...@@ -3,13 +3,16 @@ import { shallowMount } from '@vue/test-utils'; ...@@ -3,13 +3,16 @@ import { shallowMount } from '@vue/test-utils';
import MockAxiosAdapter from 'axios-mock-adapter'; import MockAxiosAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
import SharedRunnersForm from '~/group_settings/components/shared_runners_form.vue'; import SharedRunnersForm from '~/group_settings/components/shared_runners_form.vue';
import { ENABLED, DISABLED, ALLOW_OVERRIDE } from '~/group_settings/constants';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
const TEST_UPDATE_PATH = '/test/update'; const provide = {
const DISABLED_PAYLOAD = { shared_runners_setting: DISABLED }; updatePath: '/test/update',
const ENABLED_PAYLOAD = { shared_runners_setting: ENABLED }; sharedRunnersAvailability: 'enabled',
const OVERRIDE_PAYLOAD = { shared_runners_setting: ALLOW_OVERRIDE }; parentSharedRunnersAvailability: null,
runnerDisabled: 'disabled',
runnerEnabled: 'enabled',
runnerAllowOverride: 'allow_override',
};
jest.mock('~/flash'); jest.mock('~/flash');
...@@ -17,13 +20,11 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -17,13 +20,11 @@ describe('group_settings/components/shared_runners_form', () => {
let wrapper; let wrapper;
let mock; let mock;
const createComponent = (props = {}) => { const createComponent = (provides = {}) => {
wrapper = shallowMount(SharedRunnersForm, { wrapper = shallowMount(SharedRunnersForm, {
propsData: { provide: {
updatePath: TEST_UPDATE_PATH, ...provide,
sharedRunnersAvailability: ENABLED, ...provides,
parentSharedRunnersAvailability: null,
...props,
}, },
}); });
}; };
...@@ -33,13 +34,13 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -33,13 +34,13 @@ describe('group_settings/components/shared_runners_form', () => {
const findEnabledToggle = () => wrapper.find('[data-testid="enable-runners-toggle"]'); const findEnabledToggle = () => wrapper.find('[data-testid="enable-runners-toggle"]');
const findOverrideToggle = () => wrapper.find('[data-testid="override-runners-toggle"]'); const findOverrideToggle = () => wrapper.find('[data-testid="override-runners-toggle"]');
const changeToggle = (toggle) => toggle.vm.$emit('change', !toggle.props('value')); const changeToggle = (toggle) => toggle.vm.$emit('change', !toggle.props('value'));
const getRequestPayload = () => JSON.parse(mock.history.put[0].data); const getSharedRunnersSetting = () => JSON.parse(mock.history.put[0].data).shared_runners_setting;
const isLoadingIconVisible = () => findLoadingIcon().exists(); const isLoadingIconVisible = () => findLoadingIcon().exists();
beforeEach(() => { beforeEach(() => {
mock = new MockAxiosAdapter(axios); mock = new MockAxiosAdapter(axios);
mock.onPut(TEST_UPDATE_PATH).reply(200); mock.onPut(provide.updatePath).reply(200);
}); });
afterEach(() => { afterEach(() => {
...@@ -95,7 +96,7 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -95,7 +96,7 @@ describe('group_settings/components/shared_runners_form', () => {
await waitForPromises(); await waitForPromises();
expect(getRequestPayload()).toEqual(ENABLED_PAYLOAD); expect(getSharedRunnersSetting()).toEqual(provide.runnerEnabled);
expect(findOverrideToggle().exists()).toBe(false); expect(findOverrideToggle().exists()).toBe(false);
}); });
...@@ -104,14 +105,14 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -104,14 +105,14 @@ describe('group_settings/components/shared_runners_form', () => {
await waitForPromises(); await waitForPromises();
expect(getRequestPayload()).toEqual(DISABLED_PAYLOAD); expect(getSharedRunnersSetting()).toEqual(provide.runnerDisabled);
expect(findOverrideToggle().exists()).toBe(true); expect(findOverrideToggle().exists()).toBe(true);
}); });
}); });
describe('override toggle', () => { describe('override toggle', () => {
beforeEach(() => { beforeEach(() => {
createComponent({ sharedRunnersAvailability: ALLOW_OVERRIDE }); createComponent({ sharedRunnersAvailability: provide.runnerAllowOverride });
}); });
it('enabling the override toggle sends correct payload', async () => { it('enabling the override toggle sends correct payload', async () => {
...@@ -119,7 +120,7 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -119,7 +120,7 @@ describe('group_settings/components/shared_runners_form', () => {
await waitForPromises(); await waitForPromises();
expect(getRequestPayload()).toEqual(OVERRIDE_PAYLOAD); expect(getSharedRunnersSetting()).toEqual(provide.runnerAllowOverride);
}); });
it('disabling the override toggle sends correct payload', async () => { it('disabling the override toggle sends correct payload', async () => {
...@@ -127,21 +128,21 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -127,21 +128,21 @@ describe('group_settings/components/shared_runners_form', () => {
await waitForPromises(); await waitForPromises();
expect(getRequestPayload()).toEqual(DISABLED_PAYLOAD); expect(getSharedRunnersSetting()).toEqual(provide.runnerDisabled);
}); });
}); });
describe('toggle disabled state', () => { describe('toggle disabled state', () => {
it(`toggles are not disabled with setting ${DISABLED}`, () => { it(`toggles are not disabled with setting ${provide.runnerDisabled}`, () => {
createComponent({ sharedRunnersAvailability: DISABLED }); createComponent({ sharedRunnersAvailability: provide.runnerDisabled });
expect(findEnabledToggle().props('disabled')).toBe(false); expect(findEnabledToggle().props('disabled')).toBe(false);
expect(findOverrideToggle().props('disabled')).toBe(false); expect(findOverrideToggle().props('disabled')).toBe(false);
}); });
it('toggles are disabled', () => { it('toggles are disabled', () => {
createComponent({ createComponent({
sharedRunnersAvailability: DISABLED, sharedRunnersAvailability: provide.runnerDisabled,
parentSharedRunnersAvailability: DISABLED, parentSharedRunnersAvailability: provide.runnerDisabled,
}); });
expect(findEnabledToggle().props('disabled')).toBe(true); expect(findEnabledToggle().props('disabled')).toBe(true);
expect(findOverrideToggle().props('disabled')).toBe(true); expect(findOverrideToggle().props('disabled')).toBe(true);
...@@ -154,7 +155,7 @@ describe('group_settings/components/shared_runners_form', () => { ...@@ -154,7 +155,7 @@ describe('group_settings/components/shared_runners_form', () => {
${{ error: 'Undefined error' }} | ${'Undefined error Refresh the page and try again.'} ${{ error: 'Undefined error' }} | ${'Undefined error Refresh the page and try again.'}
`(`with error $errorObj`, ({ errorObj, message }) => { `(`with error $errorObj`, ({ errorObj, message }) => {
beforeEach(async () => { beforeEach(async () => {
mock.onPut(TEST_UPDATE_PATH).reply(500, errorObj); mock.onPut(provide.updatePath).reply(500, errorObj);
createComponent(); createComponent();
changeToggle(findEnabledToggle()); changeToggle(findEnabledToggle());
......
...@@ -68,23 +68,35 @@ RSpec.describe Ci::RunnersHelper do ...@@ -68,23 +68,35 @@ RSpec.describe Ci::RunnersHelper do
end end
describe '#group_shared_runners_settings_data' do describe '#group_shared_runners_settings_data' do
let(:group) { create(:group, parent: parent, shared_runners_enabled: false) } let_it_be(:parent) { create(:group) }
let(:parent) { create(:group) } let_it_be(:group) { create(:group, parent: parent, shared_runners_enabled: false) }
let(:runner_constants) do
{
runner_enabled: Namespace::SR_ENABLED,
runner_disabled: Namespace::SR_DISABLED_AND_UNOVERRIDABLE,
runner_allow_override: Namespace::SR_DISABLED_WITH_OVERRIDE
}
end
it 'returns group data for top level group' do it 'returns group data for top level group' do
data = group_shared_runners_settings_data(parent) result = {
update_path: "/api/v4/groups/#{parent.id}",
shared_runners_availability: Namespace::SR_ENABLED,
parent_shared_runners_availability: nil
}.merge(runner_constants)
expect(data[:update_path]).to eq("/api/v4/groups/#{parent.id}") expect(group_shared_runners_settings_data(parent)).to eq result
expect(data[:shared_runners_availability]).to eq('enabled')
expect(data[:parent_shared_runners_availability]).to eq(nil)
end end
it 'returns group data for child group' do it 'returns group data for child group' do
data = group_shared_runners_settings_data(group) result = {
update_path: "/api/v4/groups/#{group.id}",
shared_runners_availability: Namespace::SR_DISABLED_AND_UNOVERRIDABLE,
parent_shared_runners_availability: Namespace::SR_ENABLED
}.merge(runner_constants)
expect(data[:update_path]).to eq("/api/v4/groups/#{group.id}") expect(group_shared_runners_settings_data(group)).to eq result
expect(data[:shared_runners_availability]).to eq(Namespace::SR_DISABLED_AND_UNOVERRIDABLE)
expect(data[:parent_shared_runners_availability]).to eq('enabled')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment