]> source.dussan.org Git - gitea.git/commitdiff
Support repo license (#24872)
authoryp05327 <576951401@qq.com>
Tue, 1 Oct 2024 19:25:08 +0000 (04:25 +0900)
committerGitHub <noreply@github.com>
Tue, 1 Oct 2024 19:25:08 +0000 (15:25 -0400)
Close #278
Close #24076

## Solutions:
- Use
[google/licenseclassifier](https://github.com/google/licenseclassifier/)
Test result between
[google/licensecheck](https://github.com/google/licensecheck) and
[go-license-detector](https://github.com/go-enry/go-license-detector):
https://github.com/go-gitea/gitea/pull/24872#issuecomment-1560361167
Test result between
[google/licensecheck](https://github.com/google/licensecheck) and
[google/licenseclassifier](https://github.com/google/licenseclassifier/):
https://github.com/go-gitea/gitea/pull/24872#issuecomment-1576092178
- Generate License Convert Name List to avoid import license templates
with same contents
Gitea automatically get latest license data from[
spdx/license-list-data](https://github.com/spdx/license-list-data).
But unfortunately, some license templates have same contents. #20915
[click here to see the
list](https://github.com/go-gitea/gitea/pull/24872#issuecomment-1584141684)
So we will generate a list of these license templates with same contents
and create a new file to save the result when using `make
generate-license`. (Need to decide the save path)
- Save License info into a new table `repo_license`
Can easily support searching repo by license in the future.

## Screen shot
Single License:

![image](https://github.com/go-gitea/gitea/assets/18380374/41260bd7-0b4c-4038-8592-508706cffa9f)

Multiple Licenses:

![image](https://github.com/go-gitea/gitea/assets/18380374/34ce2f73-7e18-446b-9b96-ecc4fb61bd70)

Triggers:
- [x] Push commit to default branch
- [x] Create repo
- [x] Mirror repo
- [x] When Default Branch is changed, licenses should be updated

Todo:
- [x] Save Licenses info in to DB when there's a change to license file
in the commit
- [x] DB Migration
- [x] A nominal test?
- [x] Select which library to
use(https://github.com/go-gitea/gitea/pull/24872#issuecomment-1560361167)
- [x] API Support
- [x] Add repo license table
- ~Select license in settings if there are several licenses(Not
recommended)~
- License board(later, not in this PR)

![image](https://github.com/go-gitea/gitea/assets/18380374/2c3c3bf8-bcc2-4c6d-8ce0-81d1a9733878)

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Denys Konovalov <kontakt@denyskon.de>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: 6543 <m.huber@kithara.com>
Co-authored-by: a1012112796 <1012112796@qq.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.com>
47 files changed:
assets/go-licenses.json
build/generate-licenses.go
build/license/aliasgenerator.go [new file with mode: 0644]
build/license/aliasgenerator_test.go [new file with mode: 0644]
go.mod
go.sum
models/fixtures/repo_license.yml [new file with mode: 0644]
models/fixtures/repository.yml
models/migrations/migrations.go
models/migrations/v1_23/v305.go [new file with mode: 0644]
models/repo/license.go [new file with mode: 0644]
modules/repository/license.go
modules/structs/repo.go
options/license/etc/license-aliases.json [new file with mode: 0644]
options/locale/locale_en-US.ini
routers/api/v1/api.go
routers/api/v1/repo/license.go [new file with mode: 0644]
routers/api/v1/repo/repo.go
routers/api/v1/swagger/repo.go
routers/init.go
routers/private/default_branch.go
routers/private/hook_post_receive.go
routers/web/repo/branch.go
routers/web/repo/commit.go
routers/web/repo/release.go
routers/web/repo/view.go
services/context/repo.go
services/convert/repository.go
services/cron/tasks_basic.go
services/migrations/gitea_uploader_test.go
services/mirror/mirror_pull.go
services/repository/branch.go
services/repository/create.go
services/repository/delete.go
services/repository/fork.go
services/repository/license.go [new file with mode: 0644]
services/repository/license_test.go [new file with mode: 0644]
services/repository/migrate.go
services/repository/repository.go
templates/repo/sub_menu.tmpl
templates/swagger/v1_json.tmpl
tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6 [new file with mode: 0644]
tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a [new file with mode: 0644]
tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa [new file with mode: 0644]
tests/gitea-repositories-meta/user2/repo1.git/refs/heads/DefaultBranch
tests/integration/api_admin_test.go
tests/integration/api_repo_license_test.go [new file with mode: 0644]

index 1c0711307fce74fe206c464ad7a1d3d35abb803d..4b78a12030e3fc1cf1e79eb93167db0e517d0155 100644 (file)
     "path": "github.com/google/go-tpm/LICENSE",
     "licenseText": "\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright [yyyy] [name of copyright owner]\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
   },
+  {
+    "name": "github.com/google/licenseclassifier/v2",
+    "path": "github.com/google/licenseclassifier/v2/LICENSE",
+    "licenseText": "\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright [yyyy] [name of copyright owner]\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
+  },
   {
     "name": "github.com/google/pprof/profile",
     "path": "github.com/google/pprof/profile/LICENSE",
index 9a111bc81115035a49f91105c19700bfbf410e98..66e1d3775515e5a34fbdb22665a73afa72062597 100644 (file)
@@ -1,3 +1,6 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
 //go:build ignore
 
 package main
@@ -5,6 +8,8 @@ package main
 import (
        "archive/tar"
        "compress/gzip"
+       "crypto/md5"
+       "encoding/hex"
        "flag"
        "fmt"
        "io"
@@ -15,6 +20,8 @@ import (
        "path/filepath"
        "strings"
 
+       "code.gitea.io/gitea/build/license"
+       "code.gitea.io/gitea/modules/json"
        "code.gitea.io/gitea/modules/util"
 )
 
@@ -77,7 +84,7 @@ func main() {
        }
 
        tr := tar.NewReader(gz)
-
+       aliasesFiles := make(map[string][]string)
        for {
                hdr, err := tr.Next()
 
@@ -97,26 +104,73 @@ func main() {
                        continue
                }
 
-               if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
+               fileBaseName := filepath.Base(hdr.Name)
+               licenseName := strings.TrimSuffix(fileBaseName, ".txt")
+
+               if strings.HasPrefix(fileBaseName, "README") {
                        continue
                }
 
-               if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
+               if strings.HasPrefix(fileBaseName, "deprecated_") {
                        continue
                }
-               out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
+               out, err := os.Create(path.Join(destination, licenseName))
                if err != nil {
                        log.Fatalf("Failed to create new file. %s", err)
                }
 
                defer out.Close()
 
-               if _, err := io.Copy(out, tr); err != nil {
+               // some license files have same content, so we need to detect these files and create a convert map into a json file
+               // Later we use this convert map to avoid adding same license content with different license name
+               h := md5.New()
+               // calculate md5 and write file in the same time
+               r := io.TeeReader(tr, h)
+               if _, err := io.Copy(out, r); err != nil {
                        log.Fatalf("Failed to write new file. %s", err)
                } else {
                        fmt.Printf("Written %s\n", out.Name())
+
+                       md5 := hex.EncodeToString(h.Sum(nil))
+                       aliasesFiles[md5] = append(aliasesFiles[md5], licenseName)
                }
        }
 
+       // generate convert license name map
+       licenseAliases := make(map[string]string)
+       for _, fileNames := range aliasesFiles {
+               if len(fileNames) > 1 {
+                       licenseName := license.GetLicenseNameFromAliases(fileNames)
+                       if licenseName == "" {
+                               // license name should not be empty as expected
+                               // if it is empty, we need to rewrite the logic of GetLicenseNameFromAliases
+                               log.Fatalf("GetLicenseNameFromAliases: license name is empty")
+                       }
+                       for _, fileName := range fileNames {
+                               licenseAliases[fileName] = licenseName
+                       }
+               }
+       }
+       // save convert license name map to file
+       b, err := json.Marshal(licenseAliases)
+       if err != nil {
+               log.Fatalf("Failed to create json bytes. %s", err)
+       }
+
+       licenseAliasesDestination := filepath.Join(destination, "etc", "license-aliases.json")
+       if err := os.MkdirAll(filepath.Dir(licenseAliasesDestination), 0o755); err != nil {
+               log.Fatalf("Failed to create directory for license aliases json file. %s", err)
+       }
+
+       f, err := os.Create(licenseAliasesDestination)
+       if err != nil {
+               log.Fatalf("Failed to create license aliases json file. %s", err)
+       }
+       defer f.Close()
+
+       if _, err = f.Write(b); err != nil {
+               log.Fatalf("Failed to write license aliases json file. %s", err)
+       }
+
        fmt.Println("Done")
 }
diff --git a/build/license/aliasgenerator.go b/build/license/aliasgenerator.go
new file mode 100644 (file)
index 0000000..7de1e6f
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package license
+
+import "strings"
+
+func GetLicenseNameFromAliases(fnl []string) string {
+       if len(fnl) == 0 {
+               return ""
+       }
+
+       shortestItem := func(list []string) string {
+               s := list[0]
+               for _, l := range list[1:] {
+                       if len(l) < len(s) {
+                               s = l
+                       }
+               }
+               return s
+       }
+       allHasPrefix := func(list []string, s string) bool {
+               for _, l := range list {
+                       if !strings.HasPrefix(l, s) {
+                               return false
+                       }
+               }
+               return true
+       }
+
+       sl := shortestItem(fnl)
+       slv := strings.Split(sl, "-")
+       var result string
+       for i := len(slv); i >= 0; i-- {
+               result = strings.Join(slv[:i], "-")
+               if allHasPrefix(fnl, result) {
+                       return result
+               }
+       }
+       return ""
+}
diff --git a/build/license/aliasgenerator_test.go b/build/license/aliasgenerator_test.go
new file mode 100644 (file)
index 0000000..239181b
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package license
+
+import (
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestGetLicenseNameFromAliases(t *testing.T) {
+       tests := []struct {
+               target string
+               inputs []string
+       }{
+               {
+                       // real case which you can find in license-aliases.json
+                       target: "AGPL-1.0",
+                       inputs: []string{
+                               "AGPL-1.0-only",
+                               "AGPL-1.0-or-late",
+                       },
+               },
+               {
+                       target: "",
+                       inputs: []string{
+                               "APSL-1.0",
+                               "AGPL-1.0-only",
+                               "AGPL-1.0-or-late",
+                       },
+               },
+       }
+
+       for _, tt := range tests {
+               result := GetLicenseNameFromAliases(tt.inputs)
+               assert.Equal(t, result, tt.target)
+       }
+}
diff --git a/go.mod b/go.mod
index e620b8f70fcda0d916132fce4d9e541eb366d14f..8dd6947925281c566ae75dd222c6d27b8a205e98 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -68,6 +68,7 @@ require (
        github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85
        github.com/golang-jwt/jwt/v5 v5.2.1
        github.com/google/go-github/v61 v61.0.0
+       github.com/google/licenseclassifier/v2 v2.0.0
        github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8
        github.com/google/uuid v1.6.0
        github.com/gorilla/feeds v1.2.0
diff --git a/go.sum b/go.sum
index ee6e3c3382377f12e8399f0f78ea34a7c77220b3..aa592053b53e2eb0847f6bbdc1cdc91ac076a156 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -441,6 +441,8 @@ github.com/google/go-tpm v0.9.0/go.mod h1:FkNVkc6C+IsvDI9Jw1OveJmxGZUUaKxtrpOS47
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
 github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/google/licenseclassifier/v2 v2.0.0 h1:1Y57HHILNf4m0ABuMVb6xk4vAJYEUO0gDxNpog0pyeA=
+github.com/google/licenseclassifier/v2 v2.0.0/go.mod h1:cOjbdH0kyC9R22sdQbYsFkto4NGCAc+ZSwbeThazEtM=
 github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
 github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8 h1:ASJ/LAqdCHOyMYI+dwNxn7Rd8FscNkMyTr1KZU1JI/M=
 github.com/google/pprof v0.0.0-20240618054019-d3b898a103f8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
@@ -735,6 +737,7 @@ github.com/sassoftware/go-rpmutils v0.4.0/go.mod h1:3goNWi7PGAT3/dlql2lv3+MSN5jN
 github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
 github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
 github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
+github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
 github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
diff --git a/models/fixtures/repo_license.yml b/models/fixtures/repo_license.yml
new file mode 100644 (file)
index 0000000..ca780a7
--- /dev/null
@@ -0,0 +1 @@
+[] # empty
index 9adc6c855b1e531ad6366fa97ef746ee2c3c27f4..e141593f41576af44e9c15909fc7440d48fc6bbf 100644 (file)
@@ -26,7 +26,7 @@
   fork_id: 0
   is_template: false
   template_id: 0
-  size: 7597
+  size: 8478
   is_fsck_enabled: true
   close_issues_via_commit_in_any_branch: false
 
index 13551423ce4703ee64aa8f0119801e079a77fff2..f99718ead285991890c20846abf15bff7a322c0c 100644 (file)
@@ -500,7 +500,7 @@ var migrations = []Migration{
        // v259 -> v260
        NewMigration("Convert scoped access tokens", v1_20.ConvertScopedAccessTokens),
 
-       // Gitea 1.20.0 ends at 260
+       // Gitea 1.20.0 ends at v260
 
        // v260 -> v261
        NewMigration("Drop custom_labels column of action_runner table", v1_21.DropCustomLabelsColumnOfActionRunner),
@@ -601,6 +601,8 @@ var migrations = []Migration{
        NewMigration("Add metadata column for comment table", v1_23.AddCommentMetaDataColumn),
        // v304 -> v305
        NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1),
+       // v305 -> v306
+       NewMigration("Add Repository Licenses", v1_23.AddRepositoryLicenses),
 }
 
 // GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v1_23/v305.go b/models/migrations/v1_23/v305.go
new file mode 100644 (file)
index 0000000..4d88119
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_23 //nolint
+
+import (
+       "code.gitea.io/gitea/modules/timeutil"
+
+       "xorm.io/xorm"
+)
+
+func AddRepositoryLicenses(x *xorm.Engine) error {
+       type RepoLicense struct {
+               ID          int64 `xorm:"pk autoincr"`
+               RepoID      int64 `xorm:"UNIQUE(s) NOT NULL"`
+               CommitID    string
+               License     string             `xorm:"VARCHAR(255) UNIQUE(s) NOT NULL"`
+               CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
+               UpdatedUnix timeutil.TimeStamp `xorm:"INDEX UPDATED"`
+       }
+
+       return x.Sync(new(RepoLicense))
+}
diff --git a/models/repo/license.go b/models/repo/license.go
new file mode 100644 (file)
index 0000000..366b459
--- /dev/null
@@ -0,0 +1,120 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+       "context"
+
+       "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/modules/timeutil"
+)
+
+func init() {
+       db.RegisterModel(new(RepoLicense))
+}
+
+type RepoLicense struct { //revive:disable-line:exported
+       ID          int64 `xorm:"pk autoincr"`
+       RepoID      int64 `xorm:"UNIQUE(s) NOT NULL"`
+       CommitID    string
+       License     string             `xorm:"VARCHAR(255) UNIQUE(s) NOT NULL"`
+       CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
+       UpdatedUnix timeutil.TimeStamp `xorm:"INDEX UPDATED"`
+}
+
+// RepoLicenseList defines a list of repo licenses
+type RepoLicenseList []*RepoLicense //revive:disable-line:exported
+
+func (rll RepoLicenseList) StringList() []string {
+       var licenses []string
+       for _, rl := range rll {
+               licenses = append(licenses, rl.License)
+       }
+       return licenses
+}
+
+// GetRepoLicenses returns the license statistics for a repository
+func GetRepoLicenses(ctx context.Context, repo *Repository) (RepoLicenseList, error) {
+       licenses := make(RepoLicenseList, 0)
+       if err := db.GetEngine(ctx).Where("`repo_id` = ?", repo.ID).Asc("`license`").Find(&licenses); err != nil {
+               return nil, err
+       }
+       return licenses, nil
+}
+
+// UpdateRepoLicenses updates the license statistics for repository
+func UpdateRepoLicenses(ctx context.Context, repo *Repository, commitID string, licenses []string) error {
+       oldLicenses, err := GetRepoLicenses(ctx, repo)
+       if err != nil {
+               return err
+       }
+       for _, license := range licenses {
+               upd := false
+               for _, o := range oldLicenses {
+                       // Update already existing license
+                       if o.License == license {
+                               if _, err := db.GetEngine(ctx).ID(o.ID).Cols("`commit_id`").Update(o); err != nil {
+                                       return err
+                               }
+                               upd = true
+                               break
+                       }
+               }
+               // Insert new license
+               if !upd {
+                       if err := db.Insert(ctx, &RepoLicense{
+                               RepoID:   repo.ID,
+                               CommitID: commitID,
+                               License:  license,
+                       }); err != nil {
+                               return err
+                       }
+               }
+       }
+       // Delete old licenses
+       licenseToDelete := make([]int64, 0, len(oldLicenses))
+       for _, o := range oldLicenses {
+               if o.CommitID != commitID {
+                       licenseToDelete = append(licenseToDelete, o.ID)
+               }
+       }
+       if len(licenseToDelete) > 0 {
+               if _, err := db.GetEngine(ctx).In("`id`", licenseToDelete).Delete(&RepoLicense{}); err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
+// CopyLicense Copy originalRepo license information to destRepo (use for forked repo)
+func CopyLicense(ctx context.Context, originalRepo, destRepo *Repository) error {
+       repoLicenses, err := GetRepoLicenses(ctx, originalRepo)
+       if err != nil {
+               return err
+       }
+       if len(repoLicenses) > 0 {
+               newRepoLicenses := make(RepoLicenseList, 0, len(repoLicenses))
+
+               for _, rl := range repoLicenses {
+                       newRepoLicense := &RepoLicense{
+                               RepoID:   destRepo.ID,
+                               CommitID: rl.CommitID,
+                               License:  rl.License,
+                       }
+                       newRepoLicenses = append(newRepoLicenses, newRepoLicense)
+               }
+               if err := db.Insert(ctx, &newRepoLicenses); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+// CleanRepoLicenses will remove all license record of the repo
+func CleanRepoLicenses(ctx context.Context, repo *Repository) error {
+       return db.DeleteBeans(ctx, &RepoLicense{
+               RepoID: repo.ID,
+       })
+}
index 6ac3547e7b2f503749d37082fb792fd46becdead..9da3af84f852839677e0489989f6a22ae98774a4 100644 (file)
@@ -23,7 +23,7 @@ type LicenseValues struct {
 func GetLicense(name string, values *LicenseValues) ([]byte, error) {
        data, err := options.License(name)
        if err != nil {
-               return nil, fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
+               return nil, fmt.Errorf("GetLicense[%s]: %w", name, err)
        }
        return fillLicensePlaceholder(name, values, data), nil
 }
index fd27df384da2f3113d7560c6bad9f57cda0eddc3..832ffa8bcc958d10acbea037e1361d1d425f24dd 100644 (file)
@@ -114,6 +114,7 @@ type Repository struct {
        MirrorUpdated time.Time     `json:"mirror_updated,omitempty"`
        RepoTransfer  *RepoTransfer `json:"repo_transfer"`
        Topics        []string      `json:"topics"`
+       Licenses      []string      `json:"licenses"`
 }
 
 // CreateRepoOption options when creating repository
diff --git a/options/license/etc/license-aliases.json b/options/license/etc/license-aliases.json
new file mode 100644 (file)
index 0000000..fe2cf2d
--- /dev/null
@@ -0,0 +1 @@
+{"AGPL-1.0-only":"AGPL-1.0","AGPL-1.0-or-later":"AGPL-1.0","AGPL-3.0-only":"AGPL-3.0","AGPL-3.0-or-later":"AGPL-3.0","CAL-1.0":"CAL-1.0","CAL-1.0-Combined-Work-Exception":"CAL-1.0","GFDL-1.1-invariants-only":"GFDL-1.1","GFDL-1.1-invariants-or-later":"GFDL-1.1","GFDL-1.1-no-invariants-only":"GFDL-1.1","GFDL-1.1-no-invariants-or-later":"GFDL-1.1","GFDL-1.1-only":"GFDL-1.1","GFDL-1.1-or-later":"GFDL-1.1","GFDL-1.2-invariants-only":"GFDL-1.2","GFDL-1.2-invariants-or-later":"GFDL-1.2","GFDL-1.2-no-invariants-only":"GFDL-1.2","GFDL-1.2-no-invariants-or-later":"GFDL-1.2","GFDL-1.2-only":"GFDL-1.2","GFDL-1.2-or-later":"GFDL-1.2","GFDL-1.3-invariants-only":"GFDL-1.3","GFDL-1.3-invariants-or-later":"GFDL-1.3","GFDL-1.3-no-invariants-only":"GFDL-1.3","GFDL-1.3-no-invariants-or-later":"GFDL-1.3","GFDL-1.3-only":"GFDL-1.3","GFDL-1.3-or-later":"GFDL-1.3","GPL-1.0-only":"GPL-1.0","GPL-1.0-or-later":"GPL-1.0","GPL-2.0-only":"GPL-2.0","GPL-2.0-or-later":"GPL-2.0","GPL-3.0-only":"GPL-3.0","GPL-3.0-or-later":"GPL-3.0","LGPL-2.0-only":"LGPL-2.0","LGPL-2.0-or-later":"LGPL-2.0","LGPL-2.1-only":"LGPL-2.1","LGPL-2.1-or-later":"LGPL-2.1","LGPL-3.0-only":"LGPL-3.0","LGPL-3.0-or-later":"LGPL-3.0","MPL-2.0":"MPL-2.0","MPL-2.0-no-copyleft-exception":"MPL-2.0","OFL-1.0":"OFL-1.0","OFL-1.0-RFN":"OFL-1.0","OFL-1.0-no-RFN":"OFL-1.0","OFL-1.1":"OFL-1.1","OFL-1.1-RFN":"OFL-1.1","OFL-1.1-no-RFN":"OFL-1.1"}
\ No newline at end of file
index f77fd203a2d6b38a1b081df17f0917389baf99e1..e3b17f9a04f2d24c86cbf0bbd4d3cc519f786ada 100644 (file)
@@ -1040,6 +1040,7 @@ issue_labels_helper = Select an issue label set.
 license = License
 license_helper = Select a license file.
 license_helper_desc = A license governs what others can and can't do with your code. Not sure which one is right for your project? See <a target="_blank" rel="noopener noreferrer" href="%s">Choose a license.</a>
+multiple_licenses = Multiple Licenses
 object_format = Object Format
 object_format_helper = Object format of the repository. Cannot be changed later. SHA1 is most compatible.
 readme = README
@@ -2942,6 +2943,7 @@ dashboard.start_schedule_tasks = Start actions schedule tasks
 dashboard.sync_branch.started = Branches Sync started
 dashboard.sync_tag.started = Tags Sync started
 dashboard.rebuild_issue_indexer = Rebuild issue indexer
+dashboard.sync_repo_licenses = Sync repo licenses
 
 users.user_manage_panel = User Account Management
 users.new_account = Create User Account
index 1244676508ee554c1c25808b09f571895f543504..5aa8ad44e5e3d2bb536d0261897cd984f39fc2a8 100644 (file)
@@ -1327,6 +1327,7 @@ func Routes() *web.Router {
                                m.Get("/issue_config", context.ReferencesGitRepo(), repo.GetIssueConfig)
                                m.Get("/issue_config/validate", context.ReferencesGitRepo(), repo.ValidateIssueConfig)
                                m.Get("/languages", reqRepoReader(unit.TypeCode), repo.GetLanguages)
+                               m.Get("/licenses", reqRepoReader(unit.TypeCode), repo.GetLicenses)
                                m.Get("/activities/feeds", repo.ListRepoActivityFeeds)
                                m.Get("/new_pin_allowed", repo.AreNewIssuePinsAllowed)
                                m.Group("/avatar", func() {
diff --git a/routers/api/v1/repo/license.go b/routers/api/v1/repo/license.go
new file mode 100644 (file)
index 0000000..8a6bdfd
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+       "net/http"
+
+       repo_model "code.gitea.io/gitea/models/repo"
+       "code.gitea.io/gitea/modules/log"
+       "code.gitea.io/gitea/services/context"
+)
+
+// GetLicenses returns licenses
+func GetLicenses(ctx *context.APIContext) {
+       // swagger:operation GET /repos/{owner}/{repo}/licenses repository repoGetLicenses
+       // ---
+       // summary: Get repo licenses
+       // produces:
+       //   - application/json
+       // parameters:
+       // - name: owner
+       //   in: path
+       //   description: owner of the repo
+       //   type: string
+       //   required: true
+       // - name: repo
+       //   in: path
+       //   description: name of the repo
+       //   type: string
+       //   required: true
+       // responses:
+       //   "404":
+       //     "$ref": "#/responses/notFound"
+       //   "200":
+       //     "$ref": "#/responses/LicensesList"
+
+       licenses, err := repo_model.GetRepoLicenses(ctx, ctx.Repo.Repository)
+       if err != nil {
+               log.Error("GetRepoLicenses failed: %v", err)
+               ctx.InternalServerError(err)
+               return
+       }
+
+       resp := make([]string, len(licenses))
+       for i := range licenses {
+               resp[i] = licenses[i].License
+       }
+
+       ctx.JSON(http.StatusOK, resp)
+}
index 1bcec8fcf7e720e81324ca04cd3c37505b359006..6c1a94ee168a9c88c6746e93a3c120d9c9249f8b 100644 (file)
@@ -731,6 +731,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
        }
 
        // Default branch only updated if changed and exist or the repository is empty
+       updateRepoLicense := false
        if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) {
                if !repo.IsEmpty {
                        if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
@@ -739,6 +740,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
                                        return err
                                }
                        }
+                       updateRepoLicense = true
                }
                repo.DefaultBranch = *opts.DefaultBranch
        }
@@ -748,6 +750,15 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
                return err
        }
 
+       if updateRepoLicense {
+               if err := repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{
+                       RepoID: ctx.Repo.Repository.ID,
+               }); err != nil {
+                       ctx.Error(http.StatusInternalServerError, "AddRepoToLicenseUpdaterQueue", err)
+                       return err
+               }
+       }
+
        log.Trace("Repository basic settings updated: %s/%s", owner.Name, repo.Name)
        return nil
 }
index 345835f9a58cd76754232b08cdb59a1fc34c8634..b9d2a0217cd4f74a09b6309d76d28547439dac1f 100644 (file)
@@ -359,6 +359,13 @@ type swaggerLanguageStatistics struct {
        Body map[string]int64 `json:"body"`
 }
 
+// LicensesList
+// swagger:response LicensesList
+type swaggerLicensesList struct {
+       // in: body
+       Body []string `json:"body"`
+}
+
 // CombinedStatus
 // swagger:response CombinedStatus
 type swaggerCombinedStatus struct {
index e21f763c1e5271dcec44920ed293af9b8b23e92d..fe80dfd2cdd85639f4292dcea9e0b740766327e2 100644 (file)
@@ -172,6 +172,8 @@ func InitWebInstalled(ctx context.Context) {
 
        actions_service.Init()
 
+       mustInit(repo_service.InitLicenseClassifier)
+
        // Finally start up the cron
        cron.NewContext(ctx)
 }
index 7be909f955d170e61bb89e13010b821e40b845b2..03c19c8ff4020c8544934229d16941a389ebdf47 100644 (file)
@@ -12,6 +12,7 @@ import (
        "code.gitea.io/gitea/modules/gitrepo"
        "code.gitea.io/gitea/modules/private"
        gitea_context "code.gitea.io/gitea/services/context"
+       repo_service "code.gitea.io/gitea/services/repository"
 )
 
 // SetDefaultBranch updates the default branch
@@ -36,5 +37,15 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
                })
                return
        }
+
+       if err := repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{
+               RepoID: ctx.Repo.Repository.ID,
+       }); err != nil {
+               ctx.JSON(http.StatusInternalServerError, private.Response{
+                       Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
+               })
+               return
+       }
+
        ctx.PlainText(http.StatusOK, "success")
 }
index 2d1688523c4888af25d90d9b179c1af7ecf0ae9e..5c01216356dba9b630e0cfe47b227e3aa925c3c7 100644 (file)
@@ -278,10 +278,19 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
 
                        branch := refFullName.BranchName()
 
-                       // If our branch is the default branch of an unforked repo - there's no PR to create or refer to
-                       if !repo.IsFork && branch == baseRepo.DefaultBranch {
-                               results = append(results, private.HookPostReceiveBranchResult{})
-                               continue
+                       if branch == baseRepo.DefaultBranch {
+                               if err := repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{
+                                       RepoID: repo.ID,
+                               }); err != nil {
+                                       ctx.JSON(http.StatusInternalServerError, private.Response{Err: err.Error()})
+                                       return
+                               }
+
+                               // If our branch is the default branch of an unforked repo - there's no PR to create or refer to
+                               if !repo.IsFork {
+                                       results = append(results, private.HookPostReceiveBranchResult{})
+                                       continue
+                               }
                        }
 
                        pr, err := issues_model.GetUnmergedPullRequest(ctx, repo.ID, baseRepo.ID, branch, baseRepo.DefaultBranch, issues_model.PullRequestFlowGithub)
index 4897a5f4fcdf66da820273cbe8318eaf48f070f3..4a62237838a4a8d1bbf0b6f0f43497f44738892f 100644 (file)
@@ -89,7 +89,7 @@ func Branches(ctx *context.Context) {
        pager := context.NewPagination(int(branchesCount), pageSize, page, 5)
        pager.SetDefaultParams(ctx)
        ctx.Data["Page"] = pager
-
+       ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
        ctx.HTML(http.StatusOK, tplBranch)
 }
 
index a433dd228e3ebeec1a93f9e86ea403df4b470dc8..0e4e10bf50944643b7d8553f3f99d28f3a0ac31a 100644 (file)
@@ -29,7 +29,7 @@ import (
        "code.gitea.io/gitea/modules/util"
        "code.gitea.io/gitea/services/context"
        "code.gitea.io/gitea/services/gitdiff"
-       git_service "code.gitea.io/gitea/services/repository"
+       repo_service "code.gitea.io/gitea/services/repository"
 )
 
 const (
@@ -101,7 +101,7 @@ func Commits(ctx *context.Context) {
        pager := context.NewPagination(int(commitsCount), pageSize, page, 5)
        pager.SetDefaultParams(ctx)
        ctx.Data["Page"] = pager
-
+       ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
        ctx.HTML(http.StatusOK, tplCommits)
 }
 
@@ -218,6 +218,8 @@ func SearchCommits(ctx *context.Context) {
        }
        ctx.Data["Username"] = ctx.Repo.Owner.Name
        ctx.Data["Reponame"] = ctx.Repo.Repository.Name
+       ctx.Data["RefName"] = ctx.Repo.RefName
+       ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
        ctx.HTML(http.StatusOK, tplCommits)
 }
 
@@ -263,12 +265,12 @@ func FileHistory(ctx *context.Context) {
        pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
        pager.SetDefaultParams(ctx)
        ctx.Data["Page"] = pager
-
+       ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
        ctx.HTML(http.StatusOK, tplCommits)
 }
 
 func LoadBranchesAndTags(ctx *context.Context) {
-       response, err := git_service.LoadBranchesAndTags(ctx, ctx.Repo, ctx.PathParam("sha"))
+       response, err := repo_service.LoadBranchesAndTags(ctx, ctx.Repo, ctx.PathParam("sha"))
        if err == nil {
                ctx.JSON(http.StatusOK, response)
                return
index f551fffe956bc75c31f4e8f8f09c4a2ddbedef6d..566a82316f8e02f070fc961050fefcaa7061f266 100644 (file)
@@ -289,7 +289,6 @@ func releasesOrTagsFeed(ctx *context.Context, isReleasesOnly bool, formatType st
 // SingleRelease renders a single release's page
 func SingleRelease(ctx *context.Context) {
        ctx.Data["PageIsReleaseList"] = true
-       ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
 
        writeAccess := ctx.Repo.CanWrite(unit.TypeReleases)
        ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived
index 12d202e4a0d4573df519272cd457a628aae81a05..976911760948500fd0b325b29e4395f926ca4036 100644 (file)
@@ -51,6 +51,7 @@ import (
        "code.gitea.io/gitea/routers/web/feed"
        "code.gitea.io/gitea/services/context"
        issue_service "code.gitea.io/gitea/services/issue"
+       repo_service "code.gitea.io/gitea/services/repository"
        files_service "code.gitea.io/gitea/services/repository/files"
 
        "github.com/nektos/act/pkg/model"
@@ -1077,6 +1078,7 @@ func renderHomeCode(ctx *context.Context) {
        ctx.Data["TreeLink"] = treeLink
        ctx.Data["TreeNames"] = treeNames
        ctx.Data["BranchLink"] = branchLink
+       ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
        ctx.HTML(http.StatusOK, tplRepoHome)
 }
 
index e0d3a0bfd3e43f6e7fcc107dfbd87ec7d4c04301..c001255283ae4ea9d81afc175684cb92b3066dcd 100644 (file)
@@ -404,6 +404,13 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
        ctx.Data["PushMirrors"] = pushMirrors
        ctx.Data["RepoName"] = ctx.Repo.Repository.Name
        ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty
+
+       repoLicenses, err := repo_model.GetRepoLicenses(ctx, ctx.Repo.Repository)
+       if err != nil {
+               ctx.ServerError("GetRepoLicenses", err)
+               return
+       }
+       ctx.Data["DetectedRepoLicenses"] = repoLicenses.StringList()
 }
 
 // RepoAssignment returns a middleware to handle repository assignment
index 751260a45d3d92d92ac1214b143bc289f9077708..e026d0f4405f415555ee586e548b6d100f8184b6 100644 (file)
@@ -175,6 +175,11 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR
                language = repo.PrimaryLanguage.Language
        }
 
+       repoLicenses, err := repo_model.GetRepoLicenses(ctx, repo)
+       if err != nil {
+               return nil
+       }
+
        repoAPIURL := repo.APIURL()
 
        return &api.Repository{
@@ -238,6 +243,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR
                RepoTransfer:                  transfer,
                Topics:                        repo.Topics,
                ObjectFormatName:              repo.ObjectFormatName,
+               Licenses:                      repoLicenses.StringList(),
        }
 }
 
index 2a213ae51524c993f9d222d68f2c4ffeb6b98337..fb5938745e61c6c406d48c5f1977f39dc515d4d0 100644 (file)
@@ -156,6 +156,16 @@ func registerCleanupPackages() {
        })
 }
 
+func registerSyncRepoLicenses() {
+       RegisterTaskFatal("sync_repo_licenses", &BaseConfig{
+               Enabled:    false,
+               RunAtStart: false,
+               Schedule:   "@annually",
+       }, func(ctx context.Context, _ *user_model.User, config Config) error {
+               return repo_service.SyncRepoLicenses(ctx)
+       })
+}
+
 func initBasicTasks() {
        if setting.Mirror.Enabled {
                registerUpdateMirrorTask()
@@ -172,4 +182,5 @@ func initBasicTasks() {
        if setting.Packages.Enabled {
                registerCleanupPackages()
        }
+       registerSyncRepoLicenses()
 }
index c9b924809819baab5ab4f3bd690fc0bd79b099f7..f2379dadf8fae5e1a7d8284d738ae2b05a40c05e 100644 (file)
@@ -26,6 +26,7 @@ import (
        "code.gitea.io/gitea/modules/optional"
        "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/test"
+       repo_service "code.gitea.io/gitea/services/repository"
 
        "github.com/stretchr/testify/assert"
 )
@@ -302,6 +303,8 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
        toRepoName := "migrated"
        uploader := NewGiteaLocalUploader(context.Background(), fromRepoOwner, fromRepoOwner.Name, toRepoName)
        uploader.gitServiceType = structs.GiteaService
+
+       assert.NoError(t, repo_service.Init(context.Background()))
        assert.NoError(t, uploader.CreateRepo(&base.Repository{
                Description: "description",
                OriginalURL: fromRepo.RepoPath(),
index 9f7ffb29c9f36be06ee0c7c21f3b9f40584180fa..654a50d11efa6e59dfa4a91dded1a2e35f71b9a3 100644 (file)
@@ -24,6 +24,7 @@ import (
        "code.gitea.io/gitea/modules/timeutil"
        "code.gitea.io/gitea/modules/util"
        notify_service "code.gitea.io/gitea/services/notify"
+       repo_service "code.gitea.io/gitea/services/repository"
 )
 
 // gitShortEmptySha Git short empty SHA
@@ -559,6 +560,14 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
                }
        }
 
+       // Update License
+       if err = repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{
+               RepoID: m.Repo.ID,
+       }); err != nil {
+               log.Error("SyncMirrors [repo: %-v]: unable to add repo to license updater queue: %v", m.Repo, err)
+               return false
+       }
+
        log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)
 
        return true
index f5cdb72a7bb085ff93c85ba39e7038e2ba5dacd0..67df4363e441a5c1f2795e54facd4f3084c5f857 100644 (file)
@@ -612,6 +612,14 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
                return err
        }
 
+       if !repo.IsEmpty {
+               if err := AddRepoToLicenseUpdaterQueue(&LicenseUpdaterOptions{
+                       RepoID: repo.ID,
+               }); err != nil {
+                       log.Error("AddRepoToLicenseUpdaterQueue: %v", err)
+               }
+       }
+
        notify_service.ChangeDefaultBranch(ctx, repo)
 
        return nil
index 971793bcc6e35b38caebf108e6d76e7403c9917e..282b2d3e58d46ec01727e755e39637914ead69ba 100644 (file)
@@ -303,6 +303,25 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
                        rollbackRepo.OwnerID = u.ID
                        return fmt.Errorf("CreateRepository(git update-server-info): %w", err)
                }
+
+               // update licenses
+               var licenses []string
+               if len(opts.License) > 0 {
+                       licenses = append(licenses, ConvertLicenseName(opts.License))
+
+                       stdout, _, err := git.NewCommand(ctx, "rev-parse", "HEAD").
+                               SetDescription(fmt.Sprintf("CreateRepository(git rev-parse HEAD): %s", repoPath)).
+                               RunStdString(&git.RunOpts{Dir: repoPath})
+                       if err != nil {
+                               log.Error("CreateRepository(git rev-parse HEAD) in %v: Stdout: %s\nError: %v", repo, stdout, err)
+                               rollbackRepo = repo
+                               rollbackRepo.OwnerID = u.ID
+                               return fmt.Errorf("CreateRepository(git rev-parse HEAD): %w", err)
+                       }
+                       if err := repo_model.UpdateRepoLicenses(ctx, repo, stdout, licenses); err != nil {
+                               return err
+                       }
+               }
                return nil
        }); err != nil {
                if rollbackRepo != nil {
index cd779b05c3501bd41b695fcf934cc067dcb2afee..e580833140891bd2150f16c1445e562fbcc3f078 100644 (file)
@@ -140,6 +140,7 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
                &git_model.Branch{RepoID: repoID},
                &git_model.LFSLock{RepoID: repoID},
                &repo_model.LanguageStat{RepoID: repoID},
+               &repo_model.RepoLicense{RepoID: repoID},
                &issues_model.Milestone{RepoID: repoID},
                &repo_model.Mirror{RepoID: repoID},
                &activities_model.Notification{RepoID: repoID},
index f074fd1082118b0858274b2caf2602dedf4ca9e0..e1145556791c5668ab717293ab300f53cfdb8e44 100644 (file)
@@ -198,6 +198,9 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
        if err := repo_model.CopyLanguageStat(ctx, opts.BaseRepo, repo); err != nil {
                log.Error("Copy language stat from oldRepo failed: %v", err)
        }
+       if err := repo_model.CopyLicense(ctx, opts.BaseRepo, repo); err != nil {
+               return nil, err
+       }
 
        gitRepo, err := gitrepo.OpenRepository(ctx, repo)
        if err != nil {
diff --git a/services/repository/license.go b/services/repository/license.go
new file mode 100644 (file)
index 0000000..2453be3
--- /dev/null
@@ -0,0 +1,205 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repository
+
+import (
+       "context"
+       "fmt"
+       "io"
+
+       "code.gitea.io/gitea/models/db"
+       repo_model "code.gitea.io/gitea/models/repo"
+       "code.gitea.io/gitea/modules/container"
+       "code.gitea.io/gitea/modules/git"
+       "code.gitea.io/gitea/modules/gitrepo"
+       "code.gitea.io/gitea/modules/graceful"
+       "code.gitea.io/gitea/modules/json"
+       "code.gitea.io/gitea/modules/log"
+       "code.gitea.io/gitea/modules/options"
+       "code.gitea.io/gitea/modules/queue"
+
+       licenseclassifier "github.com/google/licenseclassifier/v2"
+)
+
+var (
+       classifier      *licenseclassifier.Classifier
+       LicenseFileName = "LICENSE"
+       licenseAliases  map[string]string
+
+       // licenseUpdaterQueue represents a queue to handle update repo licenses
+       licenseUpdaterQueue *queue.WorkerPoolQueue[*LicenseUpdaterOptions]
+)
+
+func AddRepoToLicenseUpdaterQueue(opts *LicenseUpdaterOptions) error {
+       if opts == nil {
+               return nil
+       }
+       return licenseUpdaterQueue.Push(opts)
+}
+
+func loadLicenseAliases() error {
+       if licenseAliases != nil {
+               return nil
+       }
+
+       data, err := options.AssetFS().ReadFile("license", "etc", "license-aliases.json")
+       if err != nil {
+               return err
+       }
+       err = json.Unmarshal(data, &licenseAliases)
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+func ConvertLicenseName(name string) string {
+       if err := loadLicenseAliases(); err != nil {
+               return name
+       }
+
+       v, ok := licenseAliases[name]
+       if ok {
+               return v
+       }
+       return name
+}
+
+func InitLicenseClassifier() error {
+       // threshold should be 0.84~0.86 or the test will be failed
+       classifier = licenseclassifier.NewClassifier(.85)
+       licenseFiles, err := options.AssetFS().ListFiles("license", true)
+       if err != nil {
+               return err
+       }
+
+       existLicense := make(container.Set[string])
+       if len(licenseFiles) > 0 {
+               for _, licenseFile := range licenseFiles {
+                       licenseName := ConvertLicenseName(licenseFile)
+                       if existLicense.Contains(licenseName) {
+                               continue
+                       }
+                       existLicense.Add(licenseName)
+                       data, err := options.License(licenseFile)
+                       if err != nil {
+                               return err
+                       }
+                       classifier.AddContent("License", licenseFile, licenseName, data)
+               }
+       }
+       return nil
+}
+
+type LicenseUpdaterOptions struct {
+       RepoID int64
+}
+
+func repoLicenseUpdater(items ...*LicenseUpdaterOptions) []*LicenseUpdaterOptions {
+       ctx := graceful.GetManager().ShutdownContext()
+
+       for _, opts := range items {
+               repo, err := repo_model.GetRepositoryByID(ctx, opts.RepoID)
+               if err != nil {
+                       log.Error("repoLicenseUpdater [%d] failed: GetRepositoryByID: %v", opts.RepoID, err)
+                       continue
+               }
+               if repo.IsEmpty {
+                       continue
+               }
+
+               gitRepo, err := gitrepo.OpenRepository(ctx, repo)
+               if err != nil {
+                       log.Error("repoLicenseUpdater [%d] failed: OpenRepository: %v", opts.RepoID, err)
+                       continue
+               }
+               defer gitRepo.Close()
+
+               commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
+               if err != nil {
+                       log.Error("repoLicenseUpdater [%d] failed: GetBranchCommit: %v", opts.RepoID, err)
+                       continue
+               }
+               if err = UpdateRepoLicenses(ctx, repo, commit); err != nil {
+                       log.Error("repoLicenseUpdater [%d] failed: updateRepoLicenses: %v", opts.RepoID, err)
+               }
+       }
+       return nil
+}
+
+func SyncRepoLicenses(ctx context.Context) error {
+       log.Trace("Doing: SyncRepoLicenses")
+
+       if err := db.Iterate(
+               ctx,
+               nil,
+               func(ctx context.Context, repo *repo_model.Repository) error {
+                       select {
+                       case <-ctx.Done():
+                               return db.ErrCancelledf("before sync repo licenses for %s", repo.FullName())
+                       default:
+                       }
+                       return AddRepoToLicenseUpdaterQueue(&LicenseUpdaterOptions{RepoID: repo.ID})
+               },
+       ); err != nil {
+               log.Trace("Error: SyncRepoLicenses: %v", err)
+               return err
+       }
+
+       log.Trace("Finished: SyncReposLicenses")
+       return nil
+}
+
+// UpdateRepoLicenses will update repository licenses col if license file exists
+func UpdateRepoLicenses(ctx context.Context, repo *repo_model.Repository, commit *git.Commit) error {
+       if commit == nil {
+               return nil
+       }
+
+       b, err := commit.GetBlobByPath(LicenseFileName)
+       if err != nil && !git.IsErrNotExist(err) {
+               return fmt.Errorf("GetBlobByPath: %w", err)
+       }
+
+       if git.IsErrNotExist(err) {
+               return repo_model.CleanRepoLicenses(ctx, repo)
+       }
+
+       licenses := make([]string, 0)
+       if b != nil {
+               r, err := b.DataAsync()
+               if err != nil {
+                       return err
+               }
+               defer r.Close()
+
+               licenses, err = detectLicense(r)
+               if err != nil {
+                       return fmt.Errorf("detectLicense: %w", err)
+               }
+       }
+       return repo_model.UpdateRepoLicenses(ctx, repo, commit.ID.String(), licenses)
+}
+
+// detectLicense returns the licenses detected by the given content buff
+func detectLicense(r io.Reader) ([]string, error) {
+       if r == nil {
+               return nil, nil
+       }
+
+       matches, err := classifier.MatchFrom(r)
+       if err != nil {
+               return nil, err
+       }
+       if len(matches.Matches) > 0 {
+               results := make(container.Set[string], len(matches.Matches))
+               for _, r := range matches.Matches {
+                       if r.MatchType == "License" && !results.Contains(r.Variant) {
+                               results.Add(r.Variant)
+                       }
+               }
+               return results.Values(), nil
+       }
+       return nil, nil
+}
diff --git a/services/repository/license_test.go b/services/repository/license_test.go
new file mode 100644 (file)
index 0000000..39e9738
--- /dev/null
@@ -0,0 +1,73 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repository
+
+import (
+       "fmt"
+       "strings"
+       "testing"
+
+       repo_module "code.gitea.io/gitea/modules/repository"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func Test_detectLicense(t *testing.T) {
+       type DetectLicenseTest struct {
+               name string
+               arg  string
+               want []string
+       }
+
+       tests := []DetectLicenseTest{
+               {
+                       name: "empty",
+                       arg:  "",
+                       want: nil,
+               },
+               {
+                       name: "no detected license",
+                       arg:  "Copyright (c) 2023 Gitea",
+                       want: nil,
+               },
+       }
+
+       repo_module.LoadRepoConfig()
+       err := loadLicenseAliases()
+       assert.NoError(t, err)
+       for _, licenseName := range repo_module.Licenses {
+               license, err := repo_module.GetLicense(licenseName, &repo_module.LicenseValues{
+                       Owner: "Gitea",
+                       Email: "teabot@gitea.io",
+                       Repo:  "gitea",
+                       Year:  "2024",
+               })
+               assert.NoError(t, err)
+
+               tests = append(tests, DetectLicenseTest{
+                       name: fmt.Sprintf("single license test: %s", licenseName),
+                       arg:  string(license),
+                       want: []string{ConvertLicenseName(licenseName)},
+               })
+       }
+
+       err = InitLicenseClassifier()
+       assert.NoError(t, err)
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       license, err := detectLicense(strings.NewReader(tt.arg))
+                       assert.NoError(t, err)
+                       assert.Equal(t, tt.want, license)
+               })
+       }
+
+       result, err := detectLicense(strings.NewReader(tests[2].arg + tests[3].arg + tests[4].arg))
+       assert.NoError(t, err)
+       t.Run("multiple licenses test", func(t *testing.T) {
+               assert.Equal(t, 3, len(result))
+               assert.Contains(t, result, tests[2].want[0])
+               assert.Contains(t, result, tests[3].want[0])
+               assert.Contains(t, result, tests[4].want[0])
+       })
+}
index 2e901791b4adf50114107bd7ba2837fca1e52490..c627b46fab56ff7d34538ecf01b4228fd66f527f 100644 (file)
@@ -172,6 +172,11 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
                                return repo, fmt.Errorf("StoreMissingLfsObjectsInRepository: %w", err)
                        }
                }
+
+               // Update repo license
+               if err := AddRepoToLicenseUpdaterQueue(&LicenseUpdaterOptions{RepoID: repo.ID}); err != nil {
+                       log.Error("Failed to add repo to license updater queue: %v", err)
+               }
        }
 
        ctx, committer, err := db.TxContext(ctx)
index 5306e7d45cce67b7a8ed2e6c10ec2d9a0d997c2e..59b4491132da983ed137ac1c9bdcf7710b7debf1 100644 (file)
@@ -18,6 +18,7 @@ import (
        user_model "code.gitea.io/gitea/models/user"
        "code.gitea.io/gitea/modules/graceful"
        "code.gitea.io/gitea/modules/log"
+       "code.gitea.io/gitea/modules/queue"
        repo_module "code.gitea.io/gitea/modules/repository"
        "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/modules/structs"
@@ -96,6 +97,12 @@ func PushCreateRepo(ctx context.Context, authUser, owner *user_model.User, repoN
 
 // Init start repository service
 func Init(ctx context.Context) error {
+       licenseUpdaterQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "repo_license_updater", repoLicenseUpdater)
+       if licenseUpdaterQueue == nil {
+               return fmt.Errorf("unable to create repo_license_updater queue")
+       }
+       go graceful.GetManager().RunWithCancel(licenseUpdaterQueue)
+
        if err := repo_module.LoadRepoConfig(); err != nil {
                return err
        }
index 87d21103149435aae45c37cd422df293a59cdecd..6f53acd31ece4271147c7d362f98da0a3172525e 100644 (file)
                                        {{svg "octicon-tag"}} <b>{{ctx.Locale.PrettyNumber .NumTags}}</b> {{ctx.Locale.TrN .NumTags "repo.tag" "repo.tags"}}
                                </a>
                        {{end}}
-                       <span class="item not-mobile" {{if not (eq .Repository.Size 0)}}data-tooltip-content="{{.Repository.SizeDetailsString}}"{{end}}>
+                       {{if .DetectedRepoLicenses}}
+                               <a class="item muted" href="{{.RepoLink}}/src/{{.Repository.DefaultBranch}}/{{PathEscapeSegments .LicenseFileName}}" data-tooltip-placement="top" data-tooltip-content="{{StringUtils.Join .DetectedRepoLicenses ", "}}">
+                                       {{svg "octicon-law"}} <b>{{if eq (len .DetectedRepoLicenses) 1}}{{index .DetectedRepoLicenses 0}}{{else}}{{ctx.Locale.Tr "repo.multiple_licenses"}}{{end}}</b>
+                               </a>
+                       {{end}}
+                       <span class="item not-mobile" {{if not (eq .Repository.Size 0)}}data-tooltip-placement="top" data-tooltip-content="{{.Repository.SizeDetailsString}}"{{end}}>
                                {{$fileSizeFormatted := FileSize .Repository.Size}}{{/* the formatted string is always "{val} {unit}" */}}
                                {{$fileSizeFields := StringUtils.Split $fileSizeFormatted " "}}
                                {{svg "octicon-database"}} <b>{{ctx.Locale.PrettyNumber (index $fileSizeFields 0)}}</b> {{index $fileSizeFields 1}}
index 598350550276ed5824265a6748e94ab54410c820..bac918ac3899df6e1c22b6ca638053248e442652 100644 (file)
         }
       }
     },
+    "/repos/{owner}/{repo}/licenses": {
+      "get": {
+        "produces": [
+          "application/json"
+        ],
+        "tags": [
+          "repository"
+        ],
+        "summary": "Get repo licenses",
+        "operationId": "repoGetLicenses",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          }
+        ],
+        "responses": {
+          "200": {
+            "$ref": "#/responses/LicensesList"
+          },
+          "404": {
+            "$ref": "#/responses/notFound"
+          }
+        }
+      }
+    },
     "/repos/{owner}/{repo}/media/{filepath}": {
       "get": {
         "produces": [
           "type": "string",
           "x-go-name": "LanguagesURL"
         },
+        "licenses": {
+          "type": "array",
+          "items": {
+            "type": "string"
+          },
+          "x-go-name": "Licenses"
+        },
         "link": {
           "type": "string",
           "x-go-name": "Link"
         }
       }
     },
+    "LicensesList": {
+      "description": "LicensesList",
+      "schema": {
+        "type": "array",
+        "items": {
+          "type": "string"
+        }
+      }
+    },
     "MarkdownRender": {
       "description": "MarkdownRender is a rendered markdown document",
       "schema": {
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6 b/tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6
new file mode 100644 (file)
index 0000000..69b1e03
Binary files /dev/null and b/tests/gitea-repositories-meta/user2/repo1.git/objects/08/51b61d9f8ca0e9e63617e11907988ee88b1ca6 differ
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a b/tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a
new file mode 100644 (file)
index 0000000..4c92556
Binary files /dev/null and b/tests/gitea-repositories-meta/user2/repo1.git/objects/12/8105ae73669ac2a4cb42751538f0c65c54e28a differ
diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa b/tests/gitea-repositories-meta/user2/repo1.git/objects/90/c1019714259b24fb81711d4416ac0f18667dfa
new file mode 100644 (file)
index 0000000..bbd3bf7
--- /dev/null
@@ -0,0 +1,2 @@
+x\ 1\95\8eM
+Â0\10F]ç\14Ù\v2\93d&\19\10ñ*ù\99`Á¶¶Æ\85··Wð[>Þ\83¯®ó<\rëÐ\9dÆ®j!\11\16Æ&=Õ\f\9e1*¢@\94\94TS*X3\9bWÞu\19\96©cé.\96êK\8fÐ90E¦Ä\9e\94\19$h+µ\84fòg<ÖÝ~_@ÞE{=,!\f\80÷m»Ôu¾Y\8c \8e\84B´g8f\ ez|\eú_erkö9U]Þj~2]<¼
\ No newline at end of file
index f98a263be62ff7dfa901b2c1bebc970ef842d55f..5abf667b61ee7bd56e4d5c4e5f139970d5f93574 100644 (file)
@@ -1 +1 @@
-65f1bf27bc3bf70f64657658635e66094edbcb4d
+90c1019714259b24fb81711d4416ac0f18667dfa
index 92da7ce041cdaac238bb3e4aa55b0158a982ddb5..66209ee4e0c895a0d608b022efc8df32571ce03f 100644 (file)
@@ -304,11 +304,11 @@ func TestAPICron(t *testing.T) {
                        AddTokenAuth(token)
                resp := MakeRequest(t, req, http.StatusOK)
 
-               assert.Equal(t, "28", resp.Header().Get("X-Total-Count"))
+               assert.Equal(t, "29", resp.Header().Get("X-Total-Count"))
 
                var crons []api.Cron
                DecodeJSON(t, resp, &crons)
-               assert.Len(t, crons, 28)
+               assert.Len(t, crons, 29)
        })
 
        t.Run("Execute", func(t *testing.T) {
diff --git a/tests/integration/api_repo_license_test.go b/tests/integration/api_repo_license_test.go
new file mode 100644 (file)
index 0000000..52d3085
--- /dev/null
@@ -0,0 +1,80 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package integration
+
+import (
+       "fmt"
+       "net/http"
+       "net/url"
+       "testing"
+       "time"
+
+       auth_model "code.gitea.io/gitea/models/auth"
+       api "code.gitea.io/gitea/modules/structs"
+
+       "github.com/stretchr/testify/assert"
+)
+
+var testLicenseContent = `
+Copyright (c) 2024 Gitea 
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+`
+
+func TestAPIRepoLicense(t *testing.T) {
+       onGiteaRun(t, func(t *testing.T, u *url.URL) {
+               session := loginUser(t, "user2")
+
+               // Request editor page
+               req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
+               resp := session.MakeRequest(t, req, http.StatusOK)
+
+               doc := NewHTMLParser(t, resp.Body)
+               lastCommit := doc.GetInputValueByName("last_commit")
+               assert.NotEmpty(t, lastCommit)
+
+               // Save new file to master branch
+               req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
+                       "_csrf":         doc.GetCSRF(),
+                       "last_commit":   lastCommit,
+                       "tree_path":     "LICENSE",
+                       "content":       testLicenseContent,
+                       "commit_choice": "direct",
+               })
+               session.MakeRequest(t, req, http.StatusSeeOther)
+
+               // let gitea update repo license
+               time.Sleep(time.Second)
+               checkRepoLicense(t, "user2", "repo1", []string{"BSD-2-Clause"})
+
+               // Change default branch
+               token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
+               branchName := "DefaultBranch"
+               req = NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1", api.EditRepoOption{
+                       DefaultBranch: &branchName,
+               }).AddTokenAuth(token)
+               session.MakeRequest(t, req, http.StatusOK)
+
+               // let gitea update repo license
+               time.Sleep(time.Second)
+               checkRepoLicense(t, "user2", "repo1", []string{"MIT"})
+       })
+}
+
+func checkRepoLicense(t *testing.T, owner, repo string, expected []string) {
+       reqURL := fmt.Sprintf("/api/v1/repos/%s/%s/licenses", owner, repo)
+       req := NewRequest(t, "GET", reqURL)
+       resp := MakeRequest(t, req, http.StatusOK)
+
+       var licenses []string
+       DecodeJSON(t, resp, &licenses)
+
+       assert.ElementsMatch(t, expected, licenses, 0)
+}