Commit 331c5d85 authored by Phil Hughes's avatar Phil Hughes

Merge branch '37999-spdx-dropdown' into 'master'

Pass SPDX licenses from backend to frontend

Closes #37999

See merge request gitlab-org/gitlab!36926
parents 71dd8e65 a5d2c222
...@@ -17,6 +17,7 @@ export default () => { ...@@ -17,6 +17,7 @@ export default () => {
settingsPath, settingsPath,
approvalsDocumentationPath, approvalsDocumentationPath,
lockedApprovalsRuleName, lockedApprovalsRuleName,
softwareLicenses,
} = el.dataset; } = el.dataset;
const storeSettings = { const storeSettings = {
...@@ -33,6 +34,8 @@ export default () => { ...@@ -33,6 +34,8 @@ export default () => {
store.dispatch('licenseManagement/setAPISettings', { store.dispatch('licenseManagement/setAPISettings', {
apiUrlManageLicenses: readLicensePoliciesEndpoint, apiUrlManageLicenses: readLicensePoliciesEndpoint,
}); });
store.dispatch('licenseManagement/setKnownLicenses', JSON.parse(softwareLicenses));
store.dispatch(`${LICENSE_LIST}/setLicensesEndpoint`, projectLicensesEndpoint); store.dispatch(`${LICENSE_LIST}/setLicensesEndpoint`, projectLicensesEndpoint);
return new Vue({ return new Vue({
......
...@@ -33,6 +33,10 @@ export default { ...@@ -33,6 +33,10 @@ export default {
required: false, required: false,
default: () => [], default: () => [],
}, },
knownLicenses: {
type: Array,
required: true,
},
loading: { loading: {
type: Boolean, type: Boolean,
required: false, required: false,
...@@ -78,6 +82,7 @@ export default { ...@@ -78,6 +82,7 @@ export default {
<add-license-form-dropdown <add-license-form-dropdown
id="js-license-dropdown" id="js-license-dropdown"
v-model="licenseName" v-model="licenseName"
:known-licenses="knownLicenses"
:placeholder="s__('LicenseCompliance|License name')" :placeholder="s__('LicenseCompliance|License name')"
/> />
<div class="invalid-feedback" :class="{ 'd-block': isInvalidLicense }"> <div class="invalid-feedback" :class="{ 'd-block': isInvalidLicense }">
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
import $ from 'jquery'; import $ from 'jquery';
import select2 from 'select2/select2'; import select2 from 'select2/select2';
import { KNOWN_LICENSES } from '../constants';
export default { export default {
name: 'AddLicenseFormDropdown', name: 'AddLicenseFormDropdown',
...@@ -17,6 +16,10 @@ export default { ...@@ -17,6 +16,10 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
knownLicenses: {
type: Array,
required: true,
},
}, },
mounted() { mounted() {
$(this.$refs.dropdownInput) $(this.$refs.dropdownInput)
...@@ -26,7 +29,10 @@ export default { ...@@ -26,7 +29,10 @@ export default {
placeholder: this.placeholder, placeholder: this.placeholder,
createSearchChoice: term => ({ id: term, text: term }), createSearchChoice: term => ({ id: term, text: term }),
createSearchChoicePosition: 'bottom', createSearchChoicePosition: 'bottom',
data: KNOWN_LICENSES.map(license => ({ id: license, text: license })), data: this.knownLicenses.map(license => ({
id: license,
text: license,
})),
}) })
.on('change', e => { .on('change', e => {
this.$emit('input', e.target.value); this.$emit('input', e.target.value);
......
...@@ -24,36 +24,6 @@ export const LICENSE_APPROVAL_ACTION = { ...@@ -24,36 +24,6 @@ export const LICENSE_APPROVAL_ACTION = {
DENY: 'deny', DENY: 'deny',
}; };
/* eslint-disable @gitlab/require-i18n-strings */
export const KNOWN_LICENSES = [
'AGPL-1.0',
'AGPL-3.0',
'Apache 2.0',
'Artistic-2.0',
'BSD',
'CC0 1.0 Universal',
'CDDL-1.0',
'CDDL-1.1',
'EPL-1.0',
'EPL-2.0',
'GPLv2',
'GPLv3',
'ISC',
'LGPL',
'LGPL-2.1',
'MIT',
'Mozilla Public License 2.0',
'MS-PL',
'MS-RL',
'New BSD',
'Python Software Foundation License',
'ruby',
'Simplified BSD',
'WTFPL',
'Zlib',
];
/* eslint-enable @gitlab/require-i18n-strings */
export const REPORT_GROUPS = [ export const REPORT_GROUPS = [
{ {
name: s__('LicenseManagement|Denied'), name: s__('LicenseManagement|Denied'),
......
...@@ -32,7 +32,12 @@ export default { ...@@ -32,7 +32,12 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(LICENSE_MANAGEMENT, ['managedLicenses', 'isLoadingManagedLicenses', 'isAdmin']), ...mapState(LICENSE_MANAGEMENT, [
'managedLicenses',
'isLoadingManagedLicenses',
'isAdmin',
'knownLicenses',
]),
...mapGetters(LICENSE_MANAGEMENT, [ ...mapGetters(LICENSE_MANAGEMENT, [
'isLicenseBeingUpdated', 'isLicenseBeingUpdated',
'hasPendingLicenses', 'hasPendingLicenses',
...@@ -143,6 +148,7 @@ export default { ...@@ -143,6 +148,7 @@ export default {
<div v-if="formIsOpen" class="gl-mt-3 gl-mb-3"> <div v-if="formIsOpen" class="gl-mt-3 gl-mb-3">
<add-license-form <add-license-form
:managed-licenses="managedLicenses" :managed-licenses="managedLicenses"
:known-licenses="knownLicenses"
:loading="isAddingNewLicense" :loading="isAddingNewLicense"
@addLicense="setLicenseApproval" @addLicense="setLicenseApproval"
@closeForm="closeAddLicenseForm" @closeForm="closeAddLicenseForm"
......
...@@ -12,6 +12,15 @@ export const setAPISettings = ({ commit }, data) => { ...@@ -12,6 +12,15 @@ export const setAPISettings = ({ commit }, data) => {
export const setLicenseInModal = ({ commit }, license) => { export const setLicenseInModal = ({ commit }, license) => {
commit(types.SET_LICENSE_IN_MODAL, license); commit(types.SET_LICENSE_IN_MODAL, license);
}; };
export const setIsAdmin = ({ commit }, payload) => {
commit(types.SET_IS_ADMIN, payload);
};
export const setKnownLicenses = ({ commit }, licenses) => {
commit(types.SET_KNOWN_LICENSES, licenses);
};
export const resetLicenseInModal = ({ commit }) => { export const resetLicenseInModal = ({ commit }) => {
commit(types.RESET_LICENSE_IN_MODAL); commit(types.RESET_LICENSE_IN_MODAL);
}; };
...@@ -153,10 +162,6 @@ export const receiveLicenseCheckApprovalRuleError = ({ commit }, error) => { ...@@ -153,10 +162,6 @@ export const receiveLicenseCheckApprovalRuleError = ({ commit }, error) => {
commit(types.RECEIVE_LICENSE_CHECK_APPROVAL_RULE_ERROR, error); commit(types.RECEIVE_LICENSE_CHECK_APPROVAL_RULE_ERROR, error);
}; };
export const setIsAdmin = ({ commit }, payload) => {
commit(types.SET_IS_ADMIN, payload);
};
export const addPendingLicense = ({ state, commit }, id = null) => { export const addPendingLicense = ({ state, commit }, id = null) => {
if (!state.pendingLicenses.includes(id)) { if (!state.pendingLicenses.includes(id)) {
commit(types.ADD_PENDING_LICENSE, id); commit(types.ADD_PENDING_LICENSE, id);
......
...@@ -13,6 +13,7 @@ export const REQUEST_SET_LICENSE_APPROVAL = 'REQUEST_SET_LICENSE_APPROVAL'; ...@@ -13,6 +13,7 @@ export const REQUEST_SET_LICENSE_APPROVAL = 'REQUEST_SET_LICENSE_APPROVAL';
export const RESET_LICENSE_IN_MODAL = 'RESET_LICENSE_IN_MODAL'; export const RESET_LICENSE_IN_MODAL = 'RESET_LICENSE_IN_MODAL';
export const SET_API_SETTINGS = 'SET_API_SETTINGS'; export const SET_API_SETTINGS = 'SET_API_SETTINGS';
export const SET_LICENSE_IN_MODAL = 'SET_LICENSE_IN_MODAL'; export const SET_LICENSE_IN_MODAL = 'SET_LICENSE_IN_MODAL';
export const SET_KNOWN_LICENSES = 'SET_KNOWN_LICENSES';
export const SET_IS_ADMIN = 'SET_IS_ADMIN'; export const SET_IS_ADMIN = 'SET_IS_ADMIN';
export const ADD_PENDING_LICENSE = 'ADD_PENDING_LICENSE'; export const ADD_PENDING_LICENSE = 'ADD_PENDING_LICENSE';
export const REMOVE_PENDING_LICENSE = 'REMOVE_PENDING_LICENSE'; export const REMOVE_PENDING_LICENSE = 'REMOVE_PENDING_LICENSE';
......
...@@ -20,6 +20,11 @@ export default { ...@@ -20,6 +20,11 @@ export default {
isAdmin: data, isAdmin: data,
}); });
}, },
[types.SET_KNOWN_LICENSES](state, data) {
Object.assign(state, {
knownLicenses: data,
});
},
[types.RECEIVE_MANAGED_LICENSES_SUCCESS](state, licenses = []) { [types.RECEIVE_MANAGED_LICENSES_SUCCESS](state, licenses = []) {
const managedLicenses = licenses.map(normalizeLicense).reverse(); const managedLicenses = licenses.map(normalizeLicense).reverse();
......
...@@ -17,4 +17,5 @@ export default () => ({ ...@@ -17,4 +17,5 @@ export default () => ({
existingLicenses: [], existingLicenses: [],
hasLicenseCheckApprovalRule: false, hasLicenseCheckApprovalRule: false,
isLoadingLicenseCheckApprovalRule: false, isLoadingLicenseCheckApprovalRule: false,
knownLicenses: [],
}); });
---
title: Show up to date SPDX licenses for license compliance
merge_request: 36926
author:
type: added
import Vue from 'vue';
import $ from 'jquery'; import $ from 'jquery';
import Dropdown from 'ee/vue_shared/license_compliance/components/add_license_form_dropdown.vue'; import Dropdown from 'ee/vue_shared/license_compliance/components/add_license_form_dropdown.vue';
import { KNOWN_LICENSES } from 'ee/vue_shared/license_compliance/constants'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'helpers/vue_mount_component_helper';
describe('AddLicenseFormDropdown', () => { let vm;
const Component = Vue.extend(Dropdown); let wrapper;
let vm;
const KNOWN_LICENSES = ['AGPL-1.0', 'AGPL-3.0', 'Apache 2.0', 'BSD'];
const createComponent = (props = {}) => {
wrapper = shallowMount(Dropdown, { propsData: { knownLicenses: KNOWN_LICENSES, ...props } });
vm = wrapper.vm;
};
describe('AddLicenseFormDropdown', () => {
afterEach(() => { afterEach(() => {
vm.$destroy(); vm = undefined;
wrapper.destroy();
}); });
it('emits `input` invent on change', () => { it('emits `input` invent on change', () => {
vm = mountComponent(Component); createComponent();
jest.spyOn(vm, '$emit').mockImplementation(() => {}); jest.spyOn(vm, '$emit').mockImplementation(() => {});
$(vm.$el) $(vm.$el)
...@@ -25,7 +32,7 @@ describe('AddLicenseFormDropdown', () => { ...@@ -25,7 +32,7 @@ describe('AddLicenseFormDropdown', () => {
it('sets the placeholder appropriately', () => { it('sets the placeholder appropriately', () => {
const placeholder = 'Select a license'; const placeholder = 'Select a license';
vm = mountComponent(Component, { placeholder }); createComponent({ placeholder });
const dropdownContainer = $(vm.$el).select2('container')[0]; const dropdownContainer = $(vm.$el).select2('container')[0];
...@@ -34,13 +41,13 @@ describe('AddLicenseFormDropdown', () => { ...@@ -34,13 +41,13 @@ describe('AddLicenseFormDropdown', () => {
it('sets the initial value correctly', () => { it('sets the initial value correctly', () => {
const value = 'AWESOME_LICENSE'; const value = 'AWESOME_LICENSE';
vm = mountComponent(Component, { value }); createComponent({ value });
expect(vm.$el.value).toContain(value); expect(vm.$el.value).toContain(value);
}); });
it('shows all pre-defined licenses', done => { it('shows all defined licenses', done => {
vm = mountComponent(Component); createComponent();
const element = $(vm.$el); const element = $(vm.$el);
......
...@@ -47,6 +47,7 @@ const createComponent = ({ state, getters, props, actionMocks, isAdmin, options, ...@@ -47,6 +47,7 @@ const createComponent = ({ state, getters, props, actionMocks, isAdmin, options,
managedLicenses, managedLicenses,
isLoadingManagedLicenses: true, isLoadingManagedLicenses: true,
isAdmin, isAdmin,
knownLicenses: [],
...state, ...state,
}, },
actions: { actions: {
......
...@@ -60,6 +60,21 @@ describe('License store actions', () => { ...@@ -60,6 +60,21 @@ describe('License store actions', () => {
}); });
}); });
describe('setKnownLicenses', () => {
it('commits SET_KNOWN_LICENSES', done => {
const payload = [{ name: 'BSD' }, { name: 'Apache' }];
testAction(
actions.setKnownLicenses,
payload,
state,
[{ type: mutationTypes.SET_KNOWN_LICENSES, payload }],
[],
)
.then(done)
.catch(done.fail);
});
});
describe('setLicenseInModal', () => { describe('setLicenseInModal', () => {
it('commits SET_LICENSE_IN_MODAL with license', done => { it('commits SET_LICENSE_IN_MODAL with license', done => {
testAction( testAction(
......
...@@ -20,6 +20,15 @@ describe('License store mutations', () => { ...@@ -20,6 +20,15 @@ describe('License store mutations', () => {
}); });
}); });
describe('SET_KNOWN_LICENSES', () => {
it('assigns knownLicenses to the store', () => {
const licenses = [{ name: 'BSD' }, { name: 'Apache' }];
store.commit(`licenseManagement/${types.SET_KNOWN_LICENSES}`, licenses);
expect(store.state.licenseManagement.knownLicenses).toBe(licenses);
});
});
describe('RESET_LICENSE_IN_MODAL', () => { describe('RESET_LICENSE_IN_MODAL', () => {
it('closes modal and deletes licenseInApproval', () => { it('closes modal and deletes licenseInApproval', () => {
store.replaceState({ store.replaceState({
......
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