From 6f20e33263ad20d820602daebb03180130721373 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 18 Aug 2023 20:19:24 +0800 Subject: [PATCH 1/4] Fallback more url cases --- src/main/kotlin/app/cash/licensee/licenses.kt | 94 +++++++------------ 1 file changed, 36 insertions(+), 58 deletions(-) diff --git a/src/main/kotlin/app/cash/licensee/licenses.kt b/src/main/kotlin/app/cash/licensee/licenses.kt index 301a402f..a77af21c 100644 --- a/src/main/kotlin/app/cash/licensee/licenses.kt +++ b/src/main/kotlin/app/cash/licensee/licenses.kt @@ -15,6 +15,8 @@ */ package app.cash.licensee +import java.net.URL + internal fun normalizeLicenseInfo( coordinateToPomInfo: Map, ): List { @@ -54,82 +56,58 @@ private fun PomLicense.toSpdx(): List { SpdxLicenses.embedded.findByUrl(url)?.let { license -> return license } - @Suppress("HttpUrlsUsage") - val fallbackId = when (url) { - "http://www.apache.org/licenses/LICENSE-2.0.txt", - "https://www.apache.org/licenses/LICENSE-2.0.txt", - "http://www.apache.org/licenses/LICENSE-2.0.html", - "https://www.apache.org/licenses/LICENSE-2.0.html", - "http://www.opensource.org/licenses/apache2.0.php", - "https://www.opensource.org/licenses/apache2.0.php", - "http://www.apache.org/licenses/LICENSE-2.0", - "https://www.apache.org/licenses/LICENSE-2.0", - "http://api.github.com/licenses/apache-2.0", - "https://api.github.com/licenses/apache-2.0", + + val lowerUrl = URL(url.toLowerCase()) + val host = lowerUrl.host.removePrefix("www.") + val path = lowerUrl.path.removeSuffix("/") + .removeSuffix(".txt") + .removeSuffix(".php") + .removeSuffix(".html") + val fixedUrl = host + path + + val fallbackId = when (fixedUrl) { + "apache.org/licenses/license-2.0", + "api.github.com/licenses/apache-2.0", + "opensource.org/licenses/apache2.0", + "opensource.org/license/apache-2-0", -> "Apache-2.0" - "http://creativecommons.org/publicdomain/zero/1.0/", - "https://creativecommons.org/publicdomain/zero/1.0/", - "http://api.github.com/licenses/cc0-1.0", - "https://api.github.com/licenses/cc0-1.0", + "api.github.com/licenses/cc0-1.0", + "creativecommons.org/publicdomain/zero/1.0", -> "CC0-1.0" - "http://www.opensource.org/licenses/LGPL-2.1", - "https://www.opensource.org/licenses/LGPL-2.1", - "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html", - "https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html", - "http://api.github.com/licenses/lgpl-2.1", - "https://api.github.com/licenses/lgpl-2.1", + "api.github.com/licenses/lgpl-2.1", + "gnu.org/licenses/old-licenses/lgpl-2.1", + "opensource.org/licenses/lgpl-2.1", -> "LGPL-2.1-only" - "http://opensource.org/licenses/mit-license", - "https://opensource.org/licenses/mit-license", - "http://www.opensource.org/licenses/mit-license.php", - "https://www.opensource.org/licenses/mit-license.php", - "http://opensource.org/licenses/MIT", - "https://opensource.org/licenses/MIT", - "http://api.github.com/licenses/mit", - "https://api.github.com/licenses/mit", + "opensource.org/licenses/mit-license", + "api.github.com/licenses/mit", -> "MIT" - "http://www.opensource.org/licenses/bsd-license", - "https://www.opensource.org/licenses/bsd-license", - "http://www.opensource.org/licenses/bsd-license.php", - "https://www.opensource.org/licenses/bsd-license.php", - "http://api.github.com/licenses/bsd-2-clause", - "https://api.github.com/licenses/bsd-2-clause", + "api.github.com/licenses/bsd-2-clause", + "opensource.org/licenses/bsd-license", -> "BSD-2-Clause" - "http://opensource.org/licenses/BSD-3-Clause", - "https://opensource.org/licenses/BSD-3-Clause", - "http://api.github.com/licenses/bsd-3-clause", - "https://api.github.com/licenses/bsd-3-clause", + "api.github.com/licenses/bsd-3-clause", + "opensource.org/licenses/BSD-3-Clause", -> "BSD-3-Clause" - "http://www.gnu.org/software/classpath/license.html", - "https://www.gnu.org/software/classpath/license.html", + "gnu.org/software/classpath/license", -> "GPL-2.0-with-classpath-exception" - "http://choosealicense.com/licenses/gpl-2.0", - "https://choosealicense.com/licenses/gpl-2.0", - "http://opensource.org/license/gpl-2-0", - "https://opensource.org/license/gpl-2-0", - "http://www.gnu.org/licenses/old-licenses/gpl-2.0.html", - "https://www.gnu.org/licenses/old-licenses/gpl-2.0.html", - "http://api.github.com/licenses/gpl-2.0", - "https://api.github.com/licenses/gpl-2.0", + "api.github.com/licenses/gpl-2.0", + "choosealicense.com/licenses/gpl-2.0", + "gnu.org/licenses/old-licenses/gpl-2.0", + "opensource.org/license/gpl-2-0", -> "GPL-2.0-or-later" - "http://www.eclipse.org/org/documents/epl-v10.php", - "https://www.eclipse.org/org/documents/epl-v10.php", - "http://api.github.com/licenses/epl-1.0", - "https://api.github.com/licenses/epl-1.0", + "api.github.com/licenses/epl-1.0", + "eclipse.org/org/documents/epl-v10", -> "EPL-1.0" - "http://www.eclipse.org/legal/epl-2.0/", - "https://www.eclipse.org/legal/epl-2.0/", - "http://api.github.com/licenses/epl-2.0", - "https://api.github.com/licenses/epl-2.0", + "api.github.com/licenses/epl-2.0", + "eclipse.org/legal/epl-2.0", -> "EPL-2.0" else -> null From a433ba1bd9ad267c7e9aa561f8d41dae26b97f2b Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 18 Aug 2023 20:55:48 +0800 Subject: [PATCH 2/4] Test --- src/main/kotlin/app/cash/licensee/licenses.kt | 115 +++++++++--------- .../kotlin/app/cash/licensee/LicensesTest.kt | 34 ++++++ 2 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 src/test/kotlin/app/cash/licensee/LicensesTest.kt diff --git a/src/main/kotlin/app/cash/licensee/licenses.kt b/src/main/kotlin/app/cash/licensee/licenses.kt index a77af21c..63b7671f 100644 --- a/src/main/kotlin/app/cash/licensee/licenses.kt +++ b/src/main/kotlin/app/cash/licensee/licenses.kt @@ -48,6 +48,64 @@ internal fun normalizeLicenseInfo( return artifactDetails } +internal fun getFallbackId(url: String): String? { + val lowerUrl = URL(url.toLowerCase()) + val host = lowerUrl.host.removePrefix("www.") + val path = lowerUrl.path.removeSuffix("/") + .removeSuffix(".txt") + .removeSuffix(".php") + .removeSuffix(".html") + val fixedUrl = host + path + + return when (fixedUrl) { + "apache.org/licenses/license-2.0", + "api.github.com/licenses/apache-2.0", + "opensource.org/licenses/apache2.0", + "opensource.org/license/apache-2-0", + -> "Apache-2.0" + + "api.github.com/licenses/cc0-1.0", + "creativecommons.org/publicdomain/zero/1.0", + -> "CC0-1.0" + + "api.github.com/licenses/lgpl-2.1", + "gnu.org/licenses/old-licenses/lgpl-2.1", + "opensource.org/licenses/lgpl-2.1", + -> "LGPL-2.1-only" + + "api.github.com/licenses/mit", + "opensource.org/licenses/mit-license", + -> "MIT" + + "api.github.com/licenses/bsd-2-clause", + "opensource.org/licenses/bsd-license", + -> "BSD-2-Clause" + + "api.github.com/licenses/bsd-3-clause", + "opensource.org/licenses/BSD-3-Clause", + -> "BSD-3-Clause" + + "gnu.org/software/classpath/license", + -> "GPL-2.0-with-classpath-exception" + + "api.github.com/licenses/gpl-2.0", + "choosealicense.com/licenses/gpl-2.0", + "gnu.org/licenses/old-licenses/gpl-2.0", + "opensource.org/license/gpl-2-0", + -> "GPL-2.0-or-later" + + "api.github.com/licenses/epl-1.0", + "eclipse.org/org/documents/epl-v10", + -> "EPL-1.0" + + "api.github.com/licenses/epl-2.0", + "eclipse.org/legal/epl-2.0", + -> "EPL-2.0" + + else -> null + } +} + private val detailsComparator = compareBy(ArtifactDetail::groupId, ArtifactDetail::artifactId, ArtifactDetail::version) @@ -57,62 +115,7 @@ private fun PomLicense.toSpdx(): List { return license } - val lowerUrl = URL(url.toLowerCase()) - val host = lowerUrl.host.removePrefix("www.") - val path = lowerUrl.path.removeSuffix("/") - .removeSuffix(".txt") - .removeSuffix(".php") - .removeSuffix(".html") - val fixedUrl = host + path - - val fallbackId = when (fixedUrl) { - "apache.org/licenses/license-2.0", - "api.github.com/licenses/apache-2.0", - "opensource.org/licenses/apache2.0", - "opensource.org/license/apache-2-0", - -> "Apache-2.0" - - "api.github.com/licenses/cc0-1.0", - "creativecommons.org/publicdomain/zero/1.0", - -> "CC0-1.0" - - "api.github.com/licenses/lgpl-2.1", - "gnu.org/licenses/old-licenses/lgpl-2.1", - "opensource.org/licenses/lgpl-2.1", - -> "LGPL-2.1-only" - - "opensource.org/licenses/mit-license", - "api.github.com/licenses/mit", - -> "MIT" - - "api.github.com/licenses/bsd-2-clause", - "opensource.org/licenses/bsd-license", - -> "BSD-2-Clause" - - "api.github.com/licenses/bsd-3-clause", - "opensource.org/licenses/BSD-3-Clause", - -> "BSD-3-Clause" - - "gnu.org/software/classpath/license", - -> "GPL-2.0-with-classpath-exception" - - "api.github.com/licenses/gpl-2.0", - "choosealicense.com/licenses/gpl-2.0", - "gnu.org/licenses/old-licenses/gpl-2.0", - "opensource.org/license/gpl-2-0", - -> "GPL-2.0-or-later" - - "api.github.com/licenses/epl-1.0", - "eclipse.org/org/documents/epl-v10", - -> "EPL-1.0" - - "api.github.com/licenses/epl-2.0", - "eclipse.org/legal/epl-2.0", - -> "EPL-2.0" - - else -> null - } - fallbackId?.let(SpdxLicenses.embedded::findByIdentifier)?.let { license -> + getFallbackId(url)?.let(SpdxLicenses.embedded::findByIdentifier)?.let { license -> return listOf(license) } } else if (name != null) { diff --git a/src/test/kotlin/app/cash/licensee/LicensesTest.kt b/src/test/kotlin/app/cash/licensee/LicensesTest.kt new file mode 100644 index 00000000..5e29cd5c --- /dev/null +++ b/src/test/kotlin/app/cash/licensee/LicensesTest.kt @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 Square, Inc. + * + * Licensed 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. + */ +package app.cash.licensee + +import org.junit.Assert.assertEquals +import org.junit.Test + +class LicensesTest { + + @Suppress("HttpUrlsUsage") + @Test fun fallbackId() { + assertEquals("Apache-2.0", getFallbackId("http://www.apache.org/licenses/LICENSE-2.0.txt")) + assertEquals("Apache-2.0", getFallbackId("http://www.apache.org/licenses/LICENSE-2.0.html")) + assertEquals("Apache-2.0", getFallbackId("https://www.opensource.org/licenses/apache2.0.php")) + assertEquals("Apache-2.0", getFallbackId("https://opensource.org/license/apache-2-0")) + + assertEquals("CC0-1.0", getFallbackId("https://creativecommons.org/publicdomain/zero/1.0")) + assertEquals("GPL-2.0-or-later", getFallbackId("https://choosealicense.com/licenses/gpl-2.0")) + assertEquals("EPL-2.0", getFallbackId("https://www.eclipse.org/legal/epl-2.0")) + } +} From 31870b779592b15afb9e7cfbbf6a48080c6b5526 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 18 Aug 2023 21:17:13 +0800 Subject: [PATCH 3/4] Add more & Cleanups --- src/main/kotlin/app/cash/licensee/licenses.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/app/cash/licensee/licenses.kt b/src/main/kotlin/app/cash/licensee/licenses.kt index 63b7671f..0d20cea7 100644 --- a/src/main/kotlin/app/cash/licensee/licenses.kt +++ b/src/main/kotlin/app/cash/licensee/licenses.kt @@ -60,8 +60,8 @@ internal fun getFallbackId(url: String): String? { return when (fixedUrl) { "apache.org/licenses/license-2.0", "api.github.com/licenses/apache-2.0", - "opensource.org/licenses/apache2.0", "opensource.org/license/apache-2-0", + "opensource.org/licenses/apache2.0", -> "Apache-2.0" "api.github.com/licenses/cc0-1.0", @@ -70,19 +70,23 @@ internal fun getFallbackId(url: String): String? { "api.github.com/licenses/lgpl-2.1", "gnu.org/licenses/old-licenses/lgpl-2.1", + "opensource.org/license/lgpl-2-1", "opensource.org/licenses/lgpl-2.1", -> "LGPL-2.1-only" "api.github.com/licenses/mit", + "opensource.org/license/mit", "opensource.org/licenses/mit-license", -> "MIT" "api.github.com/licenses/bsd-2-clause", + "opensource.org/license/bsd-2-clause", "opensource.org/licenses/bsd-license", -> "BSD-2-Clause" "api.github.com/licenses/bsd-3-clause", - "opensource.org/licenses/BSD-3-Clause", + "opensource.org/license/bsd-3-clause", + "opensource.org/licenses/bsd-3-Clause", -> "BSD-3-Clause" "gnu.org/software/classpath/license", From 37ddedb2c00102193b38a88da08bb95edb12cbdf Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 18 Aug 2023 21:31:01 +0800 Subject: [PATCH 4/4] Format --- src/test/kotlin/app/cash/licensee/LicensesTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/app/cash/licensee/LicensesTest.kt b/src/test/kotlin/app/cash/licensee/LicensesTest.kt index 5e29cd5c..bd660920 100644 --- a/src/test/kotlin/app/cash/licensee/LicensesTest.kt +++ b/src/test/kotlin/app/cash/licensee/LicensesTest.kt @@ -21,7 +21,8 @@ import org.junit.Test class LicensesTest { @Suppress("HttpUrlsUsage") - @Test fun fallbackId() { + @Test + fun fallbackId() { assertEquals("Apache-2.0", getFallbackId("http://www.apache.org/licenses/LICENSE-2.0.txt")) assertEquals("Apache-2.0", getFallbackId("http://www.apache.org/licenses/LICENSE-2.0.html")) assertEquals("Apache-2.0", getFallbackId("https://www.opensource.org/licenses/apache2.0.php"))