diff --git a/.eslintrc.js b/.eslintrc.js index 7dcecacceae8b9..470a9e9feb8ee3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,9 +1,37 @@ const { resolve } = require('path'); const { readdirSync } = require('fs'); -const dedent = require('dedent'); const restrictedModules = { paths: ['gulp-util'] }; +const APACHE_2_0_LICENSE_HEADER = ` +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +`; + +const ELASTIC_LICENSE_HEADER = ` +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +`; + module.exports = { extends: ['@elastic/eslint-config-kibana', '@elastic/eslint-config-kibana/jest'], @@ -215,26 +243,13 @@ module.exports = { '@kbn/license-header/require-license-header': [ 'error', { - license: dedent` - /* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - `, + license: APACHE_2_0_LICENSE_HEADER, + }, + ], + '@kbn/license-header/disallow-license-headers': [ + 'error', + { + licenses: [ELASTIC_LICENSE_HEADER], }, ], }, @@ -260,13 +275,13 @@ module.exports = { '@kbn/license-header/require-license-header': [ 'error', { - license: dedent` - /* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - `, + license: ELASTIC_LICENSE_HEADER, + }, + ], + '@kbn/license-header/disallow-license-headers': [ + 'error', + { + licenses: [APACHE_2_0_LICENSE_HEADER], }, ], }, diff --git a/packages/kbn-eslint-plugin-license-header/index.js b/packages/kbn-eslint-plugin-license-header/index.js index 093487c8691caf..003c89b0c5f3b1 100644 --- a/packages/kbn-eslint-plugin-license-header/index.js +++ b/packages/kbn-eslint-plugin-license-header/index.js @@ -20,6 +20,6 @@ module.exports = { rules: { 'require-license-header': require('./rules/require_license_header'), - 'remove-outdated-license-header': require('./rules/remove_outdated_license_header'), + 'disallow-license-headers': require('./rules/disallow_license_headers'), }, }; diff --git a/packages/kbn-eslint-plugin-license-header/rules/__tests__/remove_outdated_license_header.js b/packages/kbn-eslint-plugin-license-header/rules/__tests__/disallow_license_headers.js similarity index 95% rename from packages/kbn-eslint-plugin-license-header/rules/__tests__/remove_outdated_license_header.js rename to packages/kbn-eslint-plugin-license-header/rules/__tests__/disallow_license_headers.js index f230f2bbe2fc42..2ae61111660895 100644 --- a/packages/kbn-eslint-plugin-license-header/rules/__tests__/remove_outdated_license_header.js +++ b/packages/kbn-eslint-plugin-license-header/rules/__tests__/disallow_license_headers.js @@ -18,10 +18,10 @@ */ const { RuleTester } = require('eslint'); -const rule = require('../remove_outdated_license_header'); +const rule = require('../disallow_license_headers'); const dedent = require('dedent'); -const RULE_NAME = '@kbn/license-header/remove-outdated-license-header'; +const RULE_NAME = '@kbn/license-header/disallow-license-headers'; const ruleTester = new RuleTester({ parser: 'babel-eslint', diff --git a/packages/kbn-eslint-plugin-license-header/rules/remove_outdated_license_header.js b/packages/kbn-eslint-plugin-license-header/rules/disallow_license_headers.js similarity index 87% rename from packages/kbn-eslint-plugin-license-header/rules/remove_outdated_license_header.js rename to packages/kbn-eslint-plugin-license-header/rules/disallow_license_headers.js index 9993133f729b18..f7bd6fb68a7852 100644 --- a/packages/kbn-eslint-plugin-license-header/rules/remove_outdated_license_header.js +++ b/packages/kbn-eslint-plugin-license-header/rules/disallow_license_headers.js @@ -40,7 +40,7 @@ module.exports = { create: context => { return { Program(program) { - const nodeValues = init(context, program, () => { + const licenses = init(context, program, () => { const options = context.options[0] || {}; const licenses = options.licenses; @@ -56,21 +56,17 @@ module.exports = { }); }); - if (!nodeValues) return; + if (!licenses || !licenses.length) return; const sourceCode = context.getSourceCode(); sourceCode .getAllComments() - .filter(node => ( - nodeValues.find(nodeValue => ( - normalizeWhitespace(node.value) === nodeValue - )) - )) + .filter(node => licenses.includes(normalizeWhitespace(node.value))) .forEach(node => { context.report({ node, - message: 'Remove outdated license header.', + message: 'This license header is not allowed in this file.', fix(fixer) { return fixer.remove(node); } diff --git a/packages/kbn-test/src/functional_tests/lib/auth.js b/packages/kbn-test/src/functional_tests/lib/auth.js index 2bae4c91265abd..522dcee9d3b0be 100644 --- a/packages/kbn-test/src/functional_tests/lib/auth.js +++ b/packages/kbn-test/src/functional_tests/lib/auth.js @@ -17,12 +17,6 @@ * under the License. */ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - import { format as formatUrl } from 'url'; import request from 'request'; diff --git a/src/core_plugins/kibana/server/routes/api/remote_info/call_with_request_factory.js b/src/core_plugins/kibana/server/routes/api/remote_info/call_with_request_factory.js index dc70072ffd2854..0607995757aa33 100644 --- a/src/core_plugins/kibana/server/routes/api/remote_info/call_with_request_factory.js +++ b/src/core_plugins/kibana/server/routes/api/remote_info/call_with_request_factory.js @@ -17,12 +17,6 @@ * under the License. */ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - import { once } from 'lodash'; const callWithRequest = once(server => { diff --git a/src/core_plugins/kibana/server/routes/api/remote_info/index.js b/src/core_plugins/kibana/server/routes/api/remote_info/index.js index 2d1e57d4325505..d2270de6f9c257 100644 --- a/src/core_plugins/kibana/server/routes/api/remote_info/index.js +++ b/src/core_plugins/kibana/server/routes/api/remote_info/index.js @@ -17,12 +17,6 @@ * under the License. */ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - import { callWithRequestFactory } from './call_with_request_factory'; import handleEsError from '../../../lib/handle_es_error'; diff --git a/src/dev/tslint/rules/disallowLicenseHeaderRule.js b/src/dev/tslint/rules/disallowLicenseHeaderRule.js new file mode 100644 index 00000000000000..09995c47ff7410 --- /dev/null +++ b/src/dev/tslint/rules/disallowLicenseHeaderRule.js @@ -0,0 +1,51 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const Lint = require('tslint'); + +const FAILURE_STRING = 'This license header is not allowed in this file.'; +const RULE_NAME = 'disallow-license-header'; + +exports.Rule = class extends Lint.Rules.AbstractRule { + apply(sourceFile) { + const [headerText] = this.getOptions().ruleArguments; + + if (!headerText) { + throw new Error(`${RULE_NAME} requires a single argument containing the header text`); + } + + if (!sourceFile.text.includes(headerText)) { + return []; + } + + const start = sourceFile.text.indexOf(headerText); + const end = start + headerText.length; + + return [ + new Lint.RuleFailure( + sourceFile, + start, + end, + FAILURE_STRING, + RULE_NAME, + new Lint.Replacement(start, headerText.length, '') + ) + ]; + } +}; diff --git a/tslint.yaml b/tslint.yaml index e65bb1e931aa7d..470f8a16ec9724 100644 --- a/tslint.yaml +++ b/tslint.yaml @@ -32,3 +32,11 @@ rules: * specific language governing permissions and limitations * under the License. */ + disallow-license-header: + - true + - |- + /* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ diff --git a/x-pack/plugins/canvas/public/register_feature.js b/x-pack/plugins/canvas/public/register_feature.js index 658896d104bbdf..fcbb8a2ad9ad61 100644 --- a/x-pack/plugins/canvas/public/register_feature.js +++ b/x-pack/plugins/canvas/public/register_feature.js @@ -4,25 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - import { FeatureCatalogueRegistryProvider, FeatureCatalogueCategory, diff --git a/x-pack/tslint.yaml b/x-pack/tslint.yaml index 7fa4624642069f..4e41203ae9e809 100644 --- a/x-pack/tslint.yaml +++ b/x-pack/tslint.yaml @@ -9,3 +9,24 @@ rules: * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ + disallow-license-header: + - true + - |- + /* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */